@vdhewei/xlsx-template-lib 1.6.5 → 1.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bin.js CHANGED
@@ -4289,11 +4289,9 @@ var XlsxRender = class _XlsxRender extends Workbook {
4289
4289
 
4290
4290
  // src/bin-helpers.ts
4291
4291
  var path2 = __toESM(require("path"));
4292
- var url = __toESM(require("url"));
4293
4292
  var import_node_fs = require("fs");
4294
4293
  var fs3 = __toESM(require("fs/promises"));
4295
4294
  var import_chalk = __toESM(require("chalk"));
4296
- var import_meta = {};
4297
4295
  function generateOutputFilename(inputFile) {
4298
4296
  const basename3 = path2.basename(inputFile, path2.extname(inputFile));
4299
4297
  const timestamp = Date.now();
@@ -4304,7 +4302,7 @@ async function resolveFilePath(filePath) {
4304
4302
  if ((0, import_node_fs.existsSync)(resolvedPath)) {
4305
4303
  return resolvedPath;
4306
4304
  }
4307
- const scriptDir = path2.dirname(url.fileURLToPath(import_meta.url));
4305
+ const scriptDir = path2.dirname(process.cwd());
4308
4306
  const relativePath = path2.resolve(scriptDir, filePath);
4309
4307
  if ((0, import_node_fs.existsSync)(relativePath)) {
4310
4308
  return relativePath;
@@ -4349,43 +4347,70 @@ function checkSheetAndPlaceholders(xlsx, sheetName) {
4349
4347
  throw new Error(`Sheet "${sheetName}" not found in Excel file`);
4350
4348
  }
4351
4349
  }
4352
- async function addRuleToSheet(xlsxBuffer, ruleType, ruleExpr, sheetName = "export_metadata.config") {
4353
- const workbook = await loadWorkbook(xlsxBuffer);
4350
+ async function addRuleToSheet(workbook, ruleType, ruleExpr, sheetName = "export_metadata.config") {
4354
4351
  let worksheet = workbook.getWorksheet(sheetName);
4355
4352
  if (!worksheet) {
4356
4353
  worksheet = workbook.addWorksheet(sheetName);
4357
4354
  console.log(import_chalk.default.gray(`Created new sheet: ${sheetName}`));
4358
4355
  }
4359
4356
  let startRow = 1;
4360
- let currentRow = 1;
4361
- let columnCount = 0;
4362
- worksheet.eachRow((row, rowNumber) => {
4363
- const cell = row.getCell(1);
4364
- const cellValue = cell.value?.toString().trim();
4365
- if (!cellValue) {
4366
- return;
4367
- }
4368
- if (cellValue.toLowerCase() === ruleType.toLowerCase()) {
4369
- let col = 2;
4370
- while (col <= 4) {
4371
- const ruleCell = row.getCell(col);
4372
- if (ruleCell.value) {
4373
- columnCount++;
4374
- }
4375
- col++;
4357
+ let currentRow = NaN;
4358
+ if (worksheet.rowCount > 0) {
4359
+ let stop = false;
4360
+ worksheet.eachRow((row, rowNumber) => {
4361
+ let columnCount = 0;
4362
+ const cell = row.getCell(1);
4363
+ const cellValue = cell.value;
4364
+ if (!cellValue || stop) {
4365
+ return;
4376
4366
  }
4377
- if (columnCount < 4) {
4378
- currentRow = rowNumber;
4379
- startRow = rowNumber;
4380
- } else {
4381
- currentRow = rowNumber + 1;
4367
+ const value = toCellValue(cellValue).trim();
4368
+ if (value.toLowerCase() === ruleType.toLowerCase()) {
4369
+ for (let col = 2; col < 5; col++) {
4370
+ const ruleCell2 = row.getCell(col);
4371
+ if (ruleCell2.value) {
4372
+ columnCount++;
4373
+ } else {
4374
+ break;
4375
+ }
4376
+ }
4377
+ if (columnCount < 3) {
4378
+ currentRow = rowNumber;
4379
+ startRow = rowNumber;
4380
+ } else {
4381
+ currentRow = rowNumber + 1;
4382
+ }
4383
+ const cell2 = worksheet.getRow(currentRow).getCell(1);
4384
+ if (cell2 === void 0 || cell2.value === null || cell2.value === void 0) {
4385
+ stop = true;
4386
+ return;
4387
+ }
4388
+ if (toCellValue(cell2.value).toLowerCase() !== ruleType.toLowerCase()) {
4389
+ worksheet.insertRow(currentRow, []);
4390
+ currentRow = currentRow + 1;
4391
+ stop = true;
4392
+ return;
4393
+ }
4394
+ if (toCellValue(cell2.value).toLowerCase() === ruleType.toLowerCase() && columnCount < 3) {
4395
+ stop = true;
4396
+ return;
4397
+ }
4382
4398
  }
4383
- }
4384
- });
4399
+ });
4400
+ }
4401
+ if (isNaN(currentRow)) {
4402
+ currentRow = worksheet.rowCount + 1;
4403
+ worksheet.insertRow(currentRow, []);
4404
+ }
4385
4405
  if (startRow === 1 && currentRow === 1 && worksheet.rowCount === 0) {
4386
4406
  currentRow = 1;
4387
4407
  }
4388
4408
  let targetRow = worksheet.getRow(currentRow);
4409
+ let lastCell = targetRow.getCell(4);
4410
+ if (lastCell !== void 0 && lastCell.value !== void 0 && lastCell.value !== null && lastCell.value !== "") {
4411
+ currentRow = worksheet.rowCount + 1;
4412
+ targetRow = worksheet.insertRow(currentRow, []);
4413
+ }
4389
4414
  if (!targetRow.getCell(1).value) {
4390
4415
  const typeCell = targetRow.getCell(1);
4391
4416
  typeCell.value = ruleType;
@@ -4395,23 +4420,21 @@ async function addRuleToSheet(xlsxBuffer, ruleType, ruleExpr, sheetName = "expor
4395
4420
  let ruleCol = 2;
4396
4421
  while (ruleCol <= 4) {
4397
4422
  const existingCell = targetRow.getCell(ruleCol);
4398
- if (!existingCell.value) {
4423
+ if (existingCell === void 0 || (existingCell.value === void 0 || existingCell.value === null || existingCell.value === "")) {
4399
4424
  break;
4400
4425
  }
4401
4426
  ruleCol++;
4402
4427
  }
4403
- if (ruleCol <= 4) {
4404
- const ruleCell = targetRow.getCell(ruleCol);
4405
- ruleCell.value = ruleExpr;
4406
- ruleCell.alignment = { vertical: "middle" };
4407
- const column = worksheet.getColumn(ruleCol);
4408
- column.width = Math.max(column.width || 10, ruleExpr.length + 2);
4409
- console.log(import_chalk.default.gray(`Added rule ${ruleType} at row ${currentRow}, column ${ruleCol}`));
4410
- } else {
4411
- throw new Error(`Cannot add more than 4 rules for type: ${ruleType}`);
4428
+ const ruleCell = targetRow.getCell(ruleCol);
4429
+ ruleCell.value = ruleExpr;
4430
+ ruleCell.alignment = { vertical: "middle", horizontal: "center" };
4431
+ const column = worksheet.getColumn(ruleCol);
4432
+ column.width = Math.max(column.width || 10, ruleExpr.length + 2);
4433
+ if (currentRow === 1 && ruleCol === 2) {
4434
+ const column2 = worksheet.getColumn(1);
4435
+ column2.width = Math.max(column2.width || 10, ruleExpr.length + 2);
4412
4436
  }
4413
- const buffer = await workbook.xlsx.writeBuffer();
4414
- return Buffer.from(buffer);
4437
+ return workbook;
4415
4438
  }
4416
4439
  async function parseRulesFromFile(filePath) {
4417
4440
  const resolvedPath = await resolveFilePath(filePath);
@@ -4419,6 +4442,7 @@ async function parseRulesFromFile(filePath) {
4419
4442
  const lines = fileContent.split("\n");
4420
4443
  const rules = [];
4421
4444
  const validTypes = ["cell", "alias", "rowCell", "mergeCell"];
4445
+ const validRulesSet = /* @__PURE__ */ new Set();
4422
4446
  for (let i = 0; i < lines.length; i++) {
4423
4447
  const line = lines[i].trim();
4424
4448
  if (!line || line.startsWith("#")) {
@@ -4429,17 +4453,41 @@ async function parseRulesFromFile(filePath) {
4429
4453
  console.log(import_chalk.default.yellow(`\u26A0 Line ${i + 1}: Invalid format. Expected "<type> ruleExpr"`));
4430
4454
  continue;
4431
4455
  }
4456
+ let items = [];
4432
4457
  const type = line.substring(0, spaceIndex).trim();
4433
- const rule = line.substring(spaceIndex + 1).trim();
4434
- if (!type || !rule) {
4458
+ const values = line.substring(spaceIndex + 1).trim();
4459
+ if (values.indexOf(" ") >= 0) {
4460
+ items = values.split(" ");
4461
+ } else if (values.indexOf(" ") >= 0) {
4462
+ items = values.split(" ");
4463
+ } else {
4464
+ items = [values];
4465
+ }
4466
+ items = items.filter((item) => item.trim() !== "" && item.indexOf("=") > 0);
4467
+ if (!type) {
4435
4468
  console.log(import_chalk.default.yellow(`\u26A0 Line ${i + 1}: Invalid format. Expected "<type> ruleExpr"`));
4436
4469
  continue;
4437
4470
  }
4471
+ if (!items || items.length === 0) {
4472
+ continue;
4473
+ }
4438
4474
  if (!validTypes.includes(type)) {
4439
4475
  console.log(import_chalk.default.yellow(`\u26A0 Line ${i + 1}: Invalid rule type "${type}". Must be one of: ${validTypes.join(", ")}`));
4440
4476
  continue;
4441
4477
  }
4442
- rules.push({ type, rule });
4478
+ for (const rule of items) {
4479
+ let str = rule.trim();
4480
+ if (str === void 0 || str === null || str === "" || str === " ") {
4481
+ continue;
4482
+ }
4483
+ let key = `${type}:${str}`;
4484
+ if (validRulesSet.has(key)) {
4485
+ console.log(import_chalk.default.yellow(`\u26A0 Line ${i + 1}: Duplicate rule "${str}"`));
4486
+ continue;
4487
+ }
4488
+ validRulesSet.add(key);
4489
+ rules.push({ type, rule: str });
4490
+ }
4443
4491
  }
4444
4492
  if (rules.length === 0) {
4445
4493
  throw new Error("No valid rules found in file");
@@ -4447,12 +4495,12 @@ async function parseRulesFromFile(filePath) {
4447
4495
  return rules;
4448
4496
  }
4449
4497
  async function addMultipleRulesToSheet(xlsxBuffer, rules) {
4450
- let buffer = xlsxBuffer;
4498
+ let workbook = await loadWorkbook(xlsxBuffer);
4451
4499
  for (const { type, rule } of rules) {
4452
- console.log(import_chalk.default.gray(`Adding ${type} rule: ${rule}`));
4453
- buffer = await addRuleToSheet(buffer, type, rule);
4500
+ workbook = await addRuleToSheet(workbook, type, rule);
4454
4501
  }
4455
- return buffer;
4502
+ const buffer = await workbook.xlsx.writeBuffer();
4503
+ return Buffer.from(buffer);
4456
4504
  }
4457
4505
 
4458
4506
  // src/bin.ts