Skip to content

Commit c86d83d

Browse files
committed
Added a feature to allow the application of DB name and current date and time to Excel file names.
1 parent 831701f commit c86d83d

File tree

6 files changed

+88
-21
lines changed

6 files changed

+88
-21
lines changed

queries/queries-with-dynamic-variables.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<queries>
2-
<excel db="sampleDB" output="d:/temp/dynamic_variables_test_2024.log" maxRows="20" style="modern" aggregateInfoTemplate="Related data {count} records {details}">
2+
<excel db="sampleDB" output="d:/temp/dynamic_variables_test_${DATE:yyyy-MM-dd}.csv" maxRows="20" style="modern" aggregateInfoTemplate="Related data {count} records {details}">
33
<!-- Style template ID (default: default) -->
44
<!-- Available templates: default, modern, dark, colorful, minimal, business, premium -->
55
<!-- Can be overridden with individual style attributes if needed -->

queries/queries-with-template.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<queries>
2-
<excel db="sampleDB" output="d:/temp/sales_summary_template_2024.xlsx" maxRows="20" style="business">
2+
<excel db="sampleDB" output="d:/temp/sales_summary_template_${DB_NAME}_${DATE:yyyyMMddhhmmss}.xlsx" maxRows="20" style="business">
33
<!-- Global style template ID (default: default) -->
44
<!-- Individual styles can be specified per sheet -->
55
<!-- Available templates: default, modern, dark, colorful, minimal, business, premium -->

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,19 @@ async function main() {
247247
}
248248

249249
// 엑셀 파일 경로 결정 (CLI > excel > 쿼리파일 > 기본값)
250+
// DB 이름 변수를 합성하여 파일명에 사용할 수 있도록 주입
251+
const mergedOutVars = { ...mergedVars, DB_NAME: defaultDbKey };
250252
let outFile = argv.out || excelOutput || outputPath || 'output.xlsx';
253+
// 사용자 지정 형식 $(VAR} → 표준 ${VAR} 로 정규화 (예: $(DB_NAME} 지원)
254+
outFile = outFile.replace(/\$\((\w+)\}/g, '${$1}');
255+
outFile = variableProcessor.substituteVars(outFile, mergedOutVars);
251256
outFile = FileUtils.resolvePath(outFile);
252-
// 파일명에 _yyyymmddhhmmss 추가
253-
outFile = excelGenerator.generateOutputPath(outFile, FileUtils.getNowTimestampStr());
254257
FileUtils.ensureDirExists(outFile);
255258

