const spread = new GC.Spread.Sheets.Workbook(document.getElementById('spreadContainer'));
const sheet = spread.getActiveSheet();
spread.options.tabStripVisible = false;
// 1단계: 헤더 설정
const headers = ['제품', 'Q1 매출', 'Q2 매출', 'Q3 매출', 'Q4 매출', '연간 합계', '분기 평균', '최고 분기'];
headers.forEach((h, c) => {
sheet.setValue(0, c, h);
sheet.getCell(0, c)
.backColor('#1565C0').foreColor('#ffffff')
.font('bold 12px sans-serif')
.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
sheet.setColumnWidth(c, [90, 80, 80, 80, 80, 90, 85, 85][c]);
});
sheet.setRowHeight(0, 32);
// 2단계: 제품별 분기 매출 데이터 입력
const products = ['SpreadJS', 'DS', 'Wijmo', 'ActiveReports'];
const salesData = [
[4200, 3800, 5100, 6300],
[2900, 3200, 3500, 4100],
[1800, 2100, 2400, 2800],
[3500, 3900, 4200, 5000],
];
salesData.forEach((row, r) => {
sheet.setValue(r + 1, 0, products[r]);
sheet.getCell(r + 1, 0).font('bold 11px sans-serif').foreColor('#37474F');
row.forEach((val, c) => {
sheet.setValue(r + 1, c + 1, val);
});
sheet.setRowHeight(r + 1, 28);
});
// 3단계: Excel 수식으로 합계·평균·최고값 자동 계산
// Handsontable에서는 이 수식들을 기본 지원하지 않아 JS로 직접 계산해야 함
for (let r = 0; r < 4; r++) {
const row = r + 1;
const range = `B${row + 1}:E${row + 1}`;
sheet.setFormula(row, 5, `=SUM(${range})`);
sheet.setFormula(row, 6, `=AVERAGE(${range})`);
sheet.setFormula(row, 7, `=MAX(${range})`);
}
// 4단계: 합계 행
sheet.setValue(5, 0, '분기 합계');
sheet.getCell(5, 0).font('bold 12px sans-serif').foreColor('#0D47A1');
['B', 'C', 'D', 'E'].forEach((col, c) => {
sheet.setFormula(5, c + 1, `=SUM(${col}2:${col}5)`);
sheet.getCell(5, c + 1).backColor('#E3F2FD').font('bold 11px sans-serif');
});
sheet.setFormula(5, 5, '=SUM(F2:F5)');
sheet.getCell(5, 5).backColor('#E3F2FD').font('bold 11px sans-serif');
sheet.setRowHeight(5, 30);
// 5단계: 숫자 형식 적용
sheet.getRange(1, 1, 5, 7).formatter('#,##0 "만원"');
// 6단계: 차트 추가 (묶음 세로 막대)
const chart = sheet.charts.add(
'salesChart',
GC.Spread.Sheets.Charts.ChartType.columnClustered,
10, 195, 480, 190,
'A1:E5'
);
chart.title({ text: '제품별 분기 매출 현황' });
chart.legend({ position: GC.Spread.Sheets.Charts.LegendPosition.bottom });
// 7단계: Excel 내보내기 버튼
// spread.save() — SpreadJS v19 Workbook 내장 Excel 내보내기
// (gc.spread.sheets.io 플러그인 로드 시 활성화, GC.Spread.Excel.IO 불필요)
document.getElementById('exportBtn')?.addEventListener('click', function() {
const btn = this;
btn.disabled = true;
btn.textContent = '변환 중...';
spread.save(
function(blob) {
const reader = new FileReader();
reader.onload = function() {
const fileSizeKB = Math.round(blob.size / 1024);
const a = document.createElement('a');
a.href = reader.result;
a.download = 'SpreadJS_분기별_매출분석.xlsx';
a.style.cssText = 'display:inline-flex;align-items:center;gap:6px;padding:7px 18px;background:#2E7D32;color:#fff;border-radius:4px;text-decoration:none;font-size:13px;font-weight:bold;';
a.innerHTML = '📥 Excel 다운로드 (' + fileSizeKB + ' KB)';
document.getElementById('exportStatus').appendChild(a);
a.click();
};
reader.readAsDataURL(blob);
btn.style.display = 'none';
},
function(err) {
btn.disabled = false;
btn.textContent = 'Excel 파일로 내보내기';
console.error('Export error:', err);
}
);
});