@vdhewei/xlsx-template-lib 1.6.10 → 1.6.12
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/README.md +63 -52
- package/README.zh-CN.md +69 -57
- package/dist/bin.js +2 -2
- package/dist/bin.js.map +1 -1
- package/dist/bin.mjs +3 -3
- package/dist/bin.mjs.map +1 -1
- package/dist/{chunk-NSFBG6PV.mjs → chunk-BPKRWKLJ.mjs} +20 -1
- package/dist/{chunk-NSFBG6PV.mjs.map → chunk-BPKRWKLJ.mjs.map} +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -233,7 +233,7 @@ Configure rendering rules in a rule sheet (e.g., `export.metadata.config`) with
|
|
|
233
233
|
| Rule Type | Syntax | Description |
|
|
234
234
|
|:----------|:-------|:------------|
|
|
235
235
|
| **alias** | `alias: @#key => use aliasKey: @# => @#` | Alias for field mapping |
|
|
236
|
-
| **rowCell** | `G-AP:12=compile
|
|
236
|
+
| **rowCell** | `G-AP:12=compile:GenCell(...)` | Row rule configuration |
|
|
237
237
|
| **mergeCell** | `G-AQ:13-17=sum(...)` | Merge cell calculation |
|
|
238
238
|
| **cell** | `D-7=@#[@D.MY]` | Single cell value assignment |
|
|
239
239
|
|
|
@@ -278,8 +278,8 @@ Configure row rules to assign values to cell ranges. Multiple rowCell configurat
|
|
|
278
278
|
|
|
279
279
|
| Rule | Description |
|
|
280
280
|
|:-----|:------------|
|
|
281
|
-
| `G-AP:12=compile
|
|
282
|
-
| `A-Z:5=compile
|
|
281
|
+
| `G-AP:12=compile:GenCell(@#item,[compile:Macro]#index@0)` | Assign generated values to row 12, columns G-AP |
|
|
282
|
+
| `A-Z:5=compile:Macro(@#data,2,5,!!codeKey)` | Assign formatted cell value to row 5, columns A-Z |
|
|
283
283
|
|
|
284
284
|
#### MergeCell Rules
|
|
285
285
|
|
|
@@ -445,14 +445,14 @@ Format macro output using special formatters starting with `!!`:
|
|
|
445
445
|
**Example 1: Generate CodeKey with Row Cell**
|
|
446
446
|
|
|
447
447
|
```
|
|
448
|
-
Rule: G-AQ:117=compile
|
|
448
|
+
Rule: G-AQ:117=compile:GenCell(#LT[compile:Macro]#err@F118[#codeKey],[compile:Macro]#index@0)
|
|
449
449
|
Result: errValue·1, errValue·2, errValue·3, ...
|
|
450
450
|
```
|
|
451
451
|
|
|
452
452
|
**Example 2: Format Cell Value with CodeKey**
|
|
453
453
|
|
|
454
454
|
```
|
|
455
|
-
Rule: D-7=compile
|
|
455
|
+
Rule: D-7=compile:Macro(@#[@D.MY],5,7,!!codeKey)
|
|
456
456
|
If cell(5,7) = "project-alpha-2024"
|
|
457
457
|
Result: PROJECT_ALPHA_2024
|
|
458
458
|
```
|
|
@@ -460,7 +460,7 @@ Result: PROJECT_ALPHA_2024
|
|
|
460
460
|
**Example 3: Generate CodeKeyAlias**
|
|
461
461
|
|
|
462
462
|
```
|
|
463
|
-
Rule: cell F-10=compile
|
|
463
|
+
Rule: cell F-10=compile:Macro(@#key,3,10,!!codeKeyAlias)
|
|
464
464
|
If cell(3,10) = "test..data"
|
|
465
465
|
Result: @TEST_DATA
|
|
466
466
|
```
|
|
@@ -468,11 +468,11 @@ Result: @TEST_DATA
|
|
|
468
468
|
**Example 4: Number Conversion**
|
|
469
469
|
|
|
470
470
|
```
|
|
471
|
-
Rule: row-5=compile
|
|
471
|
+
Rule: row-5=compile:Macro(@#value,2,5,!!number)
|
|
472
472
|
If cell(2,5) = "42"
|
|
473
473
|
Result: 42
|
|
474
474
|
|
|
475
|
-
Rule: row-6=compile
|
|
475
|
+
Rule: row-6=compile:Macro(@#hex,4,6,!!number)
|
|
476
476
|
If cell(4,6) = "0x1A"
|
|
477
477
|
Result: 26
|
|
478
478
|
```
|
|
@@ -496,8 +496,8 @@ LLR=exportData.LRR
|
|
|
496
496
|
CTR=contract.contractCode
|
|
497
497
|
|
|
498
498
|
# RowCell Rules (assign values to cell ranges)
|
|
499
|
-
G-AQ:12=compile
|
|
500
|
-
A-Z:5=compile
|
|
499
|
+
G-AQ:12=compile:GenCell(@#item,[compile:Macro]#index@0)
|
|
500
|
+
A-Z:5=compile:Macro(@#data,2,5,!!codeKey)
|
|
501
501
|
|
|
502
502
|
# MergeCell Rules (merge cells and apply calculations)
|
|
503
503
|
G-AQ:13-17=sum(@LT,[compile:Macro(exprArr,F,13,17,!!codeKey)],compile:Macro(index),0)
|
|
@@ -533,8 +533,8 @@ Built-in and custom functions for data processing:
|
|
|
533
533
|
import { ZipXlsxTemplateApp } from '@vdhewei/xlsx-template-lib';
|
|
534
534
|
import * as fs from 'node:fs/promises';
|
|
535
535
|
|
|
536
|
-
// Load template from buffer
|
|
537
|
-
const templateBuffer = await fs.readFile('template.
|
|
536
|
+
// Load zip template from buffer,zip file has [a.xlsx,b.xlsx...]
|
|
537
|
+
const templateBuffer = await fs.readFile('template.zip');
|
|
538
538
|
const app = new ZipXlsxTemplateApp(templateBuffer);
|
|
539
539
|
|
|
540
540
|
// Render with data
|
|
@@ -562,7 +562,7 @@ await fs.writeFile('output.xlsx', output);
|
|
|
562
562
|
```typescript
|
|
563
563
|
import { XlsxRender } from '@vdhewei/xlsx-template-lib';
|
|
564
564
|
|
|
565
|
-
const templateBuffer = await fs.readFile('template.
|
|
565
|
+
const templateBuffer = await fs.readFile('template.zip');
|
|
566
566
|
const xlsx = await XlsxRender.create(templateBuffer);
|
|
567
567
|
|
|
568
568
|
// Render a specific sheet
|
|
@@ -594,7 +594,7 @@ const compileOpts = {
|
|
|
594
594
|
remove: true // Remove rule sheet after compile
|
|
595
595
|
};
|
|
596
596
|
|
|
597
|
-
const zipBuffer = await fs.readFile('template.
|
|
597
|
+
const zipBuffer = await fs.readFile('template.zip');
|
|
598
598
|
const result = await ZipXlsxTemplateApp.compileTo(zipBuffer, {
|
|
599
599
|
checker: async (buf, opts, values, fileName) => {
|
|
600
600
|
// Custom validation logic
|
|
@@ -620,6 +620,17 @@ Or use directly from `npx`:
|
|
|
620
620
|
npx @vdhewei/xlsx-template-lib <command> [options]
|
|
621
621
|
```
|
|
622
622
|
|
|
623
|
+
Or git clone source code use bun compile to local Native CLI
|
|
624
|
+
```bash
|
|
625
|
+
git clone https://github.com/VDHewei/xlsx-template-lib.git
|
|
626
|
+
cd xlsx-template-lib
|
|
627
|
+
pnpm i
|
|
628
|
+
npm install -g bun
|
|
629
|
+
pnpm run complie-cli # default output bin/xlsx-cli
|
|
630
|
+
# or use -o compile to user local dir
|
|
631
|
+
pnpm run compile-cli -o your-path/
|
|
632
|
+
```
|
|
633
|
+
|
|
623
634
|
#### Commands
|
|
624
635
|
|
|
625
636
|
##### 1. Compile Command
|
|
@@ -642,19 +653,19 @@ xlsx-cli compile <xlsx-file> [options]
|
|
|
642
653
|
|
|
643
654
|
```bash
|
|
644
655
|
# Basic compile with default settings
|
|
645
|
-
xlsx-cli compile template.
|
|
656
|
+
xlsx-cli compile template.zip
|
|
646
657
|
|
|
647
658
|
# Compile and save to specific location
|
|
648
|
-
xlsx-cli compile template.
|
|
659
|
+
xlsx-cli compile template.zip -s ./output/
|
|
649
660
|
|
|
650
661
|
# Compile specific sheet
|
|
651
|
-
xlsx-cli compile template.
|
|
662
|
+
xlsx-cli compile template.zip -n Sheet1
|
|
652
663
|
|
|
653
664
|
# Compile and remove config sheet
|
|
654
|
-
xlsx-cli compile template.
|
|
665
|
+
xlsx-cli compile template.zip -r
|
|
655
666
|
|
|
656
667
|
# Full example
|
|
657
|
-
xlsx-cli compile template.
|
|
668
|
+
xlsx-cli compile template.zip -s ./output/ -n Sheet1 -r
|
|
658
669
|
```
|
|
659
670
|
|
|
660
671
|
**Output:**
|
|
@@ -685,34 +696,34 @@ xlsx-cli render <xlsx-file> [options]
|
|
|
685
696
|
|
|
686
697
|
```bash
|
|
687
698
|
# Basic render with empty data
|
|
688
|
-
xlsx-cli render template.
|
|
699
|
+
xlsx-cli render template.zip
|
|
689
700
|
|
|
690
701
|
# Render with JSON data string
|
|
691
|
-
xlsx-cli render template.
|
|
702
|
+
xlsx-cli render template.zip -d '{"name":"John","age":30}'
|
|
692
703
|
|
|
693
704
|
# Render with JSON file
|
|
694
|
-
xlsx-cli render template.
|
|
705
|
+
xlsx-cli render template.zip -d ./data.json
|
|
695
706
|
|
|
696
707
|
# Render with remote JSON URL
|
|
697
|
-
xlsx-cli render template.
|
|
708
|
+
xlsx-cli render template.zip -d 'https://api.example.com/data.json'
|
|
698
709
|
|
|
699
710
|
# Render with auto-compile
|
|
700
|
-
xlsx-cli render template.
|
|
711
|
+
xlsx-cli render template.zip -c -d './data.json'
|
|
701
712
|
|
|
702
713
|
# Render specific sheet
|
|
703
|
-
xlsx-cli render template.
|
|
714
|
+
xlsx-cli render template.zip -n Sheet1 -d './data.json'
|
|
704
715
|
|
|
705
716
|
# Render with custom HTTP headers
|
|
706
|
-
xlsx-cli render template.
|
|
717
|
+
xlsx-cli render template.zip -d 'https://api.example.com/data.json' --header 'Authorization:Bearer token123' --header 'Content-Type:application/json'
|
|
707
718
|
|
|
708
719
|
# Render with POST request body
|
|
709
|
-
xlsx-cli render template.
|
|
720
|
+
xlsx-cli render template.zip -d 'https://api.example.com/api/query' --body '{"query":"SELECT * FROM users"}' --header 'Content-Type:application/json'
|
|
710
721
|
|
|
711
722
|
# Render with POST method via header
|
|
712
|
-
xlsx-cli render template.
|
|
723
|
+
xlsx-cli render template.zip -d 'https://api.example.com/api/create' --body '{"name":"Test"}' --header 'Content-Type:application/json' --header 'method:POST'
|
|
713
724
|
|
|
714
725
|
# Full example
|
|
715
|
-
xlsx-cli render template.
|
|
726
|
+
xlsx-cli render template.zip -c -n Sheet1 -s ./output/ -d './data.json'
|
|
716
727
|
```
|
|
717
728
|
|
|
718
729
|
**Data Sources:**
|
|
@@ -734,26 +745,26 @@ xlsx-cli render template.xlsx -c -n Sheet1 -s ./output/ -d './data.json'
|
|
|
734
745
|
|
|
735
746
|
```bash
|
|
736
747
|
# GET request with custom headers
|
|
737
|
-
xlsx-cli render template.
|
|
748
|
+
xlsx-cli render template.zip \
|
|
738
749
|
-d 'https://api.example.com/data.json' \
|
|
739
750
|
--header 'Authorization:Bearer your-token' \
|
|
740
751
|
--header 'X-API-Key:api-key-123'
|
|
741
752
|
|
|
742
753
|
# POST request with JSON body
|
|
743
|
-
xlsx-cli render template.
|
|
754
|
+
xlsx-cli render template.zip \
|
|
744
755
|
-d 'https://api.example.com/api/query' \
|
|
745
756
|
--body '{"query":"SELECT * FROM users LIMIT 10"}' \
|
|
746
757
|
--header 'Content-Type:application/json'
|
|
747
758
|
|
|
748
759
|
# POST request with method specified in header
|
|
749
|
-
xlsx-cli render template.
|
|
760
|
+
xlsx-cli render template.zip \
|
|
750
761
|
-d 'https://api.example.com/api/create' \
|
|
751
762
|
--body '{"name":"New Record","value":100}' \
|
|
752
763
|
--header 'Content-Type:application/json' \
|
|
753
764
|
--header 'method:POST'
|
|
754
765
|
|
|
755
766
|
# Complex example with authentication and query body
|
|
756
|
-
xlsx-cli render template.
|
|
767
|
+
xlsx-cli render template.zip \
|
|
757
768
|
-d 'https://api.example.com/v1/export' \
|
|
758
769
|
--header 'Authorization:Bearer eyJhbGc...' \
|
|
759
770
|
--header 'Content-Type:application/json' \
|
|
@@ -820,28 +831,28 @@ xlsx-cli rules <xlsx-file> [options]
|
|
|
820
831
|
**Single Rule (Command Line):**
|
|
821
832
|
```bash
|
|
822
833
|
# Add alias rule
|
|
823
|
-
xlsx-cli rules template.
|
|
834
|
+
xlsx-cli rules template.zip -t alias -r 'T=template'
|
|
824
835
|
|
|
825
836
|
# Add cell rule
|
|
826
|
-
xlsx-cli rules template.
|
|
837
|
+
xlsx-cli rules template.zip -t cell -r 'D:7=${@LLR.value}'
|
|
827
838
|
|
|
828
839
|
# Add rowCell rule
|
|
829
|
-
xlsx-cli rules template.
|
|
840
|
+
xlsx-cli rules template.zip -t rowCell -r 'G-AQ:12=compile:GenCell(@#item,[compile:Macro]#index@0)'
|
|
830
841
|
|
|
831
842
|
# Add mergeCell rule
|
|
832
|
-
xlsx-cli rules template.
|
|
843
|
+
xlsx-cli rules template.zip -t mergeCell -r 'G-AQ:13-17=sum(@LT,[compile:Macro(exprArr,F,13,17,!!codeKey)],compile:Macro(index),0)'
|
|
833
844
|
```
|
|
834
845
|
|
|
835
846
|
**Multiple Rules (Command Line):**
|
|
836
847
|
```bash
|
|
837
848
|
# Add multiple rules with same type
|
|
838
|
-
xlsx-cli rules template.
|
|
849
|
+
xlsx-cli rules template.zip -t cell -r 'D:7=${@LLR.value}' -r 'A:1=${@T}' -r 'B:1=${@LLR.value}'
|
|
839
850
|
```
|
|
840
851
|
|
|
841
852
|
**Rules from File:**
|
|
842
853
|
```bash
|
|
843
854
|
# Read rules from file
|
|
844
|
-
xlsx-cli rules template.
|
|
855
|
+
xlsx-cli rules template.zip -f rules.txt
|
|
845
856
|
|
|
846
857
|
# Create rules.txt file:
|
|
847
858
|
# This is a comment
|
|
@@ -849,14 +860,14 @@ alias T=template
|
|
|
849
860
|
alias LLR=exportData.LRR
|
|
850
861
|
cell D:7=${@T}
|
|
851
862
|
cell A:1=${@LLR.value}
|
|
852
|
-
rowCell G-AQ:12=compile
|
|
863
|
+
rowCell G-AQ:12=compile:GenCell(@#item,[compile:Macro]#index@0)
|
|
853
864
|
mergeCell G-AQ:13-17=sum(@LT,[compile:Macro(exprArr,F,13,17,!!codeKey)],compile:Macro(index),0)
|
|
854
865
|
```
|
|
855
866
|
|
|
856
867
|
**Save to Specific Directory:**
|
|
857
868
|
```bash
|
|
858
|
-
xlsx-cli rules template.
|
|
859
|
-
xlsx-cli rules template.
|
|
869
|
+
xlsx-cli rules template.zip -f rules.txt -s ./output/
|
|
870
|
+
xlsx-cli rules template.zip -t cell -r 'D:7=${@LLR.value}' -s ./output/
|
|
860
871
|
```
|
|
861
872
|
|
|
862
873
|
**File Format (-f mode):**
|
|
@@ -867,7 +878,7 @@ xlsx-cli rules template.xlsx -t cell -r 'D:7=${@LLR.value}' -s ./output/
|
|
|
867
878
|
|
|
868
879
|
cell D:7=${@LLR.value}
|
|
869
880
|
alias T=template
|
|
870
|
-
rowCell G-AQ:12=compile
|
|
881
|
+
rowCell G-AQ:12=compile:GenCell(@#item,[compile:Macro]#index@0)
|
|
871
882
|
mergeCell G-AQ:13-17=sum(@LT,[compile:Macro(exprArr,F,13,17,!!codeKey)],compile:Macro(index),0)
|
|
872
883
|
```
|
|
873
884
|
|
|
@@ -921,7 +932,7 @@ AddCommand('multiply', (obj, args) => {
|
|
|
921
932
|
const values = args.groups.map(g => valueDotGet(obj, g));
|
|
922
933
|
return values.reduce((a, b) => a * b, 1);
|
|
923
934
|
});
|
|
924
|
-
|
|
935
|
+
const data = await fs.readFile('simple.xlsx');
|
|
925
936
|
// Generate template with custom commands
|
|
926
937
|
const buffer = await generateCommandsXlsxTemplate(data, options);
|
|
927
938
|
```
|
|
@@ -989,7 +1000,7 @@ Main class for rendering single Excel files.
|
|
|
989
1000
|
### Template Structure
|
|
990
1001
|
|
|
991
1002
|
```
|
|
992
|
-
template.
|
|
1003
|
+
template.zip
|
|
993
1004
|
├── Sheet1 (Data Sheet with placeholders)
|
|
994
1005
|
│ ├── A1: ${contract.contractCode}
|
|
995
1006
|
│ ├── B1: ${contract.contractTitle}
|
|
@@ -1002,12 +1013,12 @@ template.xlsx
|
|
|
1002
1013
|
|
|
1003
1014
|
### Compile & Render Flow
|
|
1004
1015
|
|
|
1005
|
-
| Step | Input
|
|
1006
|
-
|
|
1007
|
-
| 1. Load | `template.
|
|
1008
|
-
| 2. Compile | Rule Config Sheet
|
|
1009
|
-
| 3. Substitute | Data Object
|
|
1010
|
-
| 4. Generate | -
|
|
1016
|
+
| Step | Input | Output | Description |
|
|
1017
|
+
|:-----|:----------------------|:-------|:------------|
|
|
1018
|
+
| 1. Load | `template.zip` Buffer | `ZipXlsxTemplateApp` | Load template file |
|
|
1019
|
+
| 2. Compile | Rule Config Sheet | Compiled Rules | Parse mergeCell/cell/rowCell rules |
|
|
1020
|
+
| 3. Substitute | Data Object | Rendered Sheets | Replace `${...}` placeholders |
|
|
1021
|
+
| 4. Generate | - | `output.xlsx` Buffer | Final output file |
|
|
1011
1022
|
|
|
1012
1023
|
```typescript
|
|
1013
1024
|
import { ZipXlsxTemplateApp, AddCommand } from '@vdhewei/xlsx-template-lib';
|
|
@@ -1022,7 +1033,7 @@ AddCommand('calculateTotal', (obj, args) => {
|
|
|
1022
1033
|
|
|
1023
1034
|
// Main processing
|
|
1024
1035
|
async function processTemplate() {
|
|
1025
|
-
const templateBuffer = await fs.readFile('template.
|
|
1036
|
+
const templateBuffer = await fs.readFile('template.zip');
|
|
1026
1037
|
|
|
1027
1038
|
const app = new ZipXlsxTemplateApp(templateBuffer);
|
|
1028
1039
|
|
package/README.zh-CN.md
CHANGED
|
@@ -231,7 +231,7 @@ A B
|
|
|
231
231
|
| 规则类型 | 语法 | 说明 |
|
|
232
232
|
|:---------|:-----|:-----|
|
|
233
233
|
| **alias** | `alias: @#key => use aliasKey: @# => @#` | 字段别名映射 |
|
|
234
|
-
| **rowCell** | `G-AP:12=compile
|
|
234
|
+
| **rowCell** | `G-AP:12=compile:GenCell(...)` | 行规则配置 |
|
|
235
235
|
| **mergeCell** | `G-AQ:13-17=sum(...)` | 合并单元格计算 |
|
|
236
236
|
| **cell** | `D-7=@#[@D.MY]` | 单个单元格值赋值 |
|
|
237
237
|
|
|
@@ -274,10 +274,10 @@ A B
|
|
|
274
274
|
|
|
275
275
|
**示例:**
|
|
276
276
|
|
|
277
|
-
| 规则
|
|
278
|
-
|
|
279
|
-
| `G-AP:12=compile
|
|
280
|
-
| `A-Z:5=compile
|
|
277
|
+
| 规则 | 说明 |
|
|
278
|
+
|:----------------------------------------------------------|:-----|
|
|
279
|
+
| `G-AP:12=compile:GenCell(@#item,[compile:Macro]#index@0)` | 为第 12 行的 G-AP 列赋值生成的值 |
|
|
280
|
+
| `A-Z:5=compile:Macro(@#data,2,5,!!codeKey)` | 为第 5 行的 A-Z 列赋值格式化后的单元格值 |
|
|
281
281
|
|
|
282
282
|
#### MergeCell 规则
|
|
283
283
|
|
|
@@ -443,14 +443,14 @@ sub(orders,[money,food,game],1,0)
|
|
|
443
443
|
**示例 1: 使用行单元格生成 CodeKey**
|
|
444
444
|
|
|
445
445
|
```
|
|
446
|
-
规则: G-AQ:117=compile
|
|
446
|
+
规则: G-AQ:117=compile:GenCell(#LT,compile:Macro(index),0)
|
|
447
447
|
结果: errValue·1, errValue·2, errValue·3, ...
|
|
448
448
|
```
|
|
449
449
|
|
|
450
450
|
**示例 2: 使用 CodeKey 格式化单元格值**
|
|
451
451
|
|
|
452
452
|
```
|
|
453
|
-
规则: D-7=compile
|
|
453
|
+
规则: D-7=compile:Macro(expr,5,7,!!codeKey)
|
|
454
454
|
如果 cell(5,7) = "project-alpha-2024"
|
|
455
455
|
结果: PROJECT_ALPHA_2024
|
|
456
456
|
```
|
|
@@ -458,7 +458,7 @@ sub(orders,[money,food,game],1,0)
|
|
|
458
458
|
**示例 3: 生成 CodeKeyAlias**
|
|
459
459
|
|
|
460
460
|
```
|
|
461
|
-
规则: cell F-10=compile
|
|
461
|
+
规则: cell F-10=compile:Marco(expr,3,10,!!codeKeyAlias)
|
|
462
462
|
如果 cell(3,10) = "test..data"
|
|
463
463
|
结果: @TEST_DATA
|
|
464
464
|
```
|
|
@@ -466,11 +466,11 @@ sub(orders,[money,food,game],1,0)
|
|
|
466
466
|
**示例 4: 数字转换**
|
|
467
467
|
|
|
468
468
|
```
|
|
469
|
-
规则: row-5=compile
|
|
469
|
+
规则: row-5=compile:Macro(expr,2,5,!!number)
|
|
470
470
|
如果 cell(2,5) = "42"
|
|
471
471
|
结果: 42
|
|
472
472
|
|
|
473
|
-
规则: row-6=compile
|
|
473
|
+
规则: row-6=compile:Macro(expr,4,6,!!number)
|
|
474
474
|
如果 cell(4,6) = "0x1A"
|
|
475
475
|
结果: 26
|
|
476
476
|
```
|
|
@@ -478,9 +478,9 @@ sub(orders,[money,food,game],1,0)
|
|
|
478
478
|
**示例 5: 使用 Index 迭代**
|
|
479
479
|
|
|
480
480
|
```
|
|
481
|
-
第 1 行: Code
|
|
482
|
-
第 2 行: Code
|
|
483
|
-
第 3 行: Code
|
|
481
|
+
第 1 行: Code-compile:Macro(index) → Code-1
|
|
482
|
+
第 2 行: Code-compile:Macro(index) → Code-2
|
|
483
|
+
第 3 行: Code-compile:Macro(index) → Code-3
|
|
484
484
|
```
|
|
485
485
|
|
|
486
486
|
#### 完整规则配置示例
|
|
@@ -494,8 +494,8 @@ LLR=exportData.LRR
|
|
|
494
494
|
CTR=contract.contractCode
|
|
495
495
|
|
|
496
496
|
# RowCell 规则(为单元格范围赋值)
|
|
497
|
-
G-AQ:12=compile
|
|
498
|
-
A-Z:5=compile
|
|
497
|
+
G-AQ:12=compile:GenCell(@#item,G,compile:Macro(index),0)
|
|
498
|
+
A-Z:5=compile:Macro(@#data,2,5,!!codeKey)
|
|
499
499
|
|
|
500
500
|
# MergeCell 规则(合并单元格并应用计算)
|
|
501
501
|
G-AQ:13-17=sum(@LT,[compile:Macro(exprArr,F,13,17,!!codeKey)],compile:Macro(index),0)
|
|
@@ -531,8 +531,8 @@ B:1=${@LLR.value}
|
|
|
531
531
|
import { ZipXlsxTemplateApp } from '@vdhewei/xlsx-template-lib';
|
|
532
532
|
import * as fs from 'node:fs/promises';
|
|
533
533
|
|
|
534
|
-
// 从 buffer 加载模板
|
|
535
|
-
const templateBuffer = await fs.readFile('template.
|
|
534
|
+
// 从 zip buffer 加载模板 zip 文件有 : [a.xlsx,b.xlsx...]
|
|
535
|
+
const templateBuffer = await fs.readFile('template.zip');
|
|
536
536
|
const app = new ZipXlsxTemplateApp(templateBuffer);
|
|
537
537
|
|
|
538
538
|
// 使用数据渲染
|
|
@@ -560,7 +560,7 @@ await fs.writeFile('output.xlsx', output);
|
|
|
560
560
|
```typescript
|
|
561
561
|
import { XlsxRender } from '@vdhewei/xlsx-template-lib';
|
|
562
562
|
|
|
563
|
-
const templateBuffer = await fs.readFile('template.
|
|
563
|
+
const templateBuffer = await fs.readFile('template.zip');
|
|
564
564
|
const xlsx = await XlsxRender.create(templateBuffer);
|
|
565
565
|
|
|
566
566
|
// 渲染特定工作表
|
|
@@ -592,7 +592,7 @@ const compileOpts = {
|
|
|
592
592
|
remove: true // 编译后移除规则工作表
|
|
593
593
|
};
|
|
594
594
|
|
|
595
|
-
const zipBuffer = await fs.readFile('template.
|
|
595
|
+
const zipBuffer = await fs.readFile('template.zip');
|
|
596
596
|
const result = await ZipXlsxTemplateApp.compileTo(zipBuffer, {
|
|
597
597
|
checker: async (buf, opts, values, fileName) => {
|
|
598
598
|
// 自定义验证逻辑
|
|
@@ -618,6 +618,17 @@ npm install -g @vdhewei/xlsx-template-lib
|
|
|
618
618
|
npx @vdhewei/xlsx-template-lib <命令> [选项]
|
|
619
619
|
```
|
|
620
620
|
|
|
621
|
+
或者 直接下载源码使用 Bun 将源码编译成本地二进制CLI
|
|
622
|
+
```bash
|
|
623
|
+
git clone https://github.com/VDHewei/xlsx-template-lib.git
|
|
624
|
+
cd xlsx-template-lib
|
|
625
|
+
pnpm i
|
|
626
|
+
npm install -g bun
|
|
627
|
+
pnpm run complie-cli # 默认编译产物在当前 bin/目录下 (windows:xlsx-cli.exe ,other:xlsx-cli )
|
|
628
|
+
# 或者用户指定编译输出目录 (-o 或者 --output-dir)
|
|
629
|
+
pnpm run compile-cli -o your-path/
|
|
630
|
+
```
|
|
631
|
+
|
|
621
632
|
#### 命令
|
|
622
633
|
|
|
623
634
|
##### 1. compile 命令
|
|
@@ -640,19 +651,19 @@ xlsx-cli compile <xlsx-文件> [选项]
|
|
|
640
651
|
|
|
641
652
|
```bash
|
|
642
653
|
# 使用默认设置编译
|
|
643
|
-
xlsx-cli compile template.
|
|
654
|
+
xlsx-cli compile template.zip
|
|
644
655
|
|
|
645
656
|
# 编译并保存到指定位置
|
|
646
|
-
xlsx-cli compile template.
|
|
657
|
+
xlsx-cli compile template.zip -s ./output/
|
|
647
658
|
|
|
648
659
|
# 编译指定工作表
|
|
649
|
-
xlsx-cli compile template.
|
|
660
|
+
xlsx-cli compile template.zip -n Sheet1
|
|
650
661
|
|
|
651
662
|
# 编译并移除配置工作表
|
|
652
|
-
xlsx-cli compile template.
|
|
663
|
+
xlsx-cli compile template.zip -r
|
|
653
664
|
|
|
654
665
|
# 完整示例
|
|
655
|
-
xlsx-cli compile template.
|
|
666
|
+
xlsx-cli compile template.zip -s ./output/ -n Sheet1 -r
|
|
656
667
|
```
|
|
657
668
|
|
|
658
669
|
**输出:**
|
|
@@ -683,34 +694,34 @@ xlsx-cli render <xlsx-文件> [选项]
|
|
|
683
694
|
|
|
684
695
|
```bash
|
|
685
696
|
# 使用空数据基本渲染
|
|
686
|
-
xlsx-cli render template.
|
|
697
|
+
xlsx-cli render template.zip
|
|
687
698
|
|
|
688
699
|
# 使用 JSON 字符串渲染
|
|
689
|
-
xlsx-cli render template.
|
|
700
|
+
xlsx-cli render template.zip -d '{"name":"张三","age":30}'
|
|
690
701
|
|
|
691
702
|
# 使用 JSON 文件渲染
|
|
692
|
-
xlsx-cli render template.
|
|
703
|
+
xlsx-cli render template.zip -d ./data.json
|
|
693
704
|
|
|
694
705
|
# 使用远程 JSON URL 渲染
|
|
695
|
-
xlsx-cli render template.
|
|
706
|
+
xlsx-cli render template.zip -d 'https://api.example.com/data.json'
|
|
696
707
|
|
|
697
708
|
# 渲染并自动编译
|
|
698
|
-
xlsx-cli render template.
|
|
709
|
+
xlsx-cli render template.zip -c -d './data.json'
|
|
699
710
|
|
|
700
711
|
# 渲染指定工作表
|
|
701
|
-
xlsx-cli render template.
|
|
712
|
+
xlsx-cli render template.zip -n Sheet1 -d './data.json'
|
|
702
713
|
|
|
703
714
|
# 使用自定义 HTTP 请求头渲染
|
|
704
|
-
xlsx-cli render template.
|
|
715
|
+
xlsx-cli render template.zip -d 'https://api.example.com/data.json' --header 'Authorization:Bearer token123' --header 'Content-Type:application/json'
|
|
705
716
|
|
|
706
717
|
# 使用 POST 请求体渲染
|
|
707
|
-
xlsx-cli render template.
|
|
718
|
+
xlsx-cli render template.zip -d 'https://api.example.com/api/query' --body '{"query":"SELECT * FROM users"}' --header 'Content-Type:application/json'
|
|
708
719
|
|
|
709
720
|
# 通过 header 指定 POST 方法
|
|
710
|
-
xlsx-cli render template.
|
|
721
|
+
xlsx-cli render template.zip -d 'https://api.example.com/api/create' --body '{"name":"测试"}' --header 'Content-Type:application/json' --header 'method:POST'
|
|
711
722
|
|
|
712
723
|
# 完整示例
|
|
713
|
-
xlsx-cli render template.
|
|
724
|
+
xlsx-cli render template.zip -c -n Sheet1 -s ./output/ -d './data.json'
|
|
714
725
|
```
|
|
715
726
|
|
|
716
727
|
**数据源:**
|
|
@@ -732,26 +743,26 @@ xlsx-cli render template.xlsx -c -n Sheet1 -s ./output/ -d './data.json'
|
|
|
732
743
|
|
|
733
744
|
```bash
|
|
734
745
|
# 带自定义请求头的 GET 请求
|
|
735
|
-
xlsx-cli render template.
|
|
746
|
+
xlsx-cli render template.zip \
|
|
736
747
|
-d 'https://api.example.com/data.json' \
|
|
737
748
|
--header 'Authorization:Bearer your-token' \
|
|
738
749
|
--header 'X-API-Key:api-key-123'
|
|
739
750
|
|
|
740
751
|
# 带 JSON 请求体的 POST 请求
|
|
741
|
-
xlsx-cli render template.
|
|
752
|
+
xlsx-cli render template.zip \
|
|
742
753
|
-d 'https://api.example.com/api/query' \
|
|
743
754
|
--body '{"query":"SELECT * FROM users LIMIT 10"}' \
|
|
744
755
|
--header 'Content-Type:application/json'
|
|
745
756
|
|
|
746
757
|
# 通过 header 指定 POST 方法
|
|
747
|
-
xlsx-cli render template.
|
|
758
|
+
xlsx-cli render template.zip \
|
|
748
759
|
-d 'https://api.example.com/api/create' \
|
|
749
760
|
--body '{"name":"新记录","value":100}' \
|
|
750
761
|
--header 'Content-Type:application/json' \
|
|
751
762
|
--header 'method:POST'
|
|
752
763
|
|
|
753
764
|
# 复杂示例:带认证和查询请求体
|
|
754
|
-
xlsx-cli render template.
|
|
765
|
+
xlsx-cli render template.zip \
|
|
755
766
|
-d 'https://api.example.com/v1/export' \
|
|
756
767
|
--header 'Authorization:Bearer eyJhbGc...' \
|
|
757
768
|
--header 'Content-Type:application/json' \
|
|
@@ -818,28 +829,28 @@ xlsx-cli rules <xlsx-文件> [选项]
|
|
|
818
829
|
**单个规则(命令行):**
|
|
819
830
|
```bash
|
|
820
831
|
# 添加 alias 规则
|
|
821
|
-
xlsx-cli rules template.
|
|
832
|
+
xlsx-cli rules template.zip -t alias -r 'T=template'
|
|
822
833
|
|
|
823
834
|
# 添加 cell 规则
|
|
824
|
-
xlsx-cli rules template.
|
|
835
|
+
xlsx-cli rules template.zip -t cell -r 'D:7=${@LLR.value}'
|
|
825
836
|
|
|
826
837
|
# 添加 rowCell 规则
|
|
827
|
-
xlsx-cli rules template.
|
|
838
|
+
xlsx-cli rules template.zip -t rowCell -r 'G-AQ:12=compile:GenCell(@#item,[compile:Macro]#index@0)'
|
|
828
839
|
|
|
829
840
|
# 添加 mergeCell 规则
|
|
830
|
-
xlsx-cli rules template.
|
|
841
|
+
xlsx-cli rules template.zip -t mergeCell -r 'G-AQ:13-17=sum(@LT,[compile:Macro(exprArr,F,13,17,!!codeKey)],compile:Macro(index),0)'
|
|
831
842
|
```
|
|
832
843
|
|
|
833
844
|
**多个规则(命令行):**
|
|
834
845
|
```bash
|
|
835
846
|
# 添加同类型的多个规则
|
|
836
|
-
xlsx-cli rules template.
|
|
847
|
+
xlsx-cli rules template.zip -t cell -r 'D:7=${@LLR.value}' -r 'A:1=${@T}' -r 'B:1=${@LLR.value}'
|
|
837
848
|
```
|
|
838
849
|
|
|
839
850
|
**从文件读取规则:**
|
|
840
851
|
```bash
|
|
841
852
|
# 从文件读取规则
|
|
842
|
-
xlsx-cli rules template.
|
|
853
|
+
xlsx-cli rules template.zip -f rules.txt
|
|
843
854
|
|
|
844
855
|
# 创建 rules.txt 文件:
|
|
845
856
|
# 这是注释行
|
|
@@ -847,14 +858,14 @@ alias T=template
|
|
|
847
858
|
alias LLR=exportData.LRR
|
|
848
859
|
cell D:7=${@T}
|
|
849
860
|
cell A:1=${@LLR.value}
|
|
850
|
-
rowCell G-AQ:12=compile
|
|
861
|
+
rowCell G-AQ:12=compile:GenCell(@#item,[compile:Macro]#index@0)
|
|
851
862
|
mergeCell G-AQ:13-17=sum(@LT,[compile:Macro(exprArr,F,13,17,!!codeKey)],compile:Macro(index),0)
|
|
852
863
|
```
|
|
853
864
|
|
|
854
865
|
**保存到指定目录:**
|
|
855
866
|
```bash
|
|
856
|
-
xlsx-cli rules template.
|
|
857
|
-
xlsx-cli rules template.
|
|
867
|
+
xlsx-cli rules template.zip -f rules.txt -s ./output/
|
|
868
|
+
xlsx-cli rules template.zip -t cell -r 'D:7=${@LLR.value}' -s ./output/
|
|
858
869
|
```
|
|
859
870
|
|
|
860
871
|
**文件格式(-f 模式):**
|
|
@@ -865,7 +876,7 @@ xlsx-cli rules template.xlsx -t cell -r 'D:7=${@LLR.value}' -s ./output/
|
|
|
865
876
|
|
|
866
877
|
cell D:7=${@LLR.value}
|
|
867
878
|
alias T=template
|
|
868
|
-
rowCell G-AQ:12=compile
|
|
879
|
+
rowCell G-AQ:12=compile:GenCell(@#item,[compile:Macro]#index@0)
|
|
869
880
|
mergeCell G-AQ:13-17=sum(@LT,[compile:Macro(exprArr,F,13,17,!!codeKey)],compile:Macro(index),0)
|
|
870
881
|
```
|
|
871
882
|
|
|
@@ -920,7 +931,8 @@ AddCommand('multiply', (obj, args) => {
|
|
|
920
931
|
const values = args.groups.map(g => valueDotGet(obj, g));
|
|
921
932
|
return values.reduce((a, b) => a * b, 1);
|
|
922
933
|
});
|
|
923
|
-
|
|
934
|
+
// 读取xlsx模板
|
|
935
|
+
const data = await fs.readFile('simple.xlsx');
|
|
924
936
|
// 使用自定义命令生成模板
|
|
925
937
|
const buffer = await generateCommandsXlsxTemplate(data, options);
|
|
926
938
|
```
|
|
@@ -988,7 +1000,7 @@ const output = await app.generate();
|
|
|
988
1000
|
### 模板结构
|
|
989
1001
|
|
|
990
1002
|
```
|
|
991
|
-
template.
|
|
1003
|
+
template.zip
|
|
992
1004
|
├── Sheet1 (数据工作表,包含占位符)
|
|
993
1005
|
│ ├── A1: ${contract.contractCode}
|
|
994
1006
|
│ ├── B1: ${contract.contractTitle}
|
|
@@ -1001,12 +1013,12 @@ template.xlsx
|
|
|
1001
1013
|
|
|
1002
1014
|
### 编译与渲染流程
|
|
1003
1015
|
|
|
1004
|
-
| 步骤 | 输入
|
|
1005
|
-
|
|
1006
|
-
| 1. 加载 | `template.
|
|
1007
|
-
| 2. 编译 | 规则配置工作表
|
|
1008
|
-
| 3. 替换 | 数据对象
|
|
1009
|
-
| 4. 生成 | -
|
|
1016
|
+
| 步骤 | 输入 | 输出 | 说明 |
|
|
1017
|
+
|:-----|:----------------------|:-----|:-----|
|
|
1018
|
+
| 1. 加载 | `template.zip` Buffer | `ZipXlsxTemplateApp` | 加载模板文件 |
|
|
1019
|
+
| 2. 编译 | 规则配置工作表 | 编译后的规则 | 解析 mergeCell/cell/rowCell 规则 |
|
|
1020
|
+
| 3. 替换 | 数据对象 | 渲染后的工作表 | 替换 `${...}` 占位符 |
|
|
1021
|
+
| 4. 生成 | - | `output.xlsx` Buffer | 最终输出文件 |
|
|
1010
1022
|
|
|
1011
1023
|
```typescript
|
|
1012
1024
|
import { ZipXlsxTemplateApp, AddCommand } from '@vdhewei/xlsx-template-lib';
|
|
@@ -1021,7 +1033,7 @@ AddCommand('calculateTotal', (obj, args) => {
|
|
|
1021
1033
|
|
|
1022
1034
|
// 主处理流程
|
|
1023
1035
|
async function processTemplate() {
|
|
1024
|
-
const templateBuffer = await fs.readFile('template.
|
|
1036
|
+
const templateBuffer = await fs.readFile('template.zip');
|
|
1025
1037
|
|
|
1026
1038
|
const app = new ZipXlsxTemplateApp(templateBuffer);
|
|
1027
1039
|
|