@json-table/xlsx 0.3.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/README.md +30 -0
- package/dist/tree-to-xlsx.d.ts +33 -0
- package/dist/tree-to-xlsx.d.ts.map +1 -0
- package/dist/tree-to-xlsx.js +66 -0
- package/dist/tree-to-xlsx.js.map +1 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @json-table/xlsx
|
|
2
|
+
|
|
3
|
+
The [exceljs](https://github.com/exceljs/exceljs) based XLSX renderer for [JSON Table](https://github.com/x0k/json-table) trees.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm install @json-table/core @json-table/xlsx
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { makeTreeFactory } from "@json-table/core";
|
|
15
|
+
import { renderOnWorksheet } from "@json-table/xlsx";
|
|
16
|
+
import { Workbook } from "exceljs";
|
|
17
|
+
|
|
18
|
+
const createTree = makeTreeFactory({
|
|
19
|
+
cornerCellValue: "№",
|
|
20
|
+
createHeader: (key) => key,
|
|
21
|
+
createIndex: (i) => i + 1,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const wb = new Workbook();
|
|
25
|
+
renderOnWorksheet(wb.addWorksheet("Table"), createTree(data));
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## License
|
|
29
|
+
|
|
30
|
+
MIT
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Cell as ExcelCell, type Column, type Row, type Worksheet } from "exceljs";
|
|
2
|
+
import type { JSONPrimitive } from "@json-table/core/lib/json";
|
|
3
|
+
import { type Tree } from "@json-table/core";
|
|
4
|
+
type SheetCellType = "header" | "index" | "corner" | "leaf";
|
|
5
|
+
export interface SheetCell {
|
|
6
|
+
height: number;
|
|
7
|
+
width: number;
|
|
8
|
+
type: SheetCellType;
|
|
9
|
+
value: {
|
|
10
|
+
value: JSONPrimitive;
|
|
11
|
+
col: number;
|
|
12
|
+
row: number;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export interface CalculateSheetDataOptions<V> {
|
|
16
|
+
columnWidth: (counts: number[], columnIndex: number, tree: Tree<V>) => number;
|
|
17
|
+
rowHeight: (counts: number[], rowIndex: number, tree: Tree<V>) => number;
|
|
18
|
+
}
|
|
19
|
+
export declare function calculateSheetData<V>(tree: Tree<V>, { columnWidth, rowHeight }: CalculateSheetDataOptions<V>): {
|
|
20
|
+
widths: number[];
|
|
21
|
+
heights: number[];
|
|
22
|
+
cells: SheetCell[];
|
|
23
|
+
};
|
|
24
|
+
export type MakeWorkBookOptions<V> = Partial<CalculateSheetDataOptions<V> & {
|
|
25
|
+
cellMinHeight: number;
|
|
26
|
+
cellMinWidth: number;
|
|
27
|
+
modifyColumn: (column: Column, columnIndex: number) => void;
|
|
28
|
+
modifyRow: (row: Row, rowIndex: number) => void;
|
|
29
|
+
modifyCell: (sheetCell: ExcelCell, cell: SheetCell, cellIndex: number) => void;
|
|
30
|
+
}>;
|
|
31
|
+
export declare function renderOnWorksheet<V>(sheet: Worksheet, tree: Tree<V>, { cellMinHeight, cellMinWidth, modifyCell, modifyColumn, modifyRow, ...options }?: MakeWorkBookOptions<V>): void;
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=tree-to-xlsx.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tree-to-xlsx.d.ts","sourceRoot":"","sources":["../src/tree-to-xlsx.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,IAAI,IAAI,SAAS,EACtB,KAAK,MAAM,EACX,KAAK,GAAG,EACR,KAAK,SAAS,EACf,MAAM,SAAS,CAAC;AAIjB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,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KACd,IAAI,CAAC;CACX,CACF,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,KAAK,EAAE,SAAS,EAChB,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,EACb,EACE,aAAkB,EAClB,YAAiB,EACjB,UAAU,EACV,YAAY,EACZ,SAAS,EACT,GAAG,OAAO,EACX,GAAE,mBAAmB,CAAC,CAAC,CAAM,QA2C/B"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {} from "exceljs";
|
|
2
|
+
import { array } from "@json-table/core/lib/array";
|
|
3
|
+
import { max, sum } from "@json-table/core/lib/math";
|
|
4
|
+
import { cells } from "@json-table/core";
|
|
5
|
+
function cellCount(value, width, height) {
|
|
6
|
+
return typeof value === "string" || typeof value === "number"
|
|
7
|
+
? String(value).length / width / height
|
|
8
|
+
: 0;
|
|
9
|
+
}
|
|
10
|
+
export function calculateSheetData(tree, { columnWidth, rowHeight }) {
|
|
11
|
+
const height = tree.height;
|
|
12
|
+
const width = tree.width;
|
|
13
|
+
const columnCounts = array(width, () => 0);
|
|
14
|
+
const rowCounts = array(height, () => 0);
|
|
15
|
+
const sheetCells = [];
|
|
16
|
+
for (const { node, x, y, width: w, height: h } of cells(tree)) {
|
|
17
|
+
for (let j = x; j < x + w; j++) {
|
|
18
|
+
columnCounts[j] += cellCount(node.value, w, h) * h;
|
|
19
|
+
}
|
|
20
|
+
for (let i = y; i < y + h; i++) {
|
|
21
|
+
rowCounts[i] += cellCount(node.value, w, h) * w;
|
|
22
|
+
}
|
|
23
|
+
sheetCells.push({
|
|
24
|
+
height: h,
|
|
25
|
+
width: w,
|
|
26
|
+
type: node.type,
|
|
27
|
+
value: { value: node.value, col: x + 1, row: y + 1 },
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
widths: columnCounts.map((_, i) => columnWidth(columnCounts, i, tree)),
|
|
32
|
+
heights: rowCounts.map((_, i) => rowHeight(rowCounts, i, tree)),
|
|
33
|
+
cells: sheetCells,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export function renderOnWorksheet(sheet, tree, { cellMinHeight = 22, cellMinWidth = 10, modifyCell, modifyColumn, modifyRow, ...options } = {}) {
|
|
37
|
+
const { heights, widths, cells } = calculateSheetData(tree, {
|
|
38
|
+
columnWidth: (counts) => Math.max(Math.ceil(counts.reduce(sum) / tree.height + counts.reduce(max)), cellMinWidth),
|
|
39
|
+
rowHeight: (counts) => Math.max(Math.ceil((counts.reduce(sum) / tree.width) * 2), cellMinHeight),
|
|
40
|
+
...options,
|
|
41
|
+
});
|
|
42
|
+
widths.forEach((width, i) => {
|
|
43
|
+
const column = sheet.getColumn(i + 1);
|
|
44
|
+
column.width = width;
|
|
45
|
+
modifyColumn?.(column, i + 1);
|
|
46
|
+
});
|
|
47
|
+
heights.forEach((height, i) => {
|
|
48
|
+
const row = sheet.getRow(i + 1);
|
|
49
|
+
row.height = height;
|
|
50
|
+
modifyRow?.(row, i + 1);
|
|
51
|
+
});
|
|
52
|
+
cells.forEach((cell, i) => {
|
|
53
|
+
const { height, width, type, value: { col, row, value }, } = cell;
|
|
54
|
+
const sheetCell = sheet.getRow(row).getCell(col);
|
|
55
|
+
sheetCell.value = value;
|
|
56
|
+
sheetCell.alignment = { vertical: "middle", wrapText: true };
|
|
57
|
+
if (type !== "leaf") {
|
|
58
|
+
sheetCell.font = { bold: true };
|
|
59
|
+
}
|
|
60
|
+
modifyCell?.(sheetCell, cell, i);
|
|
61
|
+
if (height > 1 || width > 1) {
|
|
62
|
+
sheet.mergeCells(row, col, row + height - 1, col + width - 1);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=tree-to-xlsx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tree-to-xlsx.js","sourceRoot":"","sources":["../src/tree-to-xlsx.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,MAAM,SAAS,CAAC;AA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AC;QAC7D,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YACpB,SAAS,CAAC,IAAI,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;QAClC,CAAC;QACD,UAAU,EAAE,CAAC,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QACjC,IAAI,MAAM,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YAC5B,KAAK,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,MAAM,GAAG,CAAC,EAAE,GAAG,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC;QAChE,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@json-table/xlsx",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Library for rendering JSON Table trees to XLSX",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"json",
|
|
8
|
+
"table",
|
|
9
|
+
"xlsx"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"!dist/**/*.test.*",
|
|
15
|
+
"!dist/**/*.spec.*"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"provenance": true
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/x0k/json-table.git",
|
|
23
|
+
"directory": "packages/xlsx"
|
|
24
|
+
},
|
|
25
|
+
"bugs": "https://github.com/x0k/json-table/issues",
|
|
26
|
+
"homepage": "https://x0k.github.io/json-table/",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc",
|
|
29
|
+
"test": "vitest --run"
|
|
30
|
+
},
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/tree-to-xlsx.d.ts",
|
|
34
|
+
"import": "./dist/tree-to-xlsx.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"exceljs": "^4.4.0",
|
|
39
|
+
"@json-table/core": "workspace:^"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"vitest": "catalog:",
|
|
43
|
+
"@json-table/core": "workspace:^0.3.0"
|
|
44
|
+
}
|
|
45
|
+
}
|