@vdhewei/xlsx-template-lib 1.4.2 → 1.5.0

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.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  ExprResolver
4
- } from "./chunk-XYVHFW4V.mjs";
4
+ } from "./chunk-TC2FHOAF.mjs";
5
5
 
6
6
  // src/bin.ts
7
7
  import chalk from "chalk";
@@ -2997,7 +2997,7 @@ var extractMacro = function(expr, options) {
2997
2997
  }
2998
2998
  return extracResult;
2999
2999
  };
3000
- var __codeKey = (str) => {
3000
+ var __codeKey = (str, expr) => {
3001
3001
  const replaces = [" ", `-`, `/`, `,`, `'`, `&`, `.`, `(`, `)`, `{`, `}`, `@`, `\\`, `[`, `]`, `#`, `:`];
3002
3002
  for (const k of replaces) {
3003
3003
  str = str.replaceAll(k, "_").trim();
@@ -3013,12 +3013,15 @@ var __codeKey = (str) => {
3013
3013
  }
3014
3014
  return str.toUpperCase();
3015
3015
  };
3016
- var __numberKey = (str) => {
3016
+ var __numberKey = (str, expr) => {
3017
3017
  return Number.parseInt(str, 10).toString();
3018
3018
  };
3019
- var __codeAliasKey = (str) => {
3019
+ var __codeAliasKey = (str, expr) => {
3020
3020
  const key = __codeKey(str);
3021
3021
  if (key !== "") {
3022
+ if (expr !== void 0 && expr.tokens.length > 0) {
3023
+ expr.tokens.push("@" /* UseAliasToken */);
3024
+ }
3022
3025
  return `${defaultRuleTokenMap.get("@" /* UseAliasToken */)}${key}`;
3023
3026
  }
3024
3027
  return "";
@@ -3029,11 +3032,11 @@ var macroFormatter = /* @__PURE__ */ new Map([
3029
3032
  [codeAliasKey, __codeAliasKey],
3030
3033
  [defaultKey, (v) => v]
3031
3034
  ]);
3032
- var execMacroFormat = function(value, formatter) {
3035
+ var execMacroFormat = function(value, formatter, expr) {
3033
3036
  if (!macroFormatter.has(formatter)) {
3034
3037
  return value;
3035
3038
  }
3036
- return macroFormatter.get(formatter)(value);
3039
+ return macroFormatter.get(formatter)(value, expr);
3037
3040
  };
3038
3041
  var toCellRow = (rowVals, setup) => {
3039
3042
  if (setup === void 0) {
@@ -3131,7 +3134,7 @@ var resolveCompileMacroExpr = (ctx, macroExpr, macroTokens2, currentCellIndex, t
3131
3134
  return;
3132
3135
  }
3133
3136
  const value = toCellValue(cellValue.value);
3134
- let exprValue2 = execMacroFormat(value, formatter);
3137
+ let exprValue2 = execMacroFormat(value, formatter, ctx.currentExpr || void 0);
3135
3138
  parts.push(exprValue2);
3136
3139
  });
3137
3140
  });
@@ -3159,6 +3162,8 @@ var resolveCompileMacroExpr = (ctx, macroExpr, macroTokens2, currentCellIndex, t
3159
3162
  exprValue = resolveCompileMacroGen(ctx, macroCurrent, currentCellIndex);
3160
3163
  }
3161
3164
  macroExpr = exprValue;
3165
+ } else {
3166
+ macroExpr = resolveAliasExpr(ctx, macroExpr, currentCellIndex);
3162
3167
  }
3163
3168
  return macroExpr;
3164
3169
  };
@@ -3977,6 +3982,170 @@ var generateCommandsXlsxTemplateWithCompile = async function(data, values, compi
3977
3982
  await w.substituteAll(values);
3978
3983
  return w.generate(options);
3979
3984
  };
3985
+ var compileAll = async (buf, compileOpts, renderData) => {
3986
+ if (compileOpts === void 0 || compileOpts.sheetName === "") {
3987
+ return buf;
3988
+ }
3989
+ const result = await ExprResolver.compile(buf, compileOpts.sheetName, compileOpts);
3990
+ if (result.errs !== void 0 && result.errs.length > 0) {
3991
+ throw result.errs[0];
3992
+ }
3993
+ if (compileOpts.remove !== void 0 && compileOpts.remove === true) {
3994
+ result.workbook = ExprResolver.removeUnExportSheets(result.workbook, compileOpts);
3995
+ }
3996
+ if (renderData !== void 0) {
3997
+ autoRegisterAlias(renderData, result.configure);
3998
+ }
3999
+ return await ExprResolver.toBuffer(result.workbook);
4000
+ };
4001
+
4002
+ // src/biz.ts
4003
+ import { extname as extname2 } from "path";
4004
+ import { clone } from "lodash";
4005
+ import AdmZip from "adm-zip";
4006
+ var XlsxRender = class _XlsxRender extends Workbook {
4007
+ constructor(option) {
4008
+ super(option);
4009
+ }
4010
+ static async create(data, option) {
4011
+ const w = await super.parse(data, option);
4012
+ w.setQueryFunctionHandler(commandExtendQuery);
4013
+ const app = new _XlsxRender(option);
4014
+ Object.assign(app, { ...w });
4015
+ return app;
4016
+ }
4017
+ async render(values, sheetName) {
4018
+ await this.substitute(sheetName, values);
4019
+ }
4020
+ getSheets() {
4021
+ return this.sheets;
4022
+ }
4023
+ };
4024
+ var ZipXlsxTemplateApp = class _ZipXlsxTemplateApp {
4025
+ constructor(data) {
4026
+ this.records = /* @__PURE__ */ new Map();
4027
+ this.zipBuffer = data;
4028
+ if (data !== void 0) {
4029
+ this.xlsxEntries = this.parse(data);
4030
+ }
4031
+ }
4032
+ loadZipBuffer(data) {
4033
+ this.zipBuffer = data;
4034
+ this.zip = new AdmZip(data);
4035
+ this.xlsxEntries = this.parse(data);
4036
+ return this;
4037
+ }
4038
+ parse(data) {
4039
+ const zip = new AdmZip(data);
4040
+ const result = /* @__PURE__ */ new Map();
4041
+ const entries = zip.getEntries();
4042
+ for (let fd of entries) {
4043
+ if (fd.isDirectory) {
4044
+ continue;
4045
+ }
4046
+ let ext = extname2(fd.entryName).substring(1).toLowerCase();
4047
+ if (ext !== "xlsx") {
4048
+ continue;
4049
+ }
4050
+ result.set(fd.entryName, fd.getData());
4051
+ }
4052
+ this.zip = zip;
4053
+ return result;
4054
+ }
4055
+ getEntries() {
4056
+ if (this.xlsxEntries !== void 0 && this.xlsxEntries.size > 0) {
4057
+ return this.xlsxEntries;
4058
+ } else {
4059
+ if (this.zipBuffer !== void 0) {
4060
+ return this.parse(this.zipBuffer);
4061
+ }
4062
+ }
4063
+ return /* @__PURE__ */ new Map();
4064
+ }
4065
+ static async compileAll(files, renderData, compileOpts) {
4066
+ const records = /* @__PURE__ */ new Map();
4067
+ if (compileOpts !== void 0 && (compileOpts.sheetName === void 0 || compileOpts.sheetName === "")) {
4068
+ compileOpts.sheetName = compileRuleSheetName;
4069
+ }
4070
+ let values = clone(renderData);
4071
+ for (let [key, buf] of files.entries()) {
4072
+ buf = await compileAll(buf, clone(compileOpts), clone(values));
4073
+ records.set(key, buf);
4074
+ }
4075
+ return records;
4076
+ }
4077
+ async substituteAll(renderData, compileOpts, renderOpts) {
4078
+ const files = await _ZipXlsxTemplateApp.compileAll(this.xlsxEntries, renderData, compileOpts);
4079
+ for (const [k, buf] of files.entries()) {
4080
+ const xlsx = await XlsxRender.create(buf, renderOpts);
4081
+ await xlsx.substituteAll(renderData);
4082
+ this.records.set(k, xlsx);
4083
+ }
4084
+ return this;
4085
+ }
4086
+ async generate(options) {
4087
+ if (this.records === void 0 || this.records.size <= 0) {
4088
+ return this.zipBuffer;
4089
+ }
4090
+ if (this.zip === void 0) {
4091
+ this.zip = new AdmZip();
4092
+ }
4093
+ if (options === void 0 || options === null) {
4094
+ options = {
4095
+ type: "nodebuffer" /* NodeBuffer */,
4096
+ compression: "DEFLATE",
4097
+ compressionOptions: {
4098
+ level: 9
4099
+ }
4100
+ };
4101
+ }
4102
+ for (const [key, xlsx] of this.records) {
4103
+ const buf = await xlsx.generate(options);
4104
+ let entry = this.zip.getEntry(key);
4105
+ if (entry !== null) {
4106
+ entry.setData(Buffer.from(buf));
4107
+ } else {
4108
+ this.zip.addFile(key, Buffer.from(buf));
4109
+ }
4110
+ }
4111
+ return this.zip.toBuffer();
4112
+ }
4113
+ static async compileTo(data, opts, values) {
4114
+ const zip = new AdmZip(data);
4115
+ const entries = zip.getEntries();
4116
+ let files = /* @__PURE__ */ new Map();
4117
+ if (values === void 0) {
4118
+ values = /* @__PURE__ */ new Map();
4119
+ }
4120
+ for (let fd of entries) {
4121
+ if (fd.isDirectory) {
4122
+ continue;
4123
+ }
4124
+ let ext = extname2(fd.entryName).substring(1).toLowerCase();
4125
+ if (ext !== "xlsx") {
4126
+ continue;
4127
+ }
4128
+ let buf = fd.getData();
4129
+ if (opts.checker !== void 0) {
4130
+ await opts.checker(buf, opts.options, values, opts.fileName || void 0);
4131
+ }
4132
+ files.set(fd.entryName, buf);
4133
+ }
4134
+ const compileOpts = new RuleMapOptions();
4135
+ compileOpts.remove = true;
4136
+ if (files.size > 0) {
4137
+ files = await _ZipXlsxTemplateApp.compileAll(files, values, compileOpts);
4138
+ } else {
4139
+ throw new Error(`empty xlsx file zip file`);
4140
+ }
4141
+ if (files.size > 0) {
4142
+ for (const [k, data2] of files.entries()) {
4143
+ zip.getEntry(k).setData(data2);
4144
+ }
4145
+ }
4146
+ return zip.toBuffer();
4147
+ }
4148
+ };
3980
4149
 
3981
4150
  export {
3982
4151
  BufferType,
@@ -4022,6 +4191,9 @@ export {
4022
4191
  compileRuleSheetName,
4023
4192
  mergeMap,
4024
4193
  autoRegisterAlias,
4025
- generateCommandsXlsxTemplateWithCompile
4194
+ generateCommandsXlsxTemplateWithCompile,
4195
+ compileAll,
4196
+ XlsxRender,
4197
+ ZipXlsxTemplateApp
4026
4198
  };
4027
- //# sourceMappingURL=chunk-XYVHFW4V.mjs.map
4199
+ //# sourceMappingURL=chunk-TC2FHOAF.mjs.map