const spread = new GC.Spread.Sheets.Workbook(document.getElementById('spreadContainer'));
const sheet = spread.getActiveSheet();
sheet.name('Q1_판매보고서');
sheet.options.sheetTabColor = '#1565C0';
const headers = ['항목', '예산(만원)', '실적(만원)', '달성률(%)'];
headers.forEach((h, c) => {
sheet.setValue(0, c, h);
sheet.getCell(0, c)
.backColor('#1565C0').foreColor('#fff')
.font('bold 13px sans-serif')
.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
});
sheet.setRowHeight(0, 36);
const rows = [
['제품 A', 5000, 5200, 104.0],
['제품 B', 3000, 2750, 91.7],
['제품 C', 4500, 4500, 100.0],
['제품 D', 2000, 1820, 91.0],
['합계', 14500, 14270, 98.4],
];
rows.forEach((row, r) => {
const isTotal = r === rows.length - 1;
row.forEach((val, c) => {
sheet.setValue(r + 1, c, val);
if (isTotal) sheet.getCell(r + 1, c).backColor('#F5F5F5').font('bold 13px sans-serif');
});
if (!isTotal) {
const rateStyle = new GC.Spread.Sheets.Style();
rateStyle.foreColor = row[3] >= 100 ? '#2E7D32' : '#B71C1C';
rateStyle.font = 'bold 12px sans-serif';
sheet.setStyle(r + 1, 3, rateStyle);
}
});
[120, 110, 110, 110].forEach((w, c) => sheet.setColumnWidth(c, w));
document.getElementById('btnExport')?.addEventListener('click', () => {
const msg = document.getElementById('exportMsg');
if (msg) msg.textContent = '생성 중...';
spread.export(
(blob) => {
// CodePen iframe sandbox는 blob: URL 다운로드를 차단하므로 data: URI로 변환
const reader = new FileReader();
reader.onload = () => {
const a = document.createElement('a');
a.href = reader.result;
a.download = 'Q1_판매보고서.xlsx';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
if (msg) msg.textContent = `✔ xlsx 다운로드 완료 (${Math.round(blob.size / 1024)}KB) — 색상·서식·시트명이 Excel에서도 동일하게 표시됩니다`;
};
reader.readAsDataURL(blob);
},
(e) => { if (msg) msg.textContent = '오류: ' + (e.errorMessage || String(e)); },
{ fileType: GC.Spread.Sheets.FileType.excel }
);
});