XLSXのセルに値を書き込む
set_cell(sheet_index, cell_ref, value) は、ゼロ起算のシートインデックスと標準A1形式のセル参照(A1、B12、AA42)を使ってワークシートに値を書き込みます。Office Oxideは文字列・数値・真偽値・空値を受け付けます。
ワークシートの周辺要素——書式、セル結合、条件付き書式、名前付き範囲、グラフ——はすべてそのまま保持されます。
基本的な型を書き込む
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")?;
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")
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");
C
int err = 0;
OfficeEditableHandle *wb = office_editable_open("budget.xlsx", &err);
/* value_type: OFFICE_CELL_EMPTY|STRING|NUMBER|BOOLEAN; value_str only for STRING */
office_editable_set_cell(wb, 0, "A1", OFFICE_CELL_STRING, "Total", 0.0, &err);
office_editable_set_cell(wb, 0, "B1", OFFICE_CELL_NUMBER, NULL, 42.5, &err);
office_editable_set_cell(wb, 0, "C1", OFFICE_CELL_BOOLEAN, NULL, 1.0, &err); /* nonzero = true */
office_editable_set_cell(wb, 0, "D1", OFFICE_CELL_EMPTY, NULL, 0.0, &err);
office_editable_save(wb, "budget.xlsx", &err);
office_editable_free(wb);
まとめて更新する
1回のopen/saveサイクルで多数の書き込みをまとめて行います。
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")?;
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")
C
const char *names[] = { "Acme", "Globex", "Initech" };
double revenue[] = { 120000.0, 85000.0, 62500.0 };
int active[] = { 1, 0, 1 };
int err = 0;
OfficeEditableHandle *ed = office_editable_open("dashboard.xlsx", &err);
for (int i = 0; i < 3; i++) {
char a[8], b[8], c[8];
int row = i + 2; /* leave row 1 for headers */
snprintf(a, sizeof a, "A%d", row);
snprintf(b, sizeof b, "B%d", row);
snprintf(c, sizeof c, "C%d", row);
office_editable_set_cell(ed, 0, a, OFFICE_CELL_STRING, names[i], 0.0, &err);
office_editable_set_cell(ed, 0, b, OFFICE_CELL_NUMBER, NULL, revenue[i], &err);
office_editable_set_cell(ed, 0, c, OFFICE_CELL_BOOLEAN, NULL, active[i], &err);
}
office_editable_save(ed, "dashboard.xlsx", &err);
office_editable_free(ed);
別のシートを対象にする
sheet_index はワークブック内のゼロ起算の位置であり、シート名ではありません。名前からインデックスを解決するには、先にワークブックを読み込みます。
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")
set_cell が変更するものとしないもの
set_cell はセルの <v> 値と <t> 型を書き込みますが、次のことは行いません。
- 数式の再計算。再計算をトリガーするには、Excelでファイルを開くか、計算エンジンを使用してください。
- 新しい文字列の追加を超えた共有文字列テーブルの変更。既存の文字列は共有されたまま維持されます。
- セル書式、条件付き書式、名前付き範囲の変更。
対象セルに数式が入っている場合、set_cell はその数式を静的な値で上書きします。数式を明示的に書き込むには、書式固有の xlsx::edit APIを使用してください。
エラー
| 症状 | 原因 |
|---|---|
OfficeError::Sheet(idx) (Rust) / IndexError (Python) |
sheet_index がワークブックのシート数以上 |
OfficeError::CellRef("...") |
セル参照がA1形式として無効 |
Number(value) 書き込み後にExcelでセルが空に見える |
セルが以前テキスト形式だった——Excelで書式をクリアするか、書式固有のAPIで書き込む |
関連ドキュメント
- DOCX/PPTXのテキスト置換
- 編集の概要
- openpyxlからの移行 — 同等のXLSXカバレッジ、大幅に高速