insert_rows
Add one or more rows to an Excel sheet starting at a specified row, enabling efficient data insertion and organization within workbooks.
Instructions
Insert 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:599-616 (handler)MCP tool handler for 'insert_rows' decorated with @mcp.tool(). Handles input validation via type hints, calls helper function, and returns result or error message.@mcp.tool() def insert_rows( filepath: str, sheet_name: str, start_row: int, count: int = 1 ) -> str: """Insert one or more rows starting at the specified row.""" try: full_path = get_excel_path(filepath) result = insert_row(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 inserting rows: {e}") raise
- src/excel_mcp/sheet.py:369-394 (helper)Core helper function implementing row insertion using openpyxl. Loads workbook, validates sheet and parameters, inserts rows via worksheet.insert_rows(), saves file, and returns success message.def insert_row(filepath: str, sheet_name: str, start_row: int, count: int = 1) -> Dict[str, Any]: """Insert 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") worksheet.insert_rows(start_row, count) wb.save(filepath) return {"message": f"Inserted {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 insert rows: {e}") raise SheetError(str(e))