write_range
Write a 2D array of values into a spreadsheet region starting from a specified cell, replacing multiple individual cell writes with a single efficient operation.
Instructions
Write a 2D array of values into a rectangular region.
Writing starts at start_cell and expands right and down to fit the data. Prefer this over multiple write_cell calls for efficiency.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | Path to the spreadsheet file | |
| start_cell | Yes | Top-left cell where writing begins, e.g. 'B2' | |
| data | Yes | 2D array of values (list of rows), e.g. [[1, 2, 3], ['a', 'b', 'c']]. Numeric strings are coerced to numbers. | |
| sheet | No | Sheet name. Defaults to the first sheet if omitted. |
Implementation Reference
- Implementation of the write_range MCP tool which writes a 2D array of values into a spreadsheet at a specified starting cell.
def write_range( file: Annotated[str, Field(description="Path to the spreadsheet file")], start_cell: Annotated[str, Field(description="Top-left cell where writing begins, e.g. 'B2'")], data: Annotated[list[list], Field(description="2D array of values (list of rows), e.g. [[1, 2, 3], ['a', 'b', 'c']]. Numeric strings are coerced to numbers.")], sheet: Annotated[str | None, Field(description="Sheet name. Defaults to the first sheet if omitted.")] = None, ) -> str: """Write a 2D array of values into a rectangular region. Writing starts at start_cell and expands right and down to fit the data. Prefer this over multiple write_cell calls for efficiency. """ wb = load_workbook(file) ws = _resolve_sheet(wb, sheet) row_off, col_off = parse_cell(start_cell) for r_idx, row in enumerate(data): for c_idx, val in enumerate(row): ws.set_cell(row_off + r_idx, col_off + c_idx, coerce_value(val)) wb.save(file) return f"Wrote {len(data)} rows starting at {start_cell}"