JavaScript
const spread = new GC.Spread.Sheets.Workbook(document.getElementById('spreadContainer'));
spread.options.tabStripVisible = false;
const sheet = spread.getActiveSheet();
// 초기 샘플 예산 데이터 구성
const headers = ['예산과목', '예산액(천원)', '집행액(천원)', '잔액(천원)', '집행률(%)'];
headers.forEach((h, c) => {
sheet.setValue(0, c, h);
sheet.getCell(0, c).backColor('#1A237E').foreColor('#FFFFFF').fontWeight('bold').hAlign(GC.Spread.Sheets.HorizontalAlign.center);
sheet.setColumnWidth(c, c === 0 ? 160 : 130);
});
sheet.setRowHeight(0, 36);
const budgetRows = [
['인건비', 850000, 612000],
['물건비', 320000, 198000],
['이전지출', 1200000, 890000],
['자본지출', 680000, 245000],
['보전지출', 150000, 102000],
];
budgetRows.forEach((row, r) => {
const ri = r + 1;
sheet.setValue(ri, 0, row[0]);
sheet.setValue(ri, 1, row[1]);
sheet.setValue(ri, 2, row[2]);
sheet.setFormula(ri, 3, `=B${ri+1}-C${ri+1}`);
sheet.setFormula(ri, 4, `=ROUND(C${ri+1}/B${ri+1}*100,1)`);
[1,2,3].forEach(c => sheet.getCell(ri, c).formatter('#,##0').hAlign(GC.Spread.Sheets.HorizontalAlign.right));
sheet.getCell(ri, 4).formatter('0.0').hAlign(GC.Spread.Sheets.HorizontalAlign.center);
const pct = Math.round(row[2] / row[1] * 100);
const bg = pct >= 70 ? '#F1F8E9' : pct >= 50 ? '#FFFDE7' : '#FFF3E0';
for (let c = 0; c < 5; c++) sheet.getCell(ri, c).backColor(bg);
sheet.setRowHeight(ri, 30);
});
// 합계 행
const totalRow = budgetRows.length + 1;
sheet.setValue(totalRow, 0, '합 계');
['B','C','D'].forEach((col, i) => {
sheet.setFormula(totalRow, i + 1, `=SUM(${col}2:${col}${totalRow})`);
sheet.getCell(totalRow, i + 1).formatter('#,##0').hAlign(GC.Spread.Sheets.HorizontalAlign.right);
});
sheet.setFormula(totalRow, 4, `=ROUND(C${totalRow+1}/B${totalRow+1}*100,1)`);
sheet.getCell(totalRow, 4).formatter('0.0').hAlign(GC.Spread.Sheets.HorizontalAlign.center);
for (let c = 0; c < 5; c++) sheet.getCell(totalRow, c).backColor('#BBDEFB').fontWeight('bold');
sheet.setRowHeight(totalRow, 34);
const statusMsg = document.getElementById('statusMsg');
// Excel 가져오기
document.getElementById('btnImport')?.addEventListener('click', () => {
const file = document.getElementById('fileInput').files[0];
if (!file) { alert('.xlsx 파일을 선택하세요.'); return; }
statusMsg.textContent = '불러오는 중...';
spread.import(file, () => {
statusMsg.textContent = '✅ 가져오기 완료';
}, (e) => {
statusMsg.textContent = '오류 발생';
console.error(e);
}, { fileType: GC.Spread.Sheets.FileType.excel });
});
// Excel 내보내기
document.getElementById('btnExport')?.addEventListener('click', () => {
statusMsg.textContent = '내보내는 중...';
spread.export((blob) => {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = '예산집행현황.xlsx';
a.click();
URL.revokeObjectURL(a.href);
statusMsg.textContent = '✅ 다운로드 완료';
}, (e) => {
statusMsg.textContent = '오류 발생';
console.error(e);
}, { fileType: GC.Spread.Sheets.FileType.excel });
});