const spread = new GC.Spread.Sheets.Workbook(document.getElementById('spreadContainer'));
const sheet = spread.getActiveSheet();
spread.options.tabStripVisible = false;
const headers = ['월', '제품', '매출(만원)', '전월 대비(%)'];
headers.forEach((h, c) => {
sheet.setValue(0, c, h);
sheet.getCell(0, c).backColor('#1565C0').foreColor('#fff').font('bold 12px sans-serif');
});
const rows = [
['1월', 'SpreadJS', 1250, 12.5],
['2월', 'SpreadJS', 980, -21.6],
['3월', 'SpreadJS', 1540, 57.1],
['4월', 'SpreadJS', 320, -79.2],
['5월', 'SpreadJS', 1680, 425.0],
];
rows.forEach((row, r) => {
row.forEach((val, c) => sheet.setValue(r + 1, c, val));
sheet.setRowHeight(r + 1, 28);
});
[60, 80, 110, 110].forEach((w, c) => sheet.setColumnWidth(c, w));
sheet.setRowHeight(0, 32);
function extractRangeToJson(sh, row, col, rowCount, colCount) {
// getArray()로 헤더 행과 선택 범위 값을 2차원 배열로 한 번에 가져옴
const headerRow = sh.getArray(0, col, 1, colCount)[0].map((h, i) => h || `열${col + i + 1}`);
const startRow = Math.max(1, row);
const count = row + rowCount - startRow;
const values = count > 0 ? sh.getArray(startRow, col, count, colCount) : [];
const data = [];
values.forEach(rowValues => {
const obj = {};
rowValues.forEach((v, i) => {
if (v !== null && v !== undefined && v !== '') obj[headerRow[i]] = v;
});
if (Object.keys(obj).length > 0) data.push(obj);
});
return data;
}
document.getElementById('btnExtract')?.addEventListener('click', () => {
const lblStatus = document.getElementById('lblStatus');
const preOutput = document.getElementById('preOutput');
const sel = sheet.getSelections();
if (!sel || !sel.length) {
if (lblStatus) lblStatus.textContent = '먼저 셀 범위를 선택하세요.';
return;
}
const { row, col, rowCount, colCount } = sel[0];
const jsonData = extractRangeToJson(sheet, row, col, rowCount, colCount);
const payload = {
model: 'gpt-4o',
messages: [
{ role: 'system', content: '판매 데이터를 분석하고 주요 트렌드를 한국어로 요약해주세요.' },
{ role: 'user', content: JSON.stringify(jsonData) }
]
};
if (preOutput) preOutput.textContent = JSON.stringify(payload, null, 2);
if (lblStatus) lblStatus.textContent = `${jsonData.length}행 추출 완료 — API 전송 준비됨`;
});