Skip to content

XLSX 셀 설정

set_cell(sheet_index, cell_ref, value)은 0 기반 시트 인덱스와 표준 A1 셀 참조(A1, B12, AA42)로 워크시트에 값을 씁니다. Office Oxide는 문자열, 숫자, 불리언, 빈 값을 받습니다.

주변 워크시트 내용 — 포맷, 병합, 조건부 서식, 이름 있는 범위, 차트 — 은 그대로 보존됩니다.

기본 타입 쓰기

Python

from office_oxide import EditableDocument

with EditableDocument.open("budget.xlsx") as ed:
    ed.set_cell(0, "A1", "Total")     # 문자열
    ed.set_cell(0, "B1", 42.5)        # 숫자 (int도 가능)
    ed.set_cell(0, "C1", True)        # 불리언
    ed.set_cell(0, "D1", None)        # 빈
    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');   // 문자열
wb.setCell(0, 'B1', 42.5);      // 숫자
wb.setCell(0, 'C1', true);      // 불리언
wb.setCell(0, 'D1', null);      // 빈

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");        // 문자열 오버로드
wb.SetCell(0u, "B1", 42.5);           // double 오버로드
wb.SetCell(0u, "C1", true);           // bool 오버로드
wb.SetCellEmpty(0u, "D1");            // 셀 비움

wb.Save("budget.xlsx");

일괄 업데이트

한 번의 open/save 사이클에서 여러 쓰기를 결합하세요.

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  # 1행은 헤더용
        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")?;

다른 시트 대상으로

sheet_index는 워크북 내의 0 기반 위치이지 시트 이름이 아닙니다. 이름 → 인덱스를 풀려면 먼저 워크북을 읽으세요:

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에서 파일을 열거나 calc 엔진을 사용하세요.
  • 새 문자열 추가를 넘어서 shared-strings 테이블을 수정. 기존 문자열은 계속 공유됩니다.
  • 셀 서식, 조건부 서식, 이름 있는 범위를 변경하지 않습니다.

대상 셀이 현재 수식을 갖고 있다면 set_cell이 수식을 정적 값으로 덮어씁니다. 수식을 명시적으로 쓰려면 형식별 xlsx::edit API를 쓰세요.

오류

증상 원인
OfficeError::Sheet(idx)(Rust) / IndexError(Python) sheet_index가 워크북의 시트 수 이상
OfficeError::CellRef("...") 셀 참조가 유효한 A1 표기가 아님
Number(value) 후 Excel에서 셀이 비어 보임 이전에 해당 셀이 텍스트로 포맷됨 — Excel에서 서식을 지우거나 형식별 API로 쓰기

더 보기