259+
256260
const createdSheetNames = [];
257261
const createdSheetCounts = [];
258262
const processedSheets = [];
259-
260-
// 시트 처리
261263
let sheetIndex = 0;
262264
for (const sheetDef of sheets) {
263265
// robust use 속성 체크

src/mssql-helper.js

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,94 @@ class MSSQLHelper {
107107
formatDate(date, format) {
108108
// UTC 기준으로 날짜/시간 추출
109109
const map = {
110-
'YYYY': date.getUTCFullYear(),
110+
// Year
111+
'YYYY': String(date.getUTCFullYear()),
112+
'yyyy': String(date.getUTCFullYear()),
111113
'YY': String(date.getUTCFullYear()).slice(-2),
114+
'yy': String(date.getUTCFullYear()).slice(-2),
115+
// Month
112116
'MM': String(date.getUTCMonth() + 1).padStart(2, '0'),
113-
'M': date.getUTCMonth() + 1,
117+
'M': String(date.getUTCMonth() + 1),
118+
// Day
114119
'DD': String(date.getUTCDate()).padStart(2, '0'),
115-
'D': date.getUTCDate(),
120+
'dd': String(date.getUTCDate()).padStart(2, '0'),
121+
'D': String(date.getUTCDate()),
122+
'd': String(date.getUTCDate()),
123+
// Hour
116124
'HH': String(date.getUTCHours()).padStart(2, '0'),
117-
'H': date.getUTCHours(),
125+
'hh': String(date.getUTCHours()).padStart(2, '0'),
126+
'H': String(date.getUTCHours()),
127+
'h': String(date.getUTCHours()),
128+
// Minute
118129
'mm': String(date.getUTCMinutes()).padStart(2, '0'),
119-
'm': date.getUTCMinutes(),
130+
'm': String(date.getUTCMinutes()),
131+
// Second
120132
'ss': String(date.getUTCSeconds()).padStart(2, '0'),
121-
's': date.getUTCSeconds(),
122-
'SSS': String(date.getUTCMilliseconds()).padStart(3, '0')
133+
's': String(date.getUTCSeconds()),
134+
// Milliseconds
135+
'SSS': String(date.getUTCMilliseconds()).padStart(3, '0'),
136+
'sss': String(date.getUTCMilliseconds()).padStart(3, '0')
123137
};
124138

125139
let result = format;
126-
// 긴 패턴부터 먼저 치환 (YYYY를 YY보다 먼저)
127-
['YYYY', 'MM', 'DD', 'HH', 'mm', 'ss', 'SSS', 'YY', 'M', 'D', 'H', 'm', 's'].forEach(token => {
140+
// 긴 패턴부터 먼저 치환해 부분 중복을 방지
141+
const tokensInOrder = [
142+
'YYYY', 'yyyy', 'SSS', 'sss',
143+
'HH', 'hh', 'MM', 'DD', 'dd', 'YY', 'yy',
144+
'H', 'h', 'M', 'D', 'd', 'mm', 'ss', 'm', 's'
145+
];
146+
tokensInOrder.forEach(token => {
147+
result = result.replace(new RegExp(token, 'g'), map[token]);
148+
});
149+
return result;
150+
}
151+
152+
/**
153+
* 날짜 포맷팅 함수 (로컬 시간 기준)
154+
* @param {Date} date - Date 객체
155+
* @param {string} format - 포맷 문자열 (yyyy, MM, dd, HH, mm, ss 등)
156+
* @returns {string} 포맷팅된 날짜 문자열
157+
*/
158+
formatDateLocal(date, format) {
159+
const map = {
160+
// Year
161+
'YYYY': String(date.getFullYear()),
162+
'yyyy': String(date.getFullYear()),
163+
'YY': String(date.getFullYear()).slice(-2),
164+
'yy': String(date.getFullYear()).slice(-2),
165+
// Month
166+
'MM': String(date.getMonth() + 1).padStart(2, '0'),
167+
'M': String(date.getMonth() + 1),
168+
// Day
169+
'DD': String(date.getDate()).padStart(2, '0'),
170+
'dd': String(date.getDate()).padStart(2, '0'),
171+
'D': String(date.getDate()),
172+
'd': String(date.getDate()),
173+
// Hour
174+
'HH': String(date.getHours()).padStart(2, '0'),
175+
'hh': String(date.getHours()).padStart(2, '0'),
176+
'H': String(date.getHours()),
177+
'h': String(date.getHours()),
178+
// Minute
179+
'mm': String(date.getMinutes()).padStart(2, '0'),
180+
'm': String(date.getMinutes()),
181+
// Second
182+
'ss': String(date.getSeconds()).padStart(2, '0'),
183+
's': String(date.getSeconds()),
184+
// Milliseconds
185+
'SSS': String(date.getMilliseconds()).padStart(3, '0'),
186+
'sss': String(date.getMilliseconds()).padStart(3, '0')
187+
};
188+
189+
let result = format;
190+
const tokensInOrder = [
191+
'YYYY', 'yyyy', 'SSS', 'sss',
192+
'HH', 'hh', 'MM', 'DD', 'dd', 'YY', 'yy',
193+
'H', 'h', 'M', 'D', 'd', 'mm', 'ss', 'm', 's'
194+
];
195+
tokensInOrder.forEach(token => {
128196
result = result.replace(new RegExp(token, 'g'), map[token]);
129197
});
130-
131198
return result;
132199
}
133200

src/variable-processor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class VariableProcessor {
331331
try {
332332
// 로컬 시간 사용 (시스템 타임존)
333333
const now = new Date();
334-
const formattedDate = this.mssqlHelper.formatDate(now, format);
334+
const formattedDate = this.mssqlHelper.formatDateLocal(now, format);
335335
result = result.replace(fullMatch, formattedDate);
336336

337337
if (debugVariables) {

templates/excel-styles.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
<!-- default style -->
44
<style id="default" name="default style" description="default style">
55
<header>
6-
<font name="Arial" size="12" color="FFFFFF" bold="true"/>
6+
<font name="Arial" size="11" color="FFFFFF" bold="true"/>
77
<fill color="4F81BD"/>
8-
<colwidths min="20" max="50"/>
98
<alignment horizontal="center" vertical="middle"/>
109
<border>
1110
<all style="thin" color="000000"/>
1211
</border>
1312
</header>
1413
<body>
15-
<font name="Arial" size="11" color="000000" bold="false"/>
16-
<fill color="FFFFCC"/>
14+
<font name="Arial" size="10" color="000000" bold="false"/>
1715
<alignment horizontal="left" vertical="middle"/>
1816
<border>
1917
<all style="thin" color="CCCCCC"/>

0 commit comments

Comments
 (0)