const spread = new GC.Spread.Sheets.Workbook(document.getElementById('spreadContainer'));
const sheet = spread.getActiveSheet();
spread.options.tabStripVisible = false;
// 시간대별 에너지 소비 데이터 (0~23시)
sheet.setValue(0, 0, '시간');
sheet.setValue(0, 1, '소비량(kWh)');
const hourlyData = [
12, 11, 10, 9, 11, 18, 35, 68,
85, 92, 95, 88, 90, 87, 82, 79,
83, 95, 88, 72, 55, 42, 28, 18
];
hourlyData.forEach((v, i) => {
sheet.setValue(i + 1, 0, i + ':00');
sheet.setValue(i + 1, 1, v);
});
// 헤더 스타일
const hStyle = new GC.Spread.Sheets.Style();
hStyle.backColor = '#1565C0';
hStyle.foreColor = '#ffffff';
hStyle.font = 'bold 12px sans-serif';
hStyle.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
[0, 1].forEach(c => sheet.setStyle(0, c, hStyle));
sheet.setColumnWidth(0, 60);
sheet.setColumnWidth(1, 80);
// 피크 시간대(85 kWh 초과) 빨강 조건부 서식
const peakStyle = new GC.Spread.Sheets.Style();
peakStyle.backColor = '#FFCDD2';
peakStyle.foreColor = '#B71C1C';
peakStyle.font = 'bold 11px sans-serif';
sheet.conditionalFormats.addCellValueRule(
GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.greaterThan,
84, 0, peakStyle, [new GC.Spread.Sheets.Range(1, 1, 24, 1)]
);
// Line 차트 삽입
const chart = sheet.charts.add(
'energyTrend',
GC.Spread.Sheets.Charts.ChartType.line,
155, 0, 550, 360, 'A1:B25'
);
const chartTitle = chart.title();
chartTitle.text = '시간대별 에너지 소비 현황 (kWh)';
chart.title(chartTitle);
// xlsx 내보내기
document.getElementById('btnExport')?.addEventListener('click', () => {
spread.export(function(blob) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'energy_report.xlsx';
a.click();
URL.revokeObjectURL(url);
}, function(e) {
console.log('내보내기 오류:', e);
}, {
fileType: GC.Spread.Sheets.FileType.excel
});
});