const spread = new GC.Spread.Sheets.Workbook(document.getElementById('spreadContainer'));
const sheet = spread.getActiveSheet();
spread.options.tabStripVisible = false;
sheet.options.rowHeaderVisible = false;
// 헤더 설정 (7컬럼)
const COLS = ['부품코드', '부품명', '단위', '승인수량', '현재수량', '변경일시', '변경내역'];
const WIDTHS = [90, 150, 50, 75, 75, 130, 200];
COLS.forEach((h, c) => {
sheet.setValue(0, c, h);
sheet.getCell(0, c).backColor('#37474F').foreColor('#fff').font('bold 12px sans-serif');
sheet.setColumnWidth(c, WIDTHS[c]);
});
sheet.setRowHeight(0, 36);
// 샘플 부품 데이터 — [부품코드, 부품명, 단위, 승인수량, 현재수량]
const parts = [
['P-0111', 'MCU 프로세서', 'EA', 2, 2],
['P-0112', '전원 IC', 'EA', 4, 4],
['P-0121', 'WiFi/BLE 칩셋', 'EA', 1, 1],
['P-0122', '안테나', 'EA', 2, 2],
['P-0201', '알루미늄 케이스', 'EA', 1, 1],
['P-0202', '방열 패드', 'EA', 2, 2],
];
// ① 기본 스타일을 먼저 '잠금 해제'로 설정 — 개별 셀 스타일 적용 전에 호출해야 함
const defStyle = new GC.Spread.Sheets.Style();
defStyle.locked = false;
sheet.setDefaultStyle(defStyle);
spread.suspendPaint();
sheet.setRowCount(parts.length + 1);
parts.forEach((p, i) => {
const r = i + 1;
p.forEach((v, c) => sheet.setValue(r, c, v));
// 잠긴 컬럼(col 0~3: 부품코드·부품명·단위·승인수량) — 회색으로 읽기 전용 표시
for (let c = 0; c < 4; c++) sheet.getCell(r, c).backColor('#EEEEEE').foreColor('#757575');
// 수정 가능한 컬럼(col 4: 현재수량) — 연한 노란 배경
sheet.getCell(r, 4).backColor('#FFFDE7').foreColor('#333333');
});
// ② 잠글 컬럼만 개별 지정 (col 0~3: 부품코드·부품명·단위·승인수량)
sheet.getRange(-1, 0, -1, 4).locked(true);
// ③ 시트 보호 활성화
sheet.options.isProtected = true;
sheet.options.protectionOptions = {
allowSelectLockedCells: true,
allowSelectUnlockedCells: true
};
spread.resumePaint();
// 변경 이벤트 — 현재수량(col 4) 수정 시 이력 자동 기록
// isProtected 환경에서 키보드 입력은 RangeChanged 미발화 → CellChanged 사용
sheet.bind(GC.Spread.Sheets.Events.CellChanged, function(sender, args) {
if (args.propertyName !== 'value') return;
if (args.col !== 4) return;
if (args.sheetArea !== GC.Spread.Sheets.SheetArea.viewport) return;
const r = args.row;
const partCode = sheet.getValue(r, 0);
const approvedQty = sheet.getValue(r, 3);
const newQty = sheet.getValue(r, 4);
const now = new Date().toLocaleString('ko-KR');
spread.suspendPaint();
spread.suspendEvent();
sheet.setValue(r, 5, now);
sheet.setValue(r, 6, `${partCode}: 승인(${approvedQty}) → 현재(${newQty})`);
// 변경된 행 노란색으로 강조
for (let c = 4; c < 7; c++) sheet.getCell(r, c).backColor('#FFF59D');
spread.resumeEvent();
spread.resumePaint();
});