Zellen in XLSX setzen
set_cell(sheet_index, cell_ref, value) schreibt einen Wert in ein Worksheet über einen nullbasierten Sheet-Index und eine Standard-A1-Zellreferenz (A1, B12, AA42). Office Oxide akzeptiert Strings, Zahlen, Booleans und leere Werte.
Das umliegende Worksheet — Formate, Merges, bedingte Formatierung, benannte Bereiche, Diagramme — bleibt unangetastet.
Basistypen schreiben
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) # Zahl (int klappt auch)
ed.set_cell(0, "C1", True) # Boolean
ed.set_cell(0, "D1", None) # leer
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); // Zahl
wb.setCell(0, 'C1', true); // Boolean
wb.setCell(0, 'D1', null); // leer
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"); // Zelle leeren
wb.Save("budget.xlsx");
Bulk-Updates
Kombiniere viele Schreibzugriffe in einem Open/Save-Zyklus.
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 # Zeile 1 bleibt für Header
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")?;
Andere Sheets adressieren
sheet_index ist die nullbasierte Position in der Arbeitsmappe — kein Sheet-Name. Um Namen → Index aufzulösen, lies zuerst die Arbeitsmappe:
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")
Was set_cell anfasst — und was nicht
set_cell schreibt den <v>-Wert und den <t>-Typ der Zelle; es macht nicht:
- Formeln neu berechnen. Zum Neuberechnen die Datei in Excel öffnen oder eine Calc-Engine einsetzen.
- Die Shared-Strings-Tabelle über das Anhängen des neuen Strings hinaus verändern. Bestehende Strings bleiben geshared.
- Zellformatierung, bedingte Formatierung oder benannte Bereiche ändern.
Enthält die Zielzelle aktuell eine Formel, überschreibt set_cell die Formel mit dem statischen Wert. Für explizite Formel-Schreibzugriffe das formatspezifische xlsx::edit-API nehmen.
Fehler
| Symptom | Ursache |
|---|---|
OfficeError::Sheet(idx) (Rust) / IndexError (Python) |
sheet_index ≥ Sheet-Anzahl der Arbeitsmappe |
OfficeError::CellRef("...") |
Zellreferenz ist keine gültige A1-Notation |
Nach Number(value) wirkt die Zelle in Excel leer |
Die Zelle war vorher als Text formatiert — Format in Excel leeren oder über das formatspezifische API schreiben |
Siehe auch
- Text in DOCX/PPTX ersetzen
- Editieren — Überblick
- Migration von openpyxl — gleiche XLSX-Abdeckung, deutlich schneller