insert_rows
Add new rows to a spreadsheet at a specified position, shifting existing rows downward. Optionally populate inserted rows with data values or leave them blank for manual entry.
Instructions
Insert rows at a given position, shifting existing rows down.
If data is provided, the inserted rows are filled with those values (type-coerced). Otherwise the rows are left blank.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | Path to the spreadsheet file | |
| row | Yes | 1-based row index where new rows will be inserted. Existing rows at and below this index shift down. | |
| count | No | Number of rows to insert. If data is provided and longer, enough rows are inserted to fit the data. | |
| data | No | Optional 2D array of values to fill the inserted rows. Leave empty for blank rows. | |
| sheet | No | Sheet name. Defaults to the first sheet if omitted. |
Implementation Reference
- The `insert_rows` tool implementation which delegates to the backend workbook's `insert_rows` method and optionally fills the inserted rows with data.
def insert_rows( file: Annotated[str, Field(description="Path to the spreadsheet file")], row: Annotated[int, Field(description="1-based row index where new rows will be inserted. Existing rows at and below this index shift down.")], count: Annotated[int, Field(description="Number of rows to insert. If data is provided and longer, enough rows are inserted to fit the data.")] = 1, data: Annotated[list[list] | None, Field(description="Optional 2D array of values to fill the inserted rows. Leave empty for blank rows.")] = None, sheet: Annotated[str | None, Field(description="Sheet name. Defaults to the first sheet if omitted.")] = None, ) -> str: """Insert rows at a given position, shifting existing rows down. If data is provided, the inserted rows are filled with those values (type-coerced). Otherwise the rows are left blank. """ wb = load_workbook(file) ws = _resolve_sheet(wb, sheet) actual_count = max(count, len(data) if data else 0) ws.insert_rows(row, actual_count) if data: for r_idx, row_data in enumerate(data): for c_idx, val in enumerate(row_data): ws.set_cell(row + r_idx, c_idx + 1, coerce_value(val)) wb.save(file) return f"Inserted {actual_count} rows at row {row}"