@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/bin.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  ExprResolver
4
- } from "./chunk-2UQSPJKC.mjs";
4
+ } from "./chunk-TC2FHOAF.mjs";
5
5
 
6
6
  // src/bin.ts
7
7
  import chalk from "chalk";
@@ -3982,6 +3982,170 @@ var generateCommandsXlsxTemplateWithCompile = async function(data, values, compi
3982
3982
  await w.substituteAll(values);
3983
3983
  return w.generate(options);
3984
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
+ };
3985
4149
 
3986
4150
  export {
3987
4151
  BufferType,
@@ -4027,6 +4191,9 @@ export {
4027
4191
  compileRuleSheetName,
4028
4192
  mergeMap,
4029
4193
  autoRegisterAlias,
4030
- generateCommandsXlsxTemplateWithCompile
4194
+ generateCommandsXlsxTemplateWithCompile,
4195
+ compileAll,
4196
+ XlsxRender,
4197
+ ZipXlsxTemplateApp
4031
4198
  };
4032
- //# sourceMappingURL=chunk-2UQSPJKC.mjs.map
4199
+ //# sourceMappingURL=chunk-TC2FHOAF.mjs.map