Skip to main content
Glama

update_table_column_name_tool

Modify column names in Google Sheets tables by specifying indices and new names. This tool updates existing column headers to organize data effectively.

Instructions

Update column names in a table.

This tool updates existing column names in a table by their index.
The number of column indices must match the number of new column names.

Args:
    spreadsheet_name: Name of the spreadsheet
    sheet_name: Name of the sheet containing the table
    table_name: Name of the table to update column names in
    column_indices: List of column indices to update (0-based)
    new_column_names: List of new column names

Returns:
    JSON string with success status and update details

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
spreadsheet_nameYesThe name of the Google Spreadsheet
sheet_nameYesThe name of the sheet containing the table
table_nameYesName of the table to update column names in
column_indicesYesList of column indices to update (0-based)
new_column_namesYesList of new column names (must match column_indices count)

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • MCP tool handler: defines input schema with Field descriptions, decorated with @mcp.tool() for registration, and delegates execution to the core handler function.
    @mcp.tool()
    def update_table_column_name_tool(
        spreadsheet_name: str = Field(..., description="The name of the Google Spreadsheet"),
        sheet_name: str = Field(..., description="The name of the sheet containing the table"),
        table_name: str = Field(..., description="Name of the table to update column names in"),
        column_indices: List[int] = Field(..., description="List of column indices to update (0-based)"),
        new_column_names: List[str] = Field(..., description="List of new column names (must match column_indices count)")
    ) -> str:
        """
        Update column names in a table.
        
        This tool updates existing column names in a table by their index.
        The number of column indices must match the number of new column names.
        
        Args:
            spreadsheet_name: Name of the spreadsheet
            sheet_name: Name of the sheet containing the table
            table_name: Name of the table to update column names in
            column_indices: List of column indices to update (0-based)
            new_column_names: List of new column names
        
        Returns:
            JSON string with success status and update details
        """
        sheets_service, drive_service = _get_google_services()
        return update_table_column_name_handler(drive_service, sheets_service, spreadsheet_name, sheet_name, table_name, column_indices, new_column_names)
  • Core handler implementation: performs input validation, retrieves spreadsheet/sheet/table info using helper utils, constructs UpdateTableRequest, executes Google Sheets batchUpdate API to update column names and properties.
    def update_table_column_name_handler(
        drive_service,
        sheets_service,
        spreadsheet_name: str,
        sheet_name: str,
        table_name: str,
        column_indices: List[int],
        new_column_names: List[str]
    ) -> str:
        """
        Update column names in a table in Google Sheets using the official updateTable operation.
        
        According to the official Google Sheets API documentation, to update table column names:
        1. Use UpdateTableRequest to update column properties including column names
        2. Update both the column properties and header cells
        
        Args:
            drive_service: Google Drive service instance
            sheets_service: Google Sheets service instance
            spreadsheet_name: Name of the spreadsheet
            sheet_name: Name of the sheet containing the table
            table_name: Name of the table to update column names in
            column_indices: List of column indices to update (0-based)
            new_column_names: List of new column names (must match column_indices count)
        
        Returns:
            str: Success message with update details or error message
        """
        try:
            # Validate inputs
            if not table_name or table_name.strip() == "":
                return compact_json_response({
                    "success": False,
                    "message": "Table name is required."
                })
            
            if not column_indices or not isinstance(column_indices, list):
                return compact_json_response({
                    "success": False,
                    "message": "Column indices are required and must be a list."
                })
            
            if not new_column_names or not isinstance(new_column_names, list):
                return compact_json_response({
                    "success": False,
                    "message": "New column names are required and must be a list."
                })
            
            if len(column_indices) != len(new_column_names):
                return compact_json_response({
                    "success": False,
                    "message": "Number of column indices must match number of new column names."
                })
            
            # Validate column indices and names
            validated_renames = []
            invalid_renames = []
            
            for i, (col_index, new_name) in enumerate(zip(column_indices, new_column_names)):
                if not isinstance(col_index, int) or col_index < 0:
                    invalid_renames.append({"index": i, "column_index": col_index, "error": "Column index must be a non-negative integer"})
                    continue
                
                if not new_name or not isinstance(new_name, str) or new_name.strip() == "":
                    invalid_renames.append({"index": i, "new_name": new_name, "error": "New column name cannot be empty"})
                    continue
                
                validated_renames.append({
                    "column_index": col_index,
                    "new_name": new_name.strip()
                })
            
            if invalid_renames:
                error_messages = [f"Rename {item['index']+1}: {item['error']}" for item in invalid_renames]
                return compact_json_response({
                    "success": False,
                    "message": f"Invalid column renames: {'; '.join(error_messages)}",
                    "invalid_renames": invalid_renames
                })
            
            if not validated_renames:
                return compact_json_response({
                    "success": False,
                    "message": "No valid column renames provided after validation."
                })
            
            # Get spreadsheet ID
            spreadsheet_id = get_spreadsheet_id_by_name(drive_service, spreadsheet_name)
            if not spreadsheet_id:
                return compact_json_response({
                    "success": False,
                    "message": f"Spreadsheet '{spreadsheet_name}' not found."
                })
    
            # Get sheet ID
            sheet_ids = get_sheet_ids_by_names(sheets_service, spreadsheet_id, [sheet_name])
            sheet_id = sheet_ids.get(sheet_name)
            if sheet_id is None:
                return compact_json_response({
                    "success": False,
                    "message": f"Sheet '{sheet_name}' not found in spreadsheet '{spreadsheet_name}'."
                })
            
            # Get table ID
            table_ids = get_table_ids_by_names(sheets_service, spreadsheet_id, sheet_name, [table_name])
            table_id = table_ids.get(table_name)
            if not table_id:
                return compact_json_response({
                    "success": False,
                    "message": f"Table '{table_name}' not found in sheet '{sheet_name}'."
                })
            
            # Get table information
            try:
                table_info = get_table_info(sheets_service, spreadsheet_id, table_id)
                columns = table_info.get('columns', [])
                table_range = table_info.get('range', {})
            except Exception as e:
                return compact_json_response({
                    "success": False,
                    "message": f"Could not retrieve information for table '{table_name}': {str(e)}"
                })
            
            # Validate that all column indices exist in the table
            existing_column_count = len(columns)
            invalid_indices = []
            valid_renames = []
            
            for rename in validated_renames:
                col_index = rename["column_index"]
                if col_index >= existing_column_count:
                    invalid_indices.append(col_index)
                else:
                    valid_renames.append(rename)
            
            if invalid_indices:
                return compact_json_response({
                    "success": False,
                    "message": f"Invalid column indices: {invalid_indices}. Table has {existing_column_count} columns (0-based indexing)."
                })
            
            if not valid_renames:
                return compact_json_response({
                    "success": False,
                    "message": "No valid column renames after validation."
                })
            
            # Create batch update requests
            requests = []
            
            # Create mapping of column indices to new names
            rename_mapping = {rename["column_index"]: rename["new_name"] for rename in valid_renames}
            
            # Convert existing columns to API format and update column names
            updated_column_properties = []
            for col in columns:
                col_name = col.get("name", "")
                col_type = col.get("type", "TEXT")
                col_index = col.get("index", 0)
                
                # Create API format column property
                api_col_prop = {
                    "columnIndex": col_index,
                    "columnName": rename_mapping.get(col_index, col_name),  # Use new name if in mapping, otherwise keep old
                    "columnType": col_type
                }
                
                # Preserve dataValidationRule if it exists
                if "dataValidationRule" in col:
                    api_col_prop["dataValidationRule"] = col["dataValidationRule"]
                
                updated_column_properties.append(api_col_prop)
            
            # Update table with new column properties
            update_table_request = {
                "updateTable": {
                    "table": {
                        "tableId": table_id,
                        "columnProperties": updated_column_properties
                    },
                    "fields": "columnProperties.columnName"
                }
            }
            requests.append(update_table_request)
            
            # Execute the batch update
            response = sheets_service.spreadsheets().batchUpdate(
                spreadsheetId=spreadsheet_id,
                body={"requests": requests}
            ).execute()
            
            # Extract response information
            replies = response.get("replies", [])
            successful_renames = len(valid_renames)
            
            response_data = {
                "success": True,
                "spreadsheet_name": spreadsheet_name,
                "sheet_name": sheet_name,
                "table_name": table_name,
                "columns_renamed": successful_renames,
                "renames": valid_renames,
                "message": f"Successfully renamed {successful_renames} column(s) in table '{table_name}' in '{sheet_name}'"
            }
            
            return compact_json_response(response_data)
            
        except HttpError as error:
            return compact_json_response({
                "success": False,
                "message": f"Google Sheets API error: {str(error)}"
            })
        except Exception as e:
            return compact_json_response({
                "success": False,
                "message": f"Error renaming table columns: {str(e)}"
            }) 
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It indicates this is a mutation tool ('update'), specifies the matching requirement for indices and names, and mentions the return format ('JSON string with success status and update details'). However, it lacks details on permissions, error handling, or side effects (e.g., impact on existing data).

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured and concise: it starts with a clear purpose statement, adds a key constraint, and then lists parameters and returns in a formatted way. Every sentence earns its place, with no redundant information, making it easy to scan and understand.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity (mutation tool with 5 parameters), no annotations, and an output schema present, the description is fairly complete. It covers the purpose, key constraints, parameters, and return format. However, it could improve by addressing behavioral aspects like permissions or error cases, which are missing.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, so the input schema already documents all parameters thoroughly. The description lists the parameters in the 'Args' section, but this adds minimal value beyond the schema. It does clarify the relationship between 'column_indices' and 'new_column_names' (must match in count), which is useful but not extensive.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Update column names in a table.' It specifies the verb ('update') and resource ('column names in a table'), making the action clear. However, it doesn't explicitly differentiate from sibling tools like 'update_table_title_tool' or 'update_table_column_type_tool', which also modify table properties.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage by stating 'updates existing column names in a table by their index' and that 'The number of column indices must match the number of new column names,' providing some context for when to use it. However, it doesn't explicitly mention when to choose this tool over alternatives like 'update_table_title_tool' or 'add_table_column_tool,' nor does it specify prerequisites or exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/henilcalagiya/google-sheets-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server