Skip to content

Commit b0a21eb

Browse files
committed
목차 시트 건수에 링크 적용
1 parent b90f7ba commit b0a21eb

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) {
@@ -326,29 +328,58 @@ async function main() {
326328
}
327329
}
328330

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

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

358389
if (require.main === module) {
359390
main().catch(err => { console.error(err); process.exit(1); });
360-
}
391+
}

0 commit comments

Comments
 (0)