@vdhewei/xlsx-template-lib 1.4.3 → 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/index.d.mts CHANGED
@@ -470,5 +470,35 @@ declare const compileRuleSheetName = "export_metadata.config";
470
470
  declare const mergeMap: (source: Map<string, string>, dest: Map<string, string>) => Map<string, string>;
471
471
  declare const autoRegisterAlias: (values: Object, configure: RuleResult) => Object;
472
472
  declare const generateCommandsXlsxTemplateWithCompile: <T extends JsZip.OutputType>(data: Buffer, values: Object, compileOptions: AutoOptions, options?: JsZip.JSZipGeneratorOptions<T> & FullOptions) => Promise<OutputByType[T]>;
473
+ declare const compileAll: (buf: Buffer, compileOpts: AutoOptions, renderData?: Object) => Promise<Buffer>;
473
474
 
474
- export { AddCommand, AddCommandMust, type AfterReplaceHook, type Argument, ArgumentData, ArgumentValue, ArgumentValueLoader, type AutoOptions, type BeforeReplaceHook, BufferType, type CellPosition, type CmdFunction, CompileContext, type CompileResult, type CustomFormatter, type CustomPlaceholderExtractor, type CustomReplacer, DefaultPlaceholderCellValue, type DrawingInfo, ExprResolver, type ExtensionOptions, type ExtractMacroArgs, type FilterMacroResult, type FullOptions, type MacroArgs, type MacroUnitHelper, type OutputByType, type Placeholder, type PlaceholderCellValue, type QueryFunction, type Range, type Ref, type RelsInfo, RuleMapOptions, type RuleOptions, type RuleResult, RuleToken, type SheetInfo, type TableInfo, TokenParserManger, Workbook, type WorkbookOptions, autoRegisterAlias, columnLetterToNumber, columnNumberToLetter, commandExtendQuery, compileRuleSheetName, compileWorkSheet, compileWorkSheetPlaceholder, defaultExtractPlaceholders, defaultFormatters, defaultValueDotGet, generateCommandsXlsxTemplate, generateCommandsXlsxTemplateWithCompile, generateXlsxTemplate, getCommands, getTokenParser, hasGeneratorToken, isRuleToken, isUrl, loadCompileSheets, loadWorkbook, mergeMap, parseWorkSheetRules, registerTokenParser, registerTokenParserMust, resolveArgument, scanCellSetPlaceholder, toArrayBuffer, toCellValue, valueDotGet, workSheetSetPlaceholder };
475
+ type CustomChecker = (data: Buffer, options: FullOptions & {
476
+ [key: string]: any;
477
+ }, values: Object, fileName?: string) => Promise<Buffer>;
478
+ type CustomCheckerOptions = {
479
+ checker?: CustomChecker;
480
+ options?: FullOptions;
481
+ [key: string]: any;
482
+ };
483
+ declare class XlsxRender extends Workbook {
484
+ constructor(option?: FullOptions);
485
+ static create(data: Buffer, option?: FullOptions): Promise<XlsxRender>;
486
+ render(values: Object, sheetName: string): Promise<void>;
487
+ getSheets(): SheetInfo[];
488
+ }
489
+ declare class ZipXlsxTemplateApp {
490
+ zipBuffer?: Buffer;
491
+ private zip;
492
+ private xlsxEntries;
493
+ private records;
494
+ constructor(data?: Buffer);
495
+ loadZipBuffer(data: Buffer): ZipXlsxTemplateApp;
496
+ parse(data: Buffer): Map<string, Buffer>;
497
+ getEntries(): Map<string, Buffer>;
498
+ static compileAll(files: Map<string, Buffer>, renderData?: Object, compileOpts?: AutoOptions): Promise<Map<string, Buffer>>;
499
+ substituteAll(renderData: Object, compileOpts?: AutoOptions, renderOpts?: FullOptions): Promise<ZipXlsxTemplateApp>;
500
+ generate(options?: JsZip.JSZipGeneratorOptions<BufferType.NodeBuffer> & FullOptions): Promise<Buffer>;
501
+ static compileTo(data: Buffer, opts: CustomCheckerOptions, values?: Record<string, any> | Object): Promise<Buffer>;
502
+ }
503
+
504
+ export { AddCommand, AddCommandMust, type AfterReplaceHook, type Argument, ArgumentData, ArgumentValue, ArgumentValueLoader, type AutoOptions, type BeforeReplaceHook, BufferType, type CellPosition, type CmdFunction, CompileContext, type CompileResult, type CustomChecker, type CustomFormatter, type CustomPlaceholderExtractor, type CustomReplacer, DefaultPlaceholderCellValue, type DrawingInfo, ExprResolver, type ExtensionOptions, type ExtractMacroArgs, type FilterMacroResult, type FullOptions, type MacroArgs, type MacroUnitHelper, type OutputByType, type Placeholder, type PlaceholderCellValue, type QueryFunction, type Range, type Ref, type RelsInfo, RuleMapOptions, type RuleOptions, type RuleResult, RuleToken, type SheetInfo, type TableInfo, TokenParserManger, Workbook, type WorkbookOptions, XlsxRender, ZipXlsxTemplateApp, autoRegisterAlias, columnLetterToNumber, columnNumberToLetter, commandExtendQuery, compileAll, compileRuleSheetName, compileWorkSheet, compileWorkSheetPlaceholder, defaultExtractPlaceholders, defaultFormatters, defaultValueDotGet, generateCommandsXlsxTemplate, generateCommandsXlsxTemplateWithCompile, generateXlsxTemplate, getCommands, getTokenParser, hasGeneratorToken, isRuleToken, isUrl, loadCompileSheets, loadWorkbook, mergeMap, parseWorkSheetRules, registerTokenParser, registerTokenParserMust, resolveArgument, scanCellSetPlaceholder, toArrayBuffer, toCellValue, valueDotGet, workSheetSetPlaceholder };
package/dist/index.d.ts CHANGED
@@ -470,5 +470,35 @@ declare const compileRuleSheetName = "export_metadata.config";
470
470
  declare const mergeMap: (source: Map<string, string>, dest: Map<string, string>) => Map<string, string>;
471
471
  declare const autoRegisterAlias: (values: Object, configure: RuleResult) => Object;
472
472
  declare const generateCommandsXlsxTemplateWithCompile: <T extends JsZip.OutputType>(data: Buffer, values: Object, compileOptions: AutoOptions, options?: JsZip.JSZipGeneratorOptions<T> & FullOptions) => Promise<OutputByType[T]>;
473
+ declare const compileAll: (buf: Buffer, compileOpts: AutoOptions, renderData?: Object) => Promise<Buffer>;
473
474
 
474
- export { AddCommand, AddCommandMust, type AfterReplaceHook, type Argument, ArgumentData, ArgumentValue, ArgumentValueLoader, type AutoOptions, type BeforeReplaceHook, BufferType, type CellPosition, type CmdFunction, CompileContext, type CompileResult, type CustomFormatter, type CustomPlaceholderExtractor, type CustomReplacer, DefaultPlaceholderCellValue, type DrawingInfo, ExprResolver, type ExtensionOptions, type ExtractMacroArgs, type FilterMacroResult, type FullOptions, type MacroArgs, type MacroUnitHelper, type OutputByType, type Placeholder, type PlaceholderCellValue, type QueryFunction, type Range, type Ref, type RelsInfo, RuleMapOptions, type RuleOptions, type RuleResult, RuleToken, type SheetInfo, type TableInfo, TokenParserManger, Workbook, type WorkbookOptions, autoRegisterAlias, columnLetterToNumber, columnNumberToLetter, commandExtendQuery, compileRuleSheetName, compileWorkSheet, compileWorkSheetPlaceholder, defaultExtractPlaceholders, defaultFormatters, defaultValueDotGet, generateCommandsXlsxTemplate, generateCommandsXlsxTemplateWithCompile, generateXlsxTemplate, getCommands, getTokenParser, hasGeneratorToken, isRuleToken, isUrl, loadCompileSheets, loadWorkbook, mergeMap, parseWorkSheetRules, registerTokenParser, registerTokenParserMust, resolveArgument, scanCellSetPlaceholder, toArrayBuffer, toCellValue, valueDotGet, workSheetSetPlaceholder };
475
+ type CustomChecker = (data: Buffer, options: FullOptions & {
476
+ [key: string]: any;
477
+ }, values: Object, fileName?: string) => Promise<Buffer>;
478
+ type CustomCheckerOptions = {
479
+ checker?: CustomChecker;
480
+ options?: FullOptions;
481
+ [key: string]: any;
482
+ };
483
+ declare class XlsxRender extends Workbook {
484
+ constructor(option?: FullOptions);
485
+ static create(data: Buffer, option?: FullOptions): Promise<XlsxRender>;
486
+ render(values: Object, sheetName: string): Promise<void>;
487
+ getSheets(): SheetInfo[];
488
+ }
489
+ declare class ZipXlsxTemplateApp {
490
+ zipBuffer?: Buffer;
491
+ private zip;
492
+ private xlsxEntries;
493
+ private records;
494
+ constructor(data?: Buffer);
495
+ loadZipBuffer(data: Buffer): ZipXlsxTemplateApp;
496
+ parse(data: Buffer): Map<string, Buffer>;
497
+ getEntries(): Map<string, Buffer>;
498
+ static compileAll(files: Map<string, Buffer>, renderData?: Object, compileOpts?: AutoOptions): Promise<Map<string, Buffer>>;
499
+ substituteAll(renderData: Object, compileOpts?: AutoOptions, renderOpts?: FullOptions): Promise<ZipXlsxTemplateApp>;
500
+ generate(options?: JsZip.JSZipGeneratorOptions<BufferType.NodeBuffer> & FullOptions): Promise<Buffer>;
501
+ static compileTo(data: Buffer, opts: CustomCheckerOptions, values?: Record<string, any> | Object): Promise<Buffer>;
502
+ }
503
+
504
+ export { AddCommand, AddCommandMust, type AfterReplaceHook, type Argument, ArgumentData, ArgumentValue, ArgumentValueLoader, type AutoOptions, type BeforeReplaceHook, BufferType, type CellPosition, type CmdFunction, CompileContext, type CompileResult, type CustomChecker, type CustomFormatter, type CustomPlaceholderExtractor, type CustomReplacer, DefaultPlaceholderCellValue, type DrawingInfo, ExprResolver, type ExtensionOptions, type ExtractMacroArgs, type FilterMacroResult, type FullOptions, type MacroArgs, type MacroUnitHelper, type OutputByType, type Placeholder, type PlaceholderCellValue, type QueryFunction, type Range, type Ref, type RelsInfo, RuleMapOptions, type RuleOptions, type RuleResult, RuleToken, type SheetInfo, type TableInfo, TokenParserManger, Workbook, type WorkbookOptions, XlsxRender, ZipXlsxTemplateApp, autoRegisterAlias, columnLetterToNumber, columnNumberToLetter, commandExtendQuery, compileAll, compileRuleSheetName, compileWorkSheet, compileWorkSheetPlaceholder, defaultExtractPlaceholders, defaultFormatters, defaultValueDotGet, generateCommandsXlsxTemplate, generateCommandsXlsxTemplateWithCompile, generateXlsxTemplate, getCommands, getTokenParser, hasGeneratorToken, isRuleToken, isUrl, loadCompileSheets, loadWorkbook, mergeMap, parseWorkSheetRules, registerTokenParser, registerTokenParserMust, resolveArgument, scanCellSetPlaceholder, toArrayBuffer, toCellValue, valueDotGet, workSheetSetPlaceholder };
package/dist/index.js CHANGED
@@ -43,10 +43,13 @@ __export(index_exports, {
43
43
  RuleToken: () => RuleToken,
44
44
  TokenParserManger: () => TokenParserManger,
45
45
  Workbook: () => Workbook,
46
+ XlsxRender: () => XlsxRender,
47
+ ZipXlsxTemplateApp: () => ZipXlsxTemplateApp,
46
48
  autoRegisterAlias: () => autoRegisterAlias,
47
49
  columnLetterToNumber: () => columnLetterToNumber,
48
50
  columnNumberToLetter: () => columnNumberToLetter,
49
51
  commandExtendQuery: () => commandExtendQuery,
52
+ compileAll: () => compileAll,
50
53
  compileRuleSheetName: () => compileRuleSheetName,
51
54
  compileWorkSheet: () => compileWorkSheet,
52
55
  compileWorkSheetPlaceholder: () => compileWorkSheetPlaceholder,
@@ -4061,6 +4064,170 @@ var generateCommandsXlsxTemplateWithCompile = async function(data, values, compi
4061
4064
  await w.substituteAll(values);
4062
4065
  return w.generate(options);
4063
4066
  };
4067
+ var compileAll = async (buf, compileOpts, renderData) => {
4068
+ if (compileOpts === void 0 || compileOpts.sheetName === "") {
4069
+ return buf;
4070
+ }
4071
+ const result = await ExprResolver.compile(buf, compileOpts.sheetName, compileOpts);
4072
+ if (result.errs !== void 0 && result.errs.length > 0) {
4073
+ throw result.errs[0];
4074
+ }
4075
+ if (compileOpts.remove !== void 0 && compileOpts.remove === true) {
4076
+ result.workbook = ExprResolver.removeUnExportSheets(result.workbook, compileOpts);
4077
+ }
4078
+ if (renderData !== void 0) {
4079
+ autoRegisterAlias(renderData, result.configure);
4080
+ }
4081
+ return await ExprResolver.toBuffer(result.workbook);
4082
+ };
4083
+
4084
+ // src/biz.ts
4085
+ var import_node_path = require("path");
4086
+ var import_lodash = require("lodash");
4087
+ var import_adm_zip = __toESM(require("adm-zip"));
4088
+ var XlsxRender = class _XlsxRender extends Workbook {
4089
+ constructor(option) {
4090
+ super(option);
4091
+ }
4092
+ static async create(data, option) {
4093
+ const w = await super.parse(data, option);
4094
+ w.setQueryFunctionHandler(commandExtendQuery);
4095
+ const app = new _XlsxRender(option);
4096
+ Object.assign(app, { ...w });
4097
+ return app;
4098
+ }
4099
+ async render(values, sheetName) {
4100
+ await this.substitute(sheetName, values);
4101
+ }
4102
+ getSheets() {
4103
+ return this.sheets;
4104
+ }
4105
+ };
4106
+ var ZipXlsxTemplateApp = class _ZipXlsxTemplateApp {
4107
+ constructor(data) {
4108
+ this.records = /* @__PURE__ */ new Map();
4109
+ this.zipBuffer = data;
4110
+ if (data !== void 0) {
4111
+ this.xlsxEntries = this.parse(data);
4112
+ }
4113
+ }
4114
+ loadZipBuffer(data) {
4115
+ this.zipBuffer = data;
4116
+ this.zip = new import_adm_zip.default(data);
4117
+ this.xlsxEntries = this.parse(data);
4118
+ return this;
4119
+ }
4120
+ parse(data) {
4121
+ const zip = new import_adm_zip.default(data);
4122
+ const result = /* @__PURE__ */ new Map();
4123
+ const entries = zip.getEntries();
4124
+ for (let fd of entries) {
4125
+ if (fd.isDirectory) {
4126
+ continue;
4127
+ }
4128
+ let ext = (0, import_node_path.extname)(fd.entryName).substring(1).toLowerCase();
4129
+ if (ext !== "xlsx") {
4130
+ continue;
4131
+ }
4132
+ result.set(fd.entryName, fd.getData());
4133
+ }
4134
+ this.zip = zip;
4135
+ return result;
4136
+ }
4137
+ getEntries() {
4138
+ if (this.xlsxEntries !== void 0 && this.xlsxEntries.size > 0) {
4139
+ return this.xlsxEntries;
4140
+ } else {
4141
+ if (this.zipBuffer !== void 0) {
4142
+ return this.parse(this.zipBuffer);
4143
+ }
4144
+ }
4145
+ return /* @__PURE__ */ new Map();
4146
+ }
4147
+ static async compileAll(files, renderData, compileOpts) {
4148
+ const records = /* @__PURE__ */ new Map();
4149
+ if (compileOpts !== void 0 && (compileOpts.sheetName === void 0 || compileOpts.sheetName === "")) {
4150
+ compileOpts.sheetName = compileRuleSheetName;
4151
+ }
4152
+ let values = (0, import_lodash.clone)(renderData);
4153
+ for (let [key, buf] of files.entries()) {
4154
+ buf = await compileAll(buf, (0, import_lodash.clone)(compileOpts), (0, import_lodash.clone)(values));
4155
+ records.set(key, buf);
4156
+ }
4157
+ return records;
4158
+ }
4159
+ async substituteAll(renderData, compileOpts, renderOpts) {
4160
+ const files = await _ZipXlsxTemplateApp.compileAll(this.xlsxEntries, renderData, compileOpts);
4161
+ for (const [k, buf] of files.entries()) {
4162
+ const xlsx = await XlsxRender.create(buf, renderOpts);
4163
+ await xlsx.substituteAll(renderData);
4164
+ this.records.set(k, xlsx);
4165
+ }
4166
+ return this;
4167
+ }
4168
+ async generate(options) {
4169
+ if (this.records === void 0 || this.records.size <= 0) {
4170
+ return this.zipBuffer;
4171
+ }
4172
+ if (this.zip === void 0) {
4173
+ this.zip = new import_adm_zip.default();
4174
+ }
4175
+ if (options === void 0 || options === null) {
4176
+ options = {
4177
+ type: "nodebuffer" /* NodeBuffer */,
4178
+ compression: "DEFLATE",
4179
+ compressionOptions: {
4180
+ level: 9
4181
+ }
4182
+ };
4183
+ }
4184
+ for (const [key, xlsx] of this.records) {
4185
+ const buf = await xlsx.generate(options);
4186
+ let entry = this.zip.getEntry(key);
4187
+ if (entry !== null) {
4188
+ entry.setData(Buffer.from(buf));
4189
+ } else {
4190
+ this.zip.addFile(key, Buffer.from(buf));
4191
+ }
4192
+ }
4193
+ return this.zip.toBuffer();
4194
+ }
4195
+ static async compileTo(data, opts, values) {
4196
+ const zip = new import_adm_zip.default(data);
4197
+ const entries = zip.getEntries();
4198
+ let files = /* @__PURE__ */ new Map();
4199
+ if (values === void 0) {
4200
+ values = /* @__PURE__ */ new Map();
4201
+ }
4202
+ for (let fd of entries) {
4203
+ if (fd.isDirectory) {
4204
+ continue;
4205
+ }
4206
+ let ext = (0, import_node_path.extname)(fd.entryName).substring(1).toLowerCase();
4207
+ if (ext !== "xlsx") {
4208
+ continue;
4209
+ }
4210
+ let buf = fd.getData();
4211
+ if (opts.checker !== void 0) {
4212
+ await opts.checker(buf, opts.options, values, opts.fileName || void 0);
4213
+ }
4214
+ files.set(fd.entryName, buf);
4215
+ }
4216
+ const compileOpts = new RuleMapOptions();
4217
+ compileOpts.remove = true;
4218
+ if (files.size > 0) {
4219
+ files = await _ZipXlsxTemplateApp.compileAll(files, values, compileOpts);
4220
+ } else {
4221
+ throw new Error(`empty xlsx file zip file`);
4222
+ }
4223
+ if (files.size > 0) {
4224
+ for (const [k, data2] of files.entries()) {
4225
+ zip.getEntry(k).setData(data2);
4226
+ }
4227
+ }
4228
+ return zip.toBuffer();
4229
+ }
4230
+ };
4064
4231
  // Annotate the CommonJS export names for ESM import in node:
4065
4232
  0 && (module.exports = {
4066
4233
  AddCommand,
@@ -4076,10 +4243,13 @@ var generateCommandsXlsxTemplateWithCompile = async function(data, values, compi
4076
4243
  RuleToken,
4077
4244
  TokenParserManger,
4078
4245
  Workbook,
4246
+ XlsxRender,
4247
+ ZipXlsxTemplateApp,
4079
4248
  autoRegisterAlias,
4080
4249
  columnLetterToNumber,
4081
4250
  columnNumberToLetter,
4082
4251
  commandExtendQuery,
4252
+ compileAll,
4083
4253
  compileRuleSheetName,
4084
4254
  compileWorkSheet,
4085
4255
  compileWorkSheetPlaceholder,