@leankylin-sheet/excel 5.2.33 → 5.2.35

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.
@@ -1 +1 @@
1
- export declare function exportSheetExcel(luckysheetRef: any, fileName: string): Promise<void>;
1
+ export declare function exportSheetExcel(luckysheetRef: any, fileName: string, variables?: any): Promise<void>;
@@ -1,3 +1,3 @@
1
1
  import ExcelJS from "exceljs";
2
- declare const setStyleAndValue: (table: any, worksheet: ExcelJS.Worksheet) => void;
2
+ declare const setStyleAndValue: (table: any, worksheet: ExcelJS.Worksheet, variables?: any) => void;
3
3
  export { setStyleAndValue };
package/dist/index.esm.js CHANGED
@@ -5076,7 +5076,24 @@ var formatHyperlink = function formatHyperlink(address) {
5076
5076
  var sheetCell = address.split("!");
5077
5077
  return "#'".concat(sheetCell[0], "'!").concat(sheetCell[1] || "A1");
5078
5078
  };
5079
- var setStyleAndValue = function setStyleAndValue(table, worksheet) {
5079
+ var getVariable = function getVariable(variables, sheetId, name) {
5080
+ var _variables$sheetId;
5081
+ if ((variables === null || variables === void 0 ? void 0 : (_variables$sheetId = variables[sheetId]) === null || _variables$sheetId === void 0 ? void 0 : _variables$sheetId[name]) !== undefined) {
5082
+ return variables[sheetId][name];
5083
+ }
5084
+ return variables === null || variables === void 0 ? void 0 : variables[name];
5085
+ };
5086
+ var replaceVariablesInFormula = function replaceVariablesInFormula(formula, variables, sheetId) {
5087
+ if (_.isNil(variables)) return formula;
5088
+ return formula.replace(/\{\{(.*?)\}\}/g, function (match) {
5089
+ var value = getVariable(variables, sheetId, match);
5090
+ if (_.isNil(value)) return match;
5091
+ if (typeof value === "string") return "\"".concat(value.replace(/"/g, '""'), "\"");
5092
+ if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
5093
+ return String(value);
5094
+ });
5095
+ };
5096
+ var setStyleAndValue = function setStyleAndValue(table, worksheet, variables) {
5080
5097
  var cellArr = table === null || table === void 0 ? void 0 : table.data;
5081
5098
  if (!Array.isArray(cellArr)) return;
5082
5099
  cellArr.forEach(function (row, rowid) {
@@ -5165,8 +5182,9 @@ var setStyleAndValue = function setStyleAndValue(table, worksheet) {
5165
5182
  v = cell.v;
5166
5183
  }
5167
5184
  if (cell.f && _typeof(v) !== "object") {
5185
+ var formulaStr = cell.f.startsWith("=") ? cell.f.slice(1) : cell.f;
5168
5186
  value = {
5169
- formula: cell.f.startsWith("=") ? cell.f.slice(1) : cell.f,
5187
+ formula: replaceVariablesInFormula(formulaStr, variables, table.id),
5170
5188
  result: v
5171
5189
  };
5172
5190
  } else {
@@ -6437,11 +6455,11 @@ function setFreeze(table, worksheet) {
6437
6455
  }];
6438
6456
  }
6439
6457
 
6440
- function exportSheetExcel(_x, _x2) {
6458
+ function exportSheetExcel(_x, _x2, _x3) {
6441
6459
  return _exportSheetExcel.apply(this, arguments);
6442
6460
  }
6443
6461
  function _exportSheetExcel() {
6444
- _exportSheetExcel = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(luckysheetRef, fileName) {
6462
+ _exportSheetExcel = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(luckysheetRef, fileName, variables) {
6445
6463
  var luckysheet, workbook, buffer, fileData;
6446
6464
  return _regeneratorRuntime().wrap(function _callee$(_context) {
6447
6465
  while (1) switch (_context.prev = _context.next) {
@@ -6454,7 +6472,7 @@ function _exportSheetExcel() {
6454
6472
  var _table$data, _table$config;
6455
6473
  if ((table === null || table === void 0 ? void 0 : (_table$data = table.data) === null || _table$data === void 0 ? void 0 : _table$data.length) === 0) return true;
6456
6474
  var worksheet = workbook.addWorksheet(table.name);
6457
- setStyleAndValue(table, worksheet);
6475
+ setStyleAndValue(table, worksheet, variables);
6458
6476
  setMerge(table === null || table === void 0 ? void 0 : (_table$config = table.config) === null || _table$config === void 0 ? void 0 : _table$config.merge, worksheet);
6459
6477
  setBorder(table, worksheet);
6460
6478
  setHiddenRowCol(table, worksheet);
@@ -6530,12 +6548,15 @@ var transformExcelToFortune = /*#__PURE__*/function () {
6530
6548
  }();
6531
6549
  var transformFortuneToExcel = /*#__PURE__*/function () {
6532
6550
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(luckysheetRef, fileName) {
6551
+ var _luckysheetRef$getVar;
6552
+ var variables;
6533
6553
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
6534
6554
  while (1) switch (_context2.prev = _context2.next) {
6535
6555
  case 0:
6536
- _context2.next = 2;
6537
- return exportSheetExcel(luckysheetRef, fileName);
6538
- case 2:
6556
+ variables = luckysheetRef === null || luckysheetRef === void 0 ? void 0 : (_luckysheetRef$getVar = luckysheetRef.getVariables) === null || _luckysheetRef$getVar === void 0 ? void 0 : _luckysheetRef$getVar.call(luckysheetRef);
6557
+ _context2.next = 3;
6558
+ return exportSheetExcel(luckysheetRef, fileName, variables);
6559
+ case 3:
6539
6560
  case "end":
6540
6561
  return _context2.stop();
6541
6562
  }
package/dist/index.js CHANGED
@@ -5088,7 +5088,24 @@ var formatHyperlink = function formatHyperlink(address) {
5088
5088
  var sheetCell = address.split("!");
5089
5089
  return "#'".concat(sheetCell[0], "'!").concat(sheetCell[1] || "A1");
5090
5090
  };
5091
- var setStyleAndValue = function setStyleAndValue(table, worksheet) {
5091
+ var getVariable = function getVariable(variables, sheetId, name) {
5092
+ var _variables$sheetId;
5093
+ if ((variables === null || variables === void 0 ? void 0 : (_variables$sheetId = variables[sheetId]) === null || _variables$sheetId === void 0 ? void 0 : _variables$sheetId[name]) !== undefined) {
5094
+ return variables[sheetId][name];
5095
+ }
5096
+ return variables === null || variables === void 0 ? void 0 : variables[name];
5097
+ };
5098
+ var replaceVariablesInFormula = function replaceVariablesInFormula(formula, variables, sheetId) {
5099
+ if (___default['default'].isNil(variables)) return formula;
5100
+ return formula.replace(/\{\{(.*?)\}\}/g, function (match) {
5101
+ var value = getVariable(variables, sheetId, match);
5102
+ if (___default['default'].isNil(value)) return match;
5103
+ if (typeof value === "string") return "\"".concat(value.replace(/"/g, '""'), "\"");
5104
+ if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
5105
+ return String(value);
5106
+ });
5107
+ };
5108
+ var setStyleAndValue = function setStyleAndValue(table, worksheet, variables) {
5092
5109
  var cellArr = table === null || table === void 0 ? void 0 : table.data;
5093
5110
  if (!Array.isArray(cellArr)) return;
5094
5111
  cellArr.forEach(function (row, rowid) {
@@ -5177,8 +5194,9 @@ var setStyleAndValue = function setStyleAndValue(table, worksheet) {
5177
5194
  v = cell.v;
5178
5195
  }
5179
5196
  if (cell.f && _typeof(v) !== "object") {
5197
+ var formulaStr = cell.f.startsWith("=") ? cell.f.slice(1) : cell.f;
5180
5198
  value = {
5181
- formula: cell.f.startsWith("=") ? cell.f.slice(1) : cell.f,
5199
+ formula: replaceVariablesInFormula(formulaStr, variables, table.id),
5182
5200
  result: v
5183
5201
  };
5184
5202
  } else {
@@ -6449,11 +6467,11 @@ function setFreeze(table, worksheet) {
6449
6467
  }];
6450
6468
  }
6451
6469
 
6452
- function exportSheetExcel(_x, _x2) {
6470
+ function exportSheetExcel(_x, _x2, _x3) {
6453
6471
  return _exportSheetExcel.apply(this, arguments);
6454
6472
  }
6455
6473
  function _exportSheetExcel() {
6456
- _exportSheetExcel = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(luckysheetRef, fileName) {
6474
+ _exportSheetExcel = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(luckysheetRef, fileName, variables) {
6457
6475
  var luckysheet, workbook, buffer, fileData;
6458
6476
  return _regeneratorRuntime().wrap(function _callee$(_context) {
6459
6477
  while (1) switch (_context.prev = _context.next) {
@@ -6466,7 +6484,7 @@ function _exportSheetExcel() {
6466
6484
  var _table$data, _table$config;
6467
6485
  if ((table === null || table === void 0 ? void 0 : (_table$data = table.data) === null || _table$data === void 0 ? void 0 : _table$data.length) === 0) return true;
6468
6486
  var worksheet = workbook.addWorksheet(table.name);
6469
- setStyleAndValue(table, worksheet);
6487
+ setStyleAndValue(table, worksheet, variables);
6470
6488
  setMerge(table === null || table === void 0 ? void 0 : (_table$config = table.config) === null || _table$config === void 0 ? void 0 : _table$config.merge, worksheet);
6471
6489
  setBorder(table, worksheet);
6472
6490
  setHiddenRowCol(table, worksheet);
@@ -6542,12 +6560,15 @@ var transformExcelToFortune = /*#__PURE__*/function () {
6542
6560
  }();
6543
6561
  var transformFortuneToExcel = /*#__PURE__*/function () {
6544
6562
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(luckysheetRef, fileName) {
6563
+ var _luckysheetRef$getVar;
6564
+ var variables;
6545
6565
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
6546
6566
  while (1) switch (_context2.prev = _context2.next) {
6547
6567
  case 0:
6548
- _context2.next = 2;
6549
- return exportSheetExcel(luckysheetRef, fileName);
6550
- case 2:
6568
+ variables = luckysheetRef === null || luckysheetRef === void 0 ? void 0 : (_luckysheetRef$getVar = luckysheetRef.getVariables) === null || _luckysheetRef$getVar === void 0 ? void 0 : _luckysheetRef$getVar.call(luckysheetRef);
6569
+ _context2.next = 3;
6570
+ return exportSheetExcel(luckysheetRef, fileName, variables);
6571
+ case 3:
6551
6572
  case "end":
6552
6573
  return _context2.stop();
6553
6574
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leankylin-sheet/excel",
3
- "version": "5.2.33",
3
+ "version": "5.2.35",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "dist/index.esm.js",