const spread = new GC.Spread.Sheets.Workbook(document.getElementById('spreadContainer'));
const MONTHS = ['1월', '2월', '3월'];
const COLORS = ['#1565C0', '#00695C', '#6A1B9A'];
const SALES = [5200000, 6800000, 7400000];
const COSTS = [3100000, 3900000, 4200000];
// 월별 시트 생성
MONTHS.forEach((name, idx) => {
const sheet = idx === 0 ? spread.getActiveSheet() : new GC.Spread.Sheets.Worksheet(name);
if (idx > 0) spread.addSheet(idx, sheet);
sheet.name(name);
['구분', '금액(원)'].forEach((h, c) => {
sheet.setValue(0, c, h);
sheet.getCell(0, c).backColor(COLORS[idx]).foreColor('#fff').font('bold 12px sans-serif');
});
[['매출', SALES[idx]], ['비용', COSTS[idx]], ['이익', SALES[idx] - COSTS[idx]]].forEach(([label, val], r) => {
sheet.setValue(r + 1, 0, label);
sheet.setValue(r + 1, 1, val);
});
sheet.setColumnWidth(0, 80); sheet.setColumnWidth(1, 130);
});
// 분기합계 시트: 시트 간 수식으로 자동 집계
const totalSheet = new GC.Spread.Sheets.Worksheet('분기합계');
spread.addSheet(3, totalSheet);
['월', '매출', '비용', '이익'].forEach((h, c) => {
totalSheet.setValue(0, c, h);
totalSheet.getCell(0, c).backColor('#37474F').foreColor('#fff').font('bold 12px sans-serif');
totalSheet.setColumnWidth(c, c === 0 ? 70 : 130);
});
totalSheet.setRowHeight(0, 32);
// 시트 간 수식 참조
MONTHS.forEach((name, r) => {
totalSheet.setValue(r + 1, 0, name);
totalSheet.setFormula(r + 1, 1, `=${name}!B2`); // 매출 참조
totalSheet.setFormula(r + 1, 2, `=${name}!B3`); // 비용 참조
totalSheet.setFormula(r + 1, 3, `=${name}!B4`); // 이익 참조
if (r % 2 === 0) [0,1,2,3].forEach(c => totalSheet.getCell(r+1,c).backColor('#FAFAFA'));
});
// 합계 행
const totalRow = MONTHS.length + 1;
totalSheet.setValue(totalRow, 0, '분기합계');
totalSheet.getCell(totalRow, 0).font('bold 13px sans-serif');
['B', 'C', 'D'].forEach((col, c) => {
totalSheet.setFormula(totalRow, c + 1, `=SUM(${col}2:${col}${totalRow})`);
totalSheet.getCell(totalRow, c + 1).backColor('#E8F5E9').font('bold 13px sans-serif');
});
spread.setActiveSheetIndex(3);