rename_worksheet
Rename a worksheet in an Excel workbook by specifying the file path, current sheet name, and new name.
Instructions
Rename worksheet in workbook.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| old_name | Yes | ||
| new_name | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/excel_mcp/server.py:491-496 (registration)Registration of the 'rename_worksheet' tool as an MCP tool with FastMCP, including title 'Rename Worksheet' and destructiveHint=True annotation.
@mcp.tool( annotations=ToolAnnotations( title="Rename Worksheet", destructiveHint=True, ), ) - src/excel_mcp/server.py:497-511 (handler)Tool handler for rename_worksheet: accepts filepath, old_name, new_name; calls get_excel_path for path resolution and rename_sheet from sheet.py for the actual renaming logic.
def rename_worksheet( filepath: str, old_name: str, new_name: str ) -> str: """Rename worksheet in workbook.""" try: full_path = get_excel_path(filepath) result = rename_sheet(full_path, old_name, new_name) return result["message"] except (ValidationError, SheetError) as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error renaming worksheet: {e}") raise - src/excel_mcp/server.py:497-501 (schema)Schema/type definition via function signature: filepath (str), old_name (str), new_name (str). Docstring: 'Rename worksheet in workbook.'
def rename_worksheet( filepath: str, old_name: str, new_name: str ) -> str: - src/excel_mcp/sheet.py:58-77 (helper)Helper function rename_sheet: loads workbook via openpyxl, validates old_name exists and new_name doesn't, renames the sheet via sheet.title = new_name, and saves.
def rename_sheet(filepath: str, old_name: str, new_name: str) -> Dict[str, Any]: """Rename a worksheet.""" try: wb = load_workbook(filepath) if old_name not in wb.sheetnames: raise SheetError(f"Sheet '{old_name}' not found") if new_name in wb.sheetnames: raise SheetError(f"Sheet '{new_name}' already exists") sheet = wb[old_name] sheet.title = new_name wb.save(filepath) return {"message": f"Sheet renamed from '{old_name}' to '{new_name}'"} except SheetError as e: logger.error(str(e)) raise except Exception as e: logger.error(f"Failed to rename sheet: {e}") raise SheetError(str(e))