abstract-sheet 0.0.3 → 0.0.5

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.
Files changed (47) hide show
  1. package/README.md +1 -1
  2. package/lib/abstract-sheet/abstract-sheet-xsd.d.ts +1 -1
  3. package/lib/abstract-sheet/abstract-sheet-xsd.d.ts.map +1 -1
  4. package/lib/abstract-sheet/abstract-sheet-xsd.js +41 -45
  5. package/lib/abstract-sheet/abstract-sheet-xsd.js.map +1 -1
  6. package/lib/abstract-sheet/abstract-sheet.d.ts +23 -9
  7. package/lib/abstract-sheet/abstract-sheet.d.ts.map +1 -1
  8. package/lib/abstract-sheet/abstract-sheet.js.map +1 -1
  9. package/lib/abstract-sheet-exporters/_style.d.ts +3 -0
  10. package/lib/abstract-sheet-exporters/_style.d.ts.map +1 -0
  11. package/lib/abstract-sheet-exporters/_style.js +113 -0
  12. package/lib/abstract-sheet-exporters/_style.js.map +1 -0
  13. package/lib/abstract-sheet-exporters/_xlsx-sheet.d.ts +2 -3
  14. package/lib/abstract-sheet-exporters/_xlsx-sheet.d.ts.map +1 -1
  15. package/lib/abstract-sheet-exporters/_xlsx-sheet.js +14 -116
  16. package/lib/abstract-sheet-exporters/_xlsx-sheet.js.map +1 -1
  17. package/lib/abstract-sheet-exporters/csv.d.ts +2 -2
  18. package/lib/abstract-sheet-exporters/csv.d.ts.map +1 -1
  19. package/lib/abstract-sheet-exporters/csv.js +19 -16
  20. package/lib/abstract-sheet-exporters/csv.js.map +1 -1
  21. package/lib/abstract-sheet-exporters/index.d.ts +1 -0
  22. package/lib/abstract-sheet-exporters/index.d.ts.map +1 -1
  23. package/lib/abstract-sheet-exporters/index.js +1 -0
  24. package/lib/abstract-sheet-exporters/index.js.map +1 -1
  25. package/lib/abstract-sheet-exporters/react.d.ts +5 -0
  26. package/lib/abstract-sheet-exporters/react.d.ts.map +1 -0
  27. package/lib/abstract-sheet-exporters/react.js +135 -0
  28. package/lib/abstract-sheet-exporters/react.js.map +1 -0
  29. package/lib/abstract-sheet-exporters/xlsx.d.ts +1 -1
  30. package/lib/abstract-sheet-exporters/xlsx.d.ts.map +1 -1
  31. package/lib/abstract-sheet-exporters/xlsx.js +8 -5
  32. package/lib/abstract-sheet-exporters/xlsx.js.map +1 -1
  33. package/lib/abstract-sheet-xml/abstract-sheet-xml.d.ts +3 -1
  34. package/lib/abstract-sheet-xml/abstract-sheet-xml.d.ts.map +1 -1
  35. package/lib/abstract-sheet-xml/abstract-sheet-xml.js +34 -23
  36. package/lib/abstract-sheet-xml/abstract-sheet-xml.js.map +1 -1
  37. package/package.json +6 -2
  38. package/src/abstract-sheet/abstract-sheet-xsd.ts +41 -45
  39. package/src/abstract-sheet/abstract-sheet-xsd.xml +41 -45
  40. package/src/abstract-sheet/abstract-sheet.ts +11 -12
  41. package/src/abstract-sheet-exporters/_style.ts +113 -0
  42. package/src/abstract-sheet-exporters/_xlsx-sheet.ts +19 -121
  43. package/src/abstract-sheet-exporters/csv.ts +23 -19
  44. package/src/abstract-sheet-exporters/index.ts +1 -0
  45. package/src/abstract-sheet-exporters/react.tsx +185 -0
  46. package/src/abstract-sheet-exporters/xlsx.ts +9 -6
  47. package/src/abstract-sheet-xml/abstract-sheet-xml.ts +36 -23
@@ -0,0 +1,185 @@
1
+ import React from "react";
2
+ import { AbstractSheet, BorderStyle, Style } from "../abstract-sheet/abstract-sheet";
3
+
4
+ export function toReact({ abstractSheet }: { readonly abstractSheet: AbstractSheet }): JSX.Element {
5
+ const styles = React.useMemo(
6
+ () => Object.fromEntries(abstractSheet.styles?.map((s) => [s.name, s]) ?? []),
7
+ [abstractSheet.styles]
8
+ );
9
+
10
+ return (
11
+ <div className="abstract-sheet">
12
+ {abstractSheet.sheets.map((s, si) => {
13
+ const maxCols = s.cells.reduce((a, c) => Math.max(a, c.length), 0);
14
+ const colArray = Array.from({ length: maxCols }, (_, i) => i);
15
+ return (
16
+ <div
17
+ className={`sheet sheet-${si} sheet-${s.name}`}
18
+ key={s.name}
19
+ style={{
20
+ display: "grid",
21
+ width: "max-content",
22
+ height: "max-content",
23
+ border: "1px solid rgb(200,200,200)",
24
+ gridTemplateRows: `${(s.direction === "col" ? colArray : s.cells)
25
+ .map((_, ri) => {
26
+ const info = s.rowInfo?.[ri];
27
+ return info?.hidden
28
+ ? ""
29
+ : info?.heightPixels !== undefined
30
+ ? `${info.heightPixels}px`
31
+ : "minmax(18px, max-content)";
32
+ })
33
+ .join(" ")}`,
34
+ gridTemplateColumns: `${(s.direction === "col" ? s.cells : colArray)
35
+ .map((_, ci) => {
36
+ const co = s.colInfo?.[ci];
37
+ return co?.hidden
38
+ ? ""
39
+ : co?.widthPixels !== undefined
40
+ ? `${co.widthPixels}px`
41
+ : "minmax(64px, max-content)";
42
+ })
43
+ .join(" ")}`,
44
+ }}
45
+ >
46
+ {s.cells.map((cells, cellsIdx) => (
47
+ <div
48
+ key={cellsIdx}
49
+ className={`cells cells-${cellsIdx}`}
50
+ style={{
51
+ display: "grid",
52
+ ...(s.direction === "col"
53
+ ? { gridTemplateRows: `subgrid`, gridRow: "1 / -1" }
54
+ : { gridTemplateColumns: `subgrid`, gridColumn: "1 / -1" }),
55
+ }}
56
+ >
57
+ {Array.from({ length: maxCols }, (_, i) => i).map((ci) => {
58
+ const c = cells[ci];
59
+ let s: Partial<Style> = {};
60
+ if (c?.styles !== undefined) {
61
+ for (const style of c.styles) {
62
+ s = { ...s!, ...styles[style] };
63
+ }
64
+ }
65
+ return (
66
+ <div
67
+ className={`cell cell-${ci} cell-${c?.type}`}
68
+ key={ci}
69
+ style={{
70
+ padding: "1px",
71
+ background: s.foreground,
72
+ justifyContent:
73
+ s.horizontal === "right"
74
+ ? "end"
75
+ : s.horizontal === "left"
76
+ ? "start"
77
+ : s.horizontal === "center"
78
+ ? "center"
79
+ : c?.type === "number"
80
+ ? "end"
81
+ : "start",
82
+ display: "flex",
83
+ alignItems:
84
+ s.vertical === "top"
85
+ ? "start"
86
+ : s.vertical === "bottom"
87
+ ? "end"
88
+ : s.vertical === "center"
89
+ ? "center"
90
+ : c?.type === "number"
91
+ ? "end"
92
+ : "center ",
93
+ borderTop: `${borderWidth(
94
+ s.borderStyle?.top ?? (s.borderColor?.top ? "medium" : undefined)
95
+ )} ${borderStyle(s.borderStyle?.top)} ${
96
+ s.borderColor?.top ?? s.foreground ?? "rgb(200,200,200)"
97
+ } `,
98
+ borderRight: `${borderWidth(
99
+ s.borderStyle?.right ?? (s.borderColor?.right ? "medium" : undefined)
100
+ )} ${borderStyle(s.borderStyle?.right)} ${
101
+ s.borderColor?.right ?? s.foreground ?? "rgb(200,200,200)"
102
+ } `,
103
+ borderBottom: `${borderWidth(
104
+ s.borderStyle?.bottom ?? (s.borderColor?.bottom ? "medium" : undefined)
105
+ )} ${borderStyle(s.borderStyle?.bottom)} ${
106
+ s.borderColor?.bottom ?? s.foreground ?? "rgb(200,200,200)"
107
+ } `,
108
+ borderLeft: `${borderWidth(
109
+ s.borderStyle?.left ?? (s.borderColor?.left ? "medium" : undefined)
110
+ )} ${borderStyle(s.borderStyle?.left)} ${
111
+ s.borderColor?.left ?? s.foreground ?? "rgb(200,200,200)"
112
+ } `,
113
+ }}
114
+ >
115
+ <div
116
+ className={`value value-${ci} value-${c?.type}`}
117
+ style={{
118
+ fontFamily: "calibri",
119
+ textDecoration: [s.strike ? "line-through" : "", s.underline ? "underline" : ""].join(" "),
120
+ fontWeight: s.bold ? "bold" : undefined,
121
+ fontStyle: s.italic ? "italic" : undefined,
122
+ fontSize: `${s.size ?? 11}px`,
123
+ lineHeight: `${s.size ?? 11}px`,
124
+ height: `${s.size ?? 11}px`,
125
+ color: s.color ? s.color : undefined,
126
+ transform: s.textRotation ? `rotate(${s.textRotation})` : undefined,
127
+ }}
128
+ >
129
+ {typeof c?.value === "object" ? c.value.toISOString() : c?.value ?? ""}
130
+ </div>
131
+ </div>
132
+ );
133
+ })}
134
+ </div>
135
+ ))}
136
+ </div>
137
+ );
138
+ })}
139
+ </div>
140
+ );
141
+ }
142
+
143
+ function borderWidth(border: BorderStyle | undefined): string {
144
+ switch (border) {
145
+ case "hair":
146
+ case "thin":
147
+ default:
148
+ case undefined:
149
+ return "1px";
150
+ case "thick":
151
+ return "3px";
152
+ case "mediumDashDot":
153
+ case "mediumDashDotDot":
154
+ case "mediumDashed":
155
+ case "dotted":
156
+ case "dashDotDot":
157
+ case "slantDashDot":
158
+ case "dashed":
159
+ case "dashDot":
160
+ case "medium":
161
+ return "2px";
162
+ }
163
+ }
164
+
165
+ function borderStyle(border: BorderStyle | undefined): string {
166
+ switch (border) {
167
+ case "dashDot":
168
+ case "mediumDashDot":
169
+ case "mediumDashDotDot":
170
+ case "mediumDashed":
171
+ case "dashDotDot":
172
+ case "slantDashDot":
173
+ case "dashed":
174
+ return "dashed";
175
+ case "hair":
176
+ case "dotted":
177
+ return "dotted";
178
+ case undefined:
179
+ case "medium":
180
+ case "thick":
181
+ case "thin":
182
+ default:
183
+ return "solid";
184
+ }
185
+ }
@@ -1,14 +1,17 @@
1
1
  import * as XLSX from "xlsx-js-style";
2
2
  import { AbstractSheet } from "../abstract-sheet/abstract-sheet";
3
- import { createStyle, xlsxWorkSheet } from "./_xlsx-sheet";
3
+ import { xlsxWorkSheet } from "./_xlsx-sheet";
4
+ import { createStyle } from "./_style";
4
5
 
5
- export function toXlsx(as: AbstractSheet): Buffer {
6
+ export function toXlsx(...abstractSheets: ReadonlyArray<AbstractSheet>): Buffer {
6
7
  const sheets: Record<string, XLSX.WorkSheet> = {};
7
8
  const sheetNames = Array<string>();
8
- const styles = Object.fromEntries(as.styles?.map((s) => [s.name, createStyle(s)]) ?? []);
9
- for (const s of as.sheets) {
10
- sheetNames.push(s.name);
11
- sheets[s.name] = xlsxWorkSheet(s, styles);
9
+ for (const as of abstractSheets) {
10
+ const styles = Object.fromEntries(as.styles?.map((s) => [s.name, createStyle(s)]) ?? []);
11
+ for (const s of as.sheets) {
12
+ sheetNames.push(s.name);
13
+ sheets[s.name] = xlsxWorkSheet(s, styles);
14
+ }
12
15
  }
13
16
  const wb: XLSX.WorkBook = { Sheets: sheets, SheetNames: Object.keys(sheets) };
14
17
  return XLSX.write(wb, { bookType: "xlsx", bookSST: false, type: "buffer" });
@@ -3,43 +3,48 @@ import {
3
3
  Cell,
4
4
  ColInfo,
5
5
  ColInfos,
6
- Row,
6
+ Cells,
7
7
  RowInfo,
8
8
  RowInfos,
9
- Rows,
10
9
  Sheet,
11
- Sheets,
12
10
  Style,
13
11
  Styles,
12
+ AbstractSheet,
14
13
  } from "../abstract-sheet/abstract-sheet";
15
14
  import { xsd } from "../abstract-sheet/abstract-sheet-xsd";
16
- import { parseXml, parseXsd, XmlElement } from "./mustache-xml";
15
+ import { parseMustacheXml, parseXsd, XmlElement } from "./mustache-xml";
17
16
 
18
- export function abstractSheetXml(el: XmlElement): unknown {
17
+ export const abstractSheetXml = (
18
+ template: string,
19
+ data: any,
20
+ partials: Record<string, string>,
21
+ sheetPrefix?: string
22
+ ): AbstractSheet => abstractSheetOfXml(parseMustacheXml(template, data, partials)[0]!, sheetPrefix) as AbstractSheet;
23
+
24
+ export function abstractSheetOfXml(el: XmlElement, sheetPrefix?: string): unknown {
19
25
  const children = Array<unknown>();
20
26
  const childElements = Array<XmlElement>();
21
27
  for (const child of el.children ?? []) {
22
28
  if (child.tagName !== undefined) {
23
- children.push(abstractSheetXml(child));
29
+ children.push(abstractSheetOfXml(child, sheetPrefix));
24
30
  childElements.push(child);
25
31
  }
26
32
  }
33
+
27
34
  switch (el.tagName) {
28
35
  case "AbstractSheet":
29
36
  let styles: Styles | undefined = undefined;
30
- let sheets: Sheets = [];
37
+ const sheets: Array<Sheet> = [];
31
38
  childElements.forEach((childEl, i) => {
32
- if (childEl.tagName === "Sheets") {
33
- sheets = children[i] as Sheets;
39
+ if (childEl.tagName === "Sheet") {
40
+ sheets.push(children[i] as Sheet);
34
41
  } else if (childEl.tagName === "Styles") {
35
42
  styles = children[i] as Styles;
36
43
  }
37
44
  });
38
- return { styles, sheets };
39
- case "Sheets":
40
- return children as Sheets;
45
+ return { styles, sheets } satisfies AbstractSheet;
41
46
  case "Sheet": {
42
- let rows: Rows = [];
47
+ const cells: Array<Cells> = [];
43
48
  let colInfo: ColInfos | undefined = undefined;
44
49
  let rowInfo: RowInfos | undefined = undefined;
45
50
  childElements.forEach((childEl, i) => {
@@ -47,16 +52,17 @@ export function abstractSheetXml(el: XmlElement): unknown {
47
52
  colInfo = children[i] as ColInfos;
48
53
  } else if (childEl.tagName === "RowInfos") {
49
54
  rowInfo = children[i] as RowInfos;
50
- } else if (childEl.tagName === "Rows") {
51
- rows = children[i] as Rows;
55
+ } else if (childEl.tagName === "Cells") {
56
+ cells.push(children[i] as Cells);
52
57
  }
53
58
  });
54
59
  return {
55
- name: ((el.attributes as Partial<Record<keyof Sheet, unknown>>)?.name ?? "") as string,
56
- rows,
60
+ name: `${sheetPrefix ?? ""}${((el.attributes as Partial<Record<keyof Sheet, unknown>>)?.name ?? "") as string}`,
61
+ cells,
57
62
  colInfo,
58
63
  rowInfo,
59
- };
64
+ direction: ((el.attributes as Partial<Record<keyof Sheet, unknown>>)?.direction ?? "") as Sheet["direction"],
65
+ } satisfies Sheet;
60
66
  }
61
67
  case "ColInfos":
62
68
  return children as ColInfos;
@@ -66,12 +72,19 @@ export function abstractSheetXml(el: XmlElement): unknown {
66
72
  return children as RowInfos;
67
73
  case "RowInfo":
68
74
  return el.attributes as RowInfo;
69
- case "Rows":
70
- return children as Rows;
71
- case "Row":
72
- return children as Row;
75
+ case "Cells":
76
+ return children as Cells;
73
77
  case "Cell": {
74
- return { ...el.attributes, styles: el.attributes.styles?.split(",") } as Cell;
78
+ const styles = el.attributes.styles?.split(",");
79
+ if (el.attributes.number !== undefined) {
80
+ return { ...el.attributes, type: "number", value: el.attributes.number, styles } as Cell;
81
+ } else if (el.attributes.bool !== undefined) {
82
+ return { ...el.attributes, type: "boolean", value: el.attributes.boolean, styles } as Cell;
83
+ } else if (el.attributes.date !== undefined) {
84
+ return { ...el.attributes, type: "date", value: el.attributes.date, styles } as Cell;
85
+ } else {
86
+ return { ...el.attributes, type: "string", value: el.attributes.text, styles } as Cell;
87
+ }
75
88
  }
76
89
  case "Styles":
77
90
  return children as Styles;