const spread = new GC.Spread.Sheets.Workbook(document.getElementById('spreadContainer'));
spread.options.tabStripVisible = false;
const sheet = spread.getActiveSheet();
const data = [
{ id: 1, product: '노트북 A', stock: 45, price: 1250000 },
{ id: 2, product: '마우스 B', stock: 200, price: 32000 },
{ id: 3, product: '키보드 C', stock: 80, price: 78000 },
{ id: 4, product: '모니터 D', stock: 30, price: 520000 },
{ id: 5, product: '헤드셋 E', stock: 60, price: 145000 },
];
const colNames = ['ID', '제품명', '재고', '단가(원)'];
colNames.forEach((h, c) => {
sheet.setValue(0, c, h);
sheet.getCell(0, c).backColor('#1565C0').foreColor('#fff').font('bold 12px sans-serif');
sheet.setColumnWidth(c, c === 1 ? 120 : 90);
});
sheet.setRowHeight(0, 34);
data.forEach((row, r) => {
sheet.setValue(r + 1, 0, row.id);
sheet.setValue(r + 1, 1, row.product);
sheet.setValue(r + 1, 2, row.stock);
sheet.setValue(r + 1, 3, row.price);
sheet.getCell(r + 1, 3).formatter('₩#,##0');
sheet.setRowHeight(r + 1, 30);
});
// ID 열 편집 잠금
sheet.options.isProtected = true;
const defStyle = sheet.getDefaultStyle();
defStyle.locked = false;
sheet.setDefaultStyle(defStyle);
sheet.getRange(-1, 0, -1, 1).locked(true);
const logEl = document.getElementById('syncLog');
// CellChanged 이벤트 — 편집 완료 시 서버 동기화 트리거
sheet.bind(GC.Spread.Sheets.Events.CellChanged, function (e, info) {
if (info.propertyName !== 'value' || info.row < 1) return;
const rowData = data[info.row - 1];
const newVal = sheet.getValue(info.row, info.col);
const timestamp = new Date().toLocaleTimeString();
// 변경 셀 dirty 표시
sheet.getCell(info.row, info.col).backColor('#FFF9C4');
logEl.innerHTML =
`[${timestamp}] 🔄 서버 동기화: ID=${rowData.id}, ${colNames[info.col]}=${newVal}<br>` +
logEl.innerHTML;
// 실제 환경:
// fetch(`/api/products/${rowData.id}`, {
// method: 'PUT',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify({ field: colNames[info.col], value: newVal })
// });
});