Skip to content

Commit 1868876

Browse files
committed
2 parents 013940f + b0a21eb commit 1868876

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

src/index.js

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ async function main() {
274274
console.log('-------------------------------------------------------------------------------');
275275
const workbook = new ExcelJS.Workbook();
276276
const createdSheetNames = [];
277+
const createdSheetCounts = [];
277278

278279
// 목차 시트를 맨 처음에 생성 (내용은 나중에 채움)
279280
let tocSheet = null;
@@ -308,6 +309,7 @@ async function main() {
308309
tabName: actualSheetName,
309310
recordCount: recordCount
310311
});
312+
createdSheetCounts.push(recordCount);
311313

312314
// 시트명이 잘렸는지 확인하고 로그 출력
313315
if (sheetName !== actualSheetName) {
@@ -327,29 +329,58 @@ async function main() {
327329
}
328330
}
329331

330-
// 목차 시트 내용 채우기 (시트는 이미 맨 처음에 생성됨)
331-
if (createdSheetNames.length > 0 && tocSheet) {
332-
excelStyleHelper.populateTableOfContents(tocSheet, createdSheetNames);
333-
console.log(`[목차] 시트 내용 생성 완료 (맨 왼쪽 위치)`);
332+
// 목차 시트 추가
333+
if (createdSheetNames.length > 0) {
334+
const tocSheet = workbook.addWorksheet('목차');
335+
tocSheet.addRow(['No', 'Sheet Name', 'Data Count']);
336+
createdSheetNames.forEach((obj, idx) => {
337+
const row = tocSheet.addRow([idx + 1, obj.displayName, createdSheetCounts[idx]]);
338+
// 시트명에 하이퍼링크 추가 (실제 탭 이름 기준)
339+
row.getCell(2).value = {
340+
text: obj.displayName,
341+
hyperlink: `#'${obj.tabName}'!A1`
342+
};
343+
row.getCell(2).font = { color: { argb: '0563C1' }, underline: true };
344+
// 데이터 건수에도 하이퍼링크 추가
345+
row.getCell(3).value = {
346+
text: createdSheetCounts[idx].toString(),
347+
hyperlink: `#'${obj.tabName}'!A1`
348+
};
349+
row.getCell(3).font = { color: { argb: '0563C1' }, underline: true };
350+
});
351+
// 목차 시트를 첫 번째로 이동
352+
workbook.worksheets = [tocSheet, ...workbook.worksheets.filter(ws => ws.name !== '목차')];
353+
// 간단한 스타일
354+
tocSheet.getRow(1).font = { bold: true };
355+
tocSheet.columns = [
356+
{ header: 'No', key: 'no', width: 6 },
357+
{ header: 'Sheet Name', key: 'name', width: 30 },
358+
{ header: 'Data Count', key: 'count', width: 12 }
359+
];
334360

335-
// 별도 목차 엑셀 파일 생성 (설정에 따라 조건부 생성)
336-
if (createSeparateToc) {
337-
const tocWb = new ExcelJS.Workbook();
338-
excelStyleHelper.createExternalTableOfContents(tocWb, createdSheetNames, outFile);
339-
340-
// 파일명: 기존 outFile 기준 _목차_yyyymmddhhmmss.xlsx
341-
const tocExt = path.extname(outFile);
342-
const tocBase = outFile.slice(0, -tocExt.length);
343-
const tocFile = `${tocBase}_목차_${getNowTimestampStr()}${tocExt}`;
344-
await tocWb.xlsx.writeFile(tocFile);
345-
console.log(`[목차] 별도 엑셀 파일 생성: ${tocFile}`);
346-
} else {
347-
console.log(`[목차] 별도 파일 생성 안함 (separateToc=false)`);
348-
}
361+
// 별도 목차 엑셀 파일 생성
362+
const tocWb = new ExcelJS.Workbook();
363+
const tocOnly = tocWb.addWorksheet('목차');
364+
tocOnly.addRow(['No', 'Sheet Name', 'Data Count']);
365+
createdSheetNames.forEach((obj, idx) => {
366+
const row = tocOnly.addRow([idx + 1, obj.displayName, createdSheetCounts[idx]]);
367+
row.getCell(2).font = { color: { argb: '0563C1' }, underline: true };
368+
row.getCell(3).font = { color: { argb: '0563C1' }, underline: true };
369+
});
370+
tocOnly.getRow(1).font = { bold: true };
371+
tocOnly.columns = [
372+
{ header: 'No', key: 'no', width: 6 },
373+
{ header: 'Sheet Name', key: 'name', width: 30 },
374+
{ header: 'Data Count', key: 'count', width: 12 }
375+
];
376+
const tocExt = path.extname(outFile);
377+
const tocBase = outFile.slice(0, -tocExt.length);
378+
const tocFile = `${tocBase}_목차_${getNowTimestampStr()}${tocExt}`;
379+
await tocWb.xlsx.writeFile(tocFile);
380+
console.log(`[목차] 별도 엑셀 파일 생성: ${tocFile}`);
349381
}
350382
console.log(`\nGenerating excel file ... `);
351383
console.log(`Wating a few seconds ... `);
352-
353384
await workbook.xlsx.writeFile(outFile);
354385
console.log(`\n\n[${outFile}] Excel file created `);
355386
console.log('-------------------------------------------------------------------------------\n\n');
@@ -358,4 +389,4 @@ async function main() {
358389

359390
if (require.main === module) {
360391
main().catch(err => { console.error(err); process.exit(1); });
361-
}
392+
}

0 commit comments

Comments
 (0)