Skip to content

Set Cells in XLSX

set_cell(sheet_index, cell_ref, value) writes a value into a worksheet by zero-based sheet index and standard A1-style cell reference (A1, B12, AA42). Office Oxide accepts strings, numbers, booleans, and empties.

The surrounding worksheet — formats, merges, conditional formatting, named ranges, charts — is preserved verbatim.

Write basic types

Python

from office_oxide import EditableDocument

with EditableDocument.open("budget.xlsx") as ed:
    ed.set_cell(0, "A1", "Total")     # string
    ed.set_cell(0, "B1", 42.5)        # number (int also accepted)
    ed.set_cell(0, "C1", True)        # boolean
    ed.set_cell(0, "D1", None)        # empty
    ed.save("budget.xlsx")

Rust

use office_oxide::edit::EditableDocument;
use office_oxide::xlsx::edit::CellValue;

let mut wb = EditableDocument::open("budget.xlsx")?;
wb.set_cell(0, "A1", CellValue::String("Total".into()))?;
wb.set_cell(0, "B1", CellValue::Number(42.5))?;
wb.set_cell(0, "C1", CellValue::Boolean(true))?;
wb.set_cell(0, "D1", CellValue::Empty)?;
wb.save("budget.xlsx")?;

JavaScript

import { EditableDocument } from 'office-oxide';

using wb = EditableDocument.open('budget.xlsx');

wb.setCell(0, 'A1', 'Total');   // string
wb.setCell(0, 'B1', 42.5);      // number
wb.setCell(0, 'C1', true);      // boolean
wb.setCell(0, 'D1', null);      // empty

wb.save('budget.xlsx');

Go

ed, _ := officeoxide.OpenEditable("budget.xlsx")
defer ed.Close()

ed.SetCell(0, "A1", officeoxide.NewStringCell("Total"))
ed.SetCell(0, "B1", officeoxide.NewNumberCell(42.5))
ed.SetCell(0, "C1", officeoxide.NewBoolCell(true))
ed.SetCell(0, "D1", officeoxide.NewEmptyCell())

ed.Save("budget.xlsx")

C#

using var wb = EditableDocument.Open("budget.xlsx");

wb.SetCell(0u, "A1", "Total");        // string overload
wb.SetCell(0u, "B1", 42.5);           // double overload
wb.SetCell(0u, "C1", true);           // bool overload
wb.SetCellEmpty(0u, "D1");            // clear a cell

wb.Save("budget.xlsx");

Bulk updates

Combine many writes in a single open/save cycle.

Python

rows = [
    ("Acme",    120_000, True),
    ("Globex",   85_000, False),
    ("Initech",  62_500, True),
]

with EditableDocument.open("dashboard.xlsx") as ed:
    for i, (name, revenue, active) in enumerate(rows):
        row = i + 2  # leave row 1 for headers
        ed.set_cell(0, f"A{row}", name)
        ed.set_cell(0, f"B{row}", revenue)
        ed.set_cell(0, f"C{row}", active)
    ed.save("dashboard.xlsx")

Rust

let rows = [
    ("Acme",    120_000.0, true),
    ("Globex",   85_000.0, false),
    ("Initech",  62_500.0, true),
];

let mut ed = EditableDocument::open("dashboard.xlsx")?;
for (i, (name, revenue, active)) in rows.iter().enumerate() {
    let row = i + 2;
    ed.set_cell(0, &format!("A{row}"), CellValue::String((*name).into()))?;
    ed.set_cell(0, &format!("B{row}"), CellValue::Number(*revenue))?;
    ed.set_cell(0, &format!("C{row}"), CellValue::Boolean(*active))?;
}
ed.save("dashboard.xlsx")?;

Targeting other sheets

sheet_index is the zero-based position in the workbook — not the sheet name. To resolve names → indices, read the workbook first:

Python

from office_oxide import Document, EditableDocument

with Document.open("budget.xlsx") as doc:
    sheet_names = [s.name() for s in doc.as_xlsx().sheets()]
print(sheet_names)   # ['Summary', 'Q1', 'Q2', 'Q3', 'Q4']

idx = sheet_names.index("Q3")
with EditableDocument.open("budget.xlsx") as ed:
    ed.set_cell(idx, "B5", 42_000)
    ed.save("budget.xlsx")

What set_cell does and doesn’t touch

set_cell writes the cell’s <v> value and <t> type; it does not:

  • Re-evaluate formulas. To trigger recalculation, open the file in Excel or use a calc engine.
  • Modify the shared-strings table beyond appending the new string. Existing strings stay shared.
  • Change cell formatting, conditional formatting, or named ranges.

If the cell currently holds a formula, set_cell overwrites the formula with a static value. Use the format-specific xlsx::edit API to write formulas explicitly.

Errors

Symptom Cause
OfficeError::Sheet(idx) (Rust) / IndexError (Python) sheet_index ≥ number of sheets in the workbook
OfficeError::CellRef("...") Cell reference isn’t valid A1 notation
Cell appears empty in Excel after Number(value) The cell was previously formatted as text — clear the format in Excel or write via the format-specific API

See also