create_worksheet
Add a new worksheet to an Excel workbook by specifying the file path and sheet name. Streamline workbook organization and data management with this tool on the Excel MCP Server.
Instructions
Create new worksheet in workbook.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| sheet_name | Yes |
Implementation Reference
- src/excel_mcp/server.py:279-291 (handler)MCP tool handler and registration for 'create_worksheet'. Resolves the full file path using get_excel_path, imports and calls the create_sheet helper from workbook module, handles errors, and returns the result message.@mcp.tool() def create_worksheet(filepath: str, sheet_name: str) -> str: """Create new worksheet in workbook.""" try: full_path = get_excel_path(filepath) from excel_mcp.workbook import create_sheet as create_worksheet_impl result = create_worksheet_impl(full_path, sheet_name) return result["message"] except (ValidationError, WorkbookError) as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error creating worksheet: {e}") raise
- src/excel_mcp/workbook.py:42-62 (helper)Core helper function implementing the worksheet creation logic using openpyxl. Loads the workbook, checks if the sheet already exists, creates a new sheet if not, saves the workbook, and returns a success message.def create_sheet(filepath: str, sheet_name: str) -> dict: """Create a new worksheet in the workbook if it doesn't exist.""" try: wb = load_workbook(filepath) # Check if sheet already exists if sheet_name in wb.sheetnames: raise WorkbookError(f"Sheet {sheet_name} already exists") # Create new sheet wb.create_sheet(sheet_name) wb.save(filepath) wb.close() return {"message": f"Sheet {sheet_name} created successfully"} except WorkbookError as e: logger.error(str(e)) raise except Exception as e: logger.error(f"Failed to create sheet: {e}") raise WorkbookError(str(e))