delete_sheet_columns
Remove specified columns from an Excel sheet by defining the starting column and count. Simplify data cleanup and reorganization for improved spreadsheet management.
Instructions
Delete one or more columns starting at the specified column.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | ||
| filepath | Yes | ||
| sheet_name | Yes | ||
| start_col | Yes |
Implementation Reference
- src/excel_mcp/server.py:653-669 (handler)MCP tool handler function for 'delete_sheet_columns' that resolves file path and delegates to core delete_cols implementation, handles errors and returns message.@mcp.tool() def delete_sheet_columns( filepath: str, sheet_name: str, start_col: int, count: int = 1 ) -> str: """Delete one or more columns starting at the specified column.""" try: full_path = get_excel_path(filepath) result = delete_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 deleting columns: {e}") raise
- src/excel_mcp/sheet.py:449-475 (helper)Core helper function that performs the actual column deletion using openpyxl: loads workbook, validates inputs, calls worksheet.delete_cols(start_col, count), saves the file, and returns success message.def delete_cols(filepath: str, sheet_name: str, start_col: int, count: int = 1) -> Dict[str, Any]: """Delete 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") if start_col > worksheet.max_column: raise ValidationError(f"Start column {start_col} exceeds worksheet bounds (max column: {worksheet.max_column})") worksheet.delete_cols(start_col, count) wb.save(filepath) return {"message": f"Deleted {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 delete columns: {e}") raise SheetError(str(e))