delete_sheet_rows
Remove specified rows from an Excel sheet by defining the start row and count. Streamlines data cleanup and restructuring within workbooks for improved organization.
Instructions
Delete one or more rows starting at the specified row.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | ||
| filepath | Yes | ||
| sheet_name | Yes | ||
| start_row | Yes |
Implementation Reference
- src/excel_mcp/server.py:635-651 (handler)MCP tool handler for delete_sheet_rows, decorated with @mcp.tool() for registration and execution. Delegates to delete_rows helper.@mcp.tool() def delete_sheet_rows( filepath: str, sheet_name: str, start_row: int, count: int = 1 ) -> str: """Delete one or more rows starting at the specified row.""" try: full_path = get_excel_path(filepath) result = delete_rows(full_path, sheet_name, start_row, count) return result["message"] except (ValidationError, SheetError) as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error deleting rows: {e}") raise
- src/excel_mcp/sheet.py:421-447 (helper)Core helper function implementing row deletion using openpyxl's worksheet.delete_rows method.def delete_rows(filepath: str, sheet_name: str, start_row: int, count: int = 1) -> Dict[str, Any]: """Delete one or more rows starting at the specified row.""" 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_row < 1: raise ValidationError("Start row must be 1 or greater") if count < 1: raise ValidationError("Count must be 1 or greater") if start_row > worksheet.max_row: raise ValidationError(f"Start row {start_row} exceeds worksheet bounds (max row: {worksheet.max_row})") worksheet.delete_rows(start_row, count) wb.save(filepath) return {"message": f"Deleted {count} row(s) starting at row {start_row} in sheet '{sheet_name}'"} except (ValidationError, SheetError) as e: logger.error(str(e)) raise except Exception as e: logger.error(f"Failed to delete rows: {e}") raise SheetError(str(e))
- src/excel_mcp/server.py:635-651 (registration)Tool registration via @mcp.tool() decorator in server.py.@mcp.tool() def delete_sheet_rows( filepath: str, sheet_name: str, start_row: int, count: int = 1 ) -> str: """Delete one or more rows starting at the specified row.""" try: full_path = get_excel_path(filepath) result = delete_rows(full_path, sheet_name, start_row, count) return result["message"] except (ValidationError, SheetError) as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error deleting rows: {e}") raise