insert_columns
Add one or more columns to an Excel sheet at a specified location. Define the start column and count to customize insertion, enhancing workbook organization and data structure.
Instructions
Insert one or more columns starting at the specified column.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | ||
| filepath | Yes | ||
| sheet_name | Yes | ||
| start_col | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/excel_mcp/server.py:617-633 (handler)MCP tool handler for the 'insert_columns' tool. This function is decorated with @mcp.tool(), which both defines the tool interface (parameters and docstring serving as schema) and registers it with the FastMCP server. It wraps the core implementation by calling insert_cols from sheet.py.
@mcp.tool() def insert_columns( filepath: str, sheet_name: str, start_col: int, count: int = 1 ) -> str: """Insert one or more columns starting at the specified column.""" try: full_path = get_excel_path(filepath) result = insert_cols(full_path, sheet_name, start_col, count) return result["message"] except (ValidationError, SheetError) as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error inserting columns: {e}") raise - src/excel_mcp/sheet.py:395-419 (helper)Core helper function implementing the column insertion logic using openpyxl. Loads the workbook, validates inputs, calls worksheet.insert_cols(), saves the file, and returns a success message.
def insert_cols(filepath: str, sheet_name: str, start_col: int, count: int = 1) -> Dict[str, Any]: """Insert one or more columns starting at the specified column.""" try: wb = load_workbook(filepath) if sheet_name not in wb.sheetnames: raise SheetError(f"Sheet '{sheet_name}' not found") worksheet = wb[sheet_name] # Validate parameters if start_col < 1: raise ValidationError("Start column must be 1 or greater") if count < 1: raise ValidationError("Count must be 1 or greater") worksheet.insert_cols(start_col, count) wb.save(filepath) return {"message": f"Inserted {count} column(s) starting at column {start_col} in sheet '{sheet_name}'"} except (ValidationError, SheetError) as e: logger.error(str(e)) raise except Exception as e: logger.error(f"Failed to insert columns: {e}") raise SheetError(str(e))