to-spreadsheet 1.1.4 → 1.2.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 +250 -4
- package/lib/docProps/core.xml.js +10 -5
- package/lib/generate-excel.js +40 -7
- package/lib/index.d.ts +38 -5
- package/lib/index.js +127 -2
- package/lib/util.d.ts +19 -2
- package/lib/util.js +112 -1
- package/lib/xl/styles.xml.d.ts +2 -1
- package/lib/xl/styles.xml.js +127 -26
- package/lib/xl/worksheets/sheet.xml.d.ts +2 -2
- package/lib/xl/worksheets/sheet.xml.js +29 -3
- package/package.json +34 -3
package/README.md
CHANGED
|
@@ -3,11 +3,12 @@ npm package to create spreadsheet in node environment and in browser
|
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/to-spreadsheet)
|
|
6
|
-
[](https://
|
|
7
|
-
[](https://
|
|
6
|
+
[](https://badgen.net/bundlephobia/min/to-spreadsheet)
|
|
7
|
+
[](https://badgen.net/bundlephobia/minzip/to-spreadsheet)
|
|
8
8
|
|
|
9
9
|
[](https://github.com/maifeeulasad/to-spreadsheet/stargazers)
|
|
10
10
|
[](https://github.com/maifeeulasad/to-spreadsheet/watchers)
|
|
11
|
+
[](https://img.shields.io/github/commits-since/maifeeulasad/to-spreadsheet/latest/main?include_prereleases)
|
|
11
12
|
|
|
12
13
|
# NPM
|
|
13
14
|
```
|
|
@@ -39,8 +40,253 @@ generateExcel(sampleData); // <-- by default generate XLSX for node
|
|
|
39
40
|
generateExcel(sampleData, EnvironmentType.BROWSER); // <-- for browser
|
|
40
41
|
```
|
|
41
42
|
|
|
43
|
+
# Cell Features
|
|
44
|
+
|
|
45
|
+
## Dates
|
|
46
|
+
|
|
47
|
+
You can create date cells that are properly formatted in Excel:
|
|
48
|
+
|
|
49
|
+
### Basic Date Usage
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import {
|
|
53
|
+
generateExcel,
|
|
54
|
+
createDateCell,
|
|
55
|
+
createBorderedDateCell,
|
|
56
|
+
createBackgroundDateCell
|
|
57
|
+
} from 'to-spreadsheet/lib/index';
|
|
58
|
+
|
|
59
|
+
const data = [
|
|
60
|
+
{
|
|
61
|
+
title: 'DateDemo',
|
|
62
|
+
content: [
|
|
63
|
+
[
|
|
64
|
+
'Event',
|
|
65
|
+
'Date',
|
|
66
|
+
'Styled Date'
|
|
67
|
+
],
|
|
68
|
+
[
|
|
69
|
+
'Project Start',
|
|
70
|
+
createDateCell(new Date('2024-01-01')),
|
|
71
|
+
createBorderedDateCell(new Date('2024-01-15'), createAllBorders())
|
|
72
|
+
],
|
|
73
|
+
[
|
|
74
|
+
'Milestone',
|
|
75
|
+
createDateCell(new Date()),
|
|
76
|
+
createBackgroundDateCell(new Date('2024-12-25'), '#FFCCCC')
|
|
77
|
+
]
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
];
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Date Helper Functions
|
|
84
|
+
|
|
85
|
+
- `createDateCell(date, style?)` - Creates a date cell with optional styling
|
|
86
|
+
- `createBorderedDateCell(date, border)` - Creates a date cell with borders
|
|
87
|
+
- `createBackgroundDateCell(date, backgroundColor)` - Creates a date cell with background color
|
|
88
|
+
|
|
89
|
+
## Colors
|
|
90
|
+
|
|
91
|
+
You can add background and foreground colors to cells:
|
|
92
|
+
|
|
93
|
+
### Basic Color Usage
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import {
|
|
97
|
+
generateExcel,
|
|
98
|
+
createBackgroundCell,
|
|
99
|
+
createForegroundCell,
|
|
100
|
+
createColoredCell,
|
|
101
|
+
createStyledCell
|
|
102
|
+
} from 'to-spreadsheet/lib/index';
|
|
103
|
+
|
|
104
|
+
const data = [
|
|
105
|
+
{
|
|
106
|
+
title: 'ColorDemo',
|
|
107
|
+
content: [
|
|
108
|
+
[
|
|
109
|
+
'Feature',
|
|
110
|
+
'Background Color',
|
|
111
|
+
'Foreground Color',
|
|
112
|
+
'Both Colors'
|
|
113
|
+
],
|
|
114
|
+
[
|
|
115
|
+
'Yellow Background',
|
|
116
|
+
createBackgroundCell('Highlighted', '#FFFF00'),
|
|
117
|
+
createForegroundCell('Red Text', '#FF0000'),
|
|
118
|
+
createColoredCell('Green BG, Red Text', '#00FF00', '#FF0000')
|
|
119
|
+
],
|
|
120
|
+
[
|
|
121
|
+
'Complex Styling',
|
|
122
|
+
createStyledCell('Full Style', {
|
|
123
|
+
backgroundColor: '#FFFFCC',
|
|
124
|
+
foregroundColor: '#0000FF',
|
|
125
|
+
border: createAllBorders(BorderStyle.thick, '#000000')
|
|
126
|
+
}),
|
|
127
|
+
'Regular cell',
|
|
128
|
+
42
|
|
129
|
+
]
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
];
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Color Helper Functions
|
|
136
|
+
|
|
137
|
+
- `createBackgroundCell(value, backgroundColor)` - Creates cell with background color
|
|
138
|
+
- `createForegroundCell(value, foregroundColor)` - Creates cell with text color
|
|
139
|
+
- `createColoredCell(value, backgroundColor, foregroundColor)` - Creates cell with both colors
|
|
140
|
+
- `createStyledCell(value, style)` - Creates cell with full styling options
|
|
141
|
+
|
|
42
142
|
# Features
|
|
43
143
|
- [x] Multiple sheet support
|
|
44
144
|
- [x] Equations
|
|
45
|
-
- [
|
|
46
|
-
- [
|
|
145
|
+
- [x] Cell borders
|
|
146
|
+
- [x] Cell styling (background colors, foreground colors, dates)
|
|
147
|
+
- [x] Date cells with proper Excel formatting
|
|
148
|
+
- [ ] Cell alignment
|
|
149
|
+
- [ ] Sheet styling
|
|
150
|
+
|
|
151
|
+
## Cell Borders
|
|
152
|
+
|
|
153
|
+
You can add borders to cells using the border functionality:
|
|
154
|
+
|
|
155
|
+
### Basic Border Usage
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import {
|
|
159
|
+
generateExcel,
|
|
160
|
+
createBorderedCell,
|
|
161
|
+
createAllBorders,
|
|
162
|
+
createTopBorder,
|
|
163
|
+
createBottomBorder,
|
|
164
|
+
createLeftBorder,
|
|
165
|
+
createRightBorder,
|
|
166
|
+
BorderStyle
|
|
167
|
+
} from 'to-spreadsheet/lib/index';
|
|
168
|
+
|
|
169
|
+
const data = [
|
|
170
|
+
{
|
|
171
|
+
title: 'BorderDemo',
|
|
172
|
+
content: [
|
|
173
|
+
[
|
|
174
|
+
// Create cells with all borders
|
|
175
|
+
createBorderedCell('Header 1', createAllBorders(BorderStyle.thick, '#000000')),
|
|
176
|
+
createBorderedCell('Header 2', createAllBorders(BorderStyle.thick, '#000000'))
|
|
177
|
+
],
|
|
178
|
+
[
|
|
179
|
+
// Create cells with specific borders
|
|
180
|
+
createBorderedCell('Data 1', createTopBorder()),
|
|
181
|
+
createBorderedCell(100, createRightBorder()),
|
|
182
|
+
createBorderedCell('Final', createBottomBorder())
|
|
183
|
+
]
|
|
184
|
+
]
|
|
185
|
+
}
|
|
186
|
+
];
|
|
187
|
+
|
|
188
|
+
generateExcel(data);
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Border Styles
|
|
192
|
+
|
|
193
|
+
Available border styles:
|
|
194
|
+
- `BorderStyle.none` - No border
|
|
195
|
+
- `BorderStyle.thin` - Thin border (default)
|
|
196
|
+
- `BorderStyle.medium` - Medium border
|
|
197
|
+
- `BorderStyle.thick` - Thick border
|
|
198
|
+
- `BorderStyle.double` - Double border
|
|
199
|
+
- `BorderStyle.dotted` - Dotted border
|
|
200
|
+
- `BorderStyle.dashed` - Dashed border
|
|
201
|
+
|
|
202
|
+
### Border Helper Functions
|
|
203
|
+
|
|
204
|
+
**Border Creation:**
|
|
205
|
+
- `createAllBorders(style?, color?)` - Creates borders on all sides
|
|
206
|
+
- `createTopBorder(style?, color?)` - Creates only top border
|
|
207
|
+
- `createBottomBorder(style?, color?)` - Creates only bottom border
|
|
208
|
+
- `createLeftBorder(style?, color?)` - Creates only left border
|
|
209
|
+
- `createRightBorder(style?, color?)` - Creates only right border
|
|
210
|
+
- `createBorder(borderConfig)` - Creates custom border configuration
|
|
211
|
+
|
|
212
|
+
**Cell Creation:**
|
|
213
|
+
- `createBorderedCell(value, border)` - Creates a cell with border
|
|
214
|
+
- `createStyledCell(value, style)` - Creates a cell with custom styling
|
|
215
|
+
|
|
216
|
+
### Advanced Border Usage
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { createStyledCell, BorderStyle } from 'to-spreadsheet/lib/index';
|
|
220
|
+
|
|
221
|
+
// Custom border configuration
|
|
222
|
+
const customCell = createStyledCell('Custom', {
|
|
223
|
+
border: {
|
|
224
|
+
left: BorderStyle.double,
|
|
225
|
+
top: BorderStyle.thin,
|
|
226
|
+
right: BorderStyle.dashed,
|
|
227
|
+
bottom: BorderStyle.thick,
|
|
228
|
+
color: '#FF0000' // Red borders
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Mix styled and regular cells
|
|
233
|
+
const data = [
|
|
234
|
+
{
|
|
235
|
+
title: 'Mixed',
|
|
236
|
+
content: [
|
|
237
|
+
[customCell, 'Regular Cell', 42]
|
|
238
|
+
]
|
|
239
|
+
}
|
|
240
|
+
];
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Combined Styling
|
|
244
|
+
|
|
245
|
+
All styling features can be combined together:
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
import {
|
|
249
|
+
createStyledCell,
|
|
250
|
+
createAllBorders,
|
|
251
|
+
BorderStyle
|
|
252
|
+
} from 'to-spreadsheet/lib/index';
|
|
253
|
+
|
|
254
|
+
const data = [
|
|
255
|
+
{
|
|
256
|
+
title: 'CombinedDemo',
|
|
257
|
+
content: [
|
|
258
|
+
[
|
|
259
|
+
// Cell with background color, text color, and borders
|
|
260
|
+
createStyledCell('Fully Styled', {
|
|
261
|
+
backgroundColor: '#FFFFCC', // Light yellow background
|
|
262
|
+
foregroundColor: '#0000FF', // Blue text
|
|
263
|
+
border: createAllBorders(BorderStyle.thick, '#FF0000') // Red thick border
|
|
264
|
+
}),
|
|
265
|
+
|
|
266
|
+
// Date with background and border
|
|
267
|
+
createDateCell(new Date(), {
|
|
268
|
+
backgroundColor: '#CCFFCC', // Light green background
|
|
269
|
+
border: createAllBorders(BorderStyle.double, '#008000') // Green double border
|
|
270
|
+
}),
|
|
271
|
+
|
|
272
|
+
// Regular cells for comparison
|
|
273
|
+
'Plain text',
|
|
274
|
+
42
|
|
275
|
+
]
|
|
276
|
+
]
|
|
277
|
+
}
|
|
278
|
+
];
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Color Format
|
|
282
|
+
|
|
283
|
+
Colors should be specified in hex format:
|
|
284
|
+
- `#FF0000` - Red
|
|
285
|
+
- `#00FF00` - Green
|
|
286
|
+
- `#0000FF` - Blue
|
|
287
|
+
- `#FFFF00` - Yellow
|
|
288
|
+
- `#FF00FF` - Magenta
|
|
289
|
+
- `#00FFFF` - Cyan
|
|
290
|
+
- `#000000` - Black
|
|
291
|
+
- `#FFFFFF` - White
|
|
292
|
+
- `#CCCCCC` - Light gray
|
package/lib/docProps/core.xml.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateCoreXml = void 0;
|
|
4
|
-
const
|
|
4
|
+
const formatDate = (date) => {
|
|
5
|
+
const year = date.getUTCFullYear();
|
|
6
|
+
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
|
|
7
|
+
const day = String(date.getUTCDate()).padStart(2, '0');
|
|
8
|
+
const hours = String(date.getUTCHours()).padStart(2, '0');
|
|
9
|
+
const minutes = String(date.getUTCMinutes()).padStart(2, '0');
|
|
10
|
+
const seconds = String(date.getUTCSeconds()).padStart(2, '0');
|
|
11
|
+
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}Z`;
|
|
12
|
+
};
|
|
5
13
|
const generateCoreXml = ({ username = "to-spreadsheet", description = "", language = "en-US", version = "1", subject = "", title = "" }) => {
|
|
6
|
-
const date = (
|
|
7
|
-
"T" +
|
|
8
|
-
(0, date_fns_1.format)(Date.now(), "HH:mm:ss") +
|
|
9
|
-
"Z";
|
|
14
|
+
const date = formatDate(new Date());
|
|
10
15
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
11
16
|
<cp:coreProperties
|
|
12
17
|
xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"
|
package/lib/generate-excel.js
CHANGED
|
@@ -14,7 +14,23 @@ const sheet_xml_1 = require("./xl/worksheets/sheet.xml");
|
|
|
14
14
|
const index_1 = require("./index");
|
|
15
15
|
const util_1 = require("./util");
|
|
16
16
|
const generateTree = (workbook) => {
|
|
17
|
-
|
|
17
|
+
const styleMap = new Map();
|
|
18
|
+
styleMap.set("default", {});
|
|
19
|
+
let hasDateCells = false;
|
|
20
|
+
workbook.sheets.forEach(sheet => {
|
|
21
|
+
sheet.rows.forEach(row => {
|
|
22
|
+
row.cells.forEach(cell => {
|
|
23
|
+
if (cell.type === index_1.ICellType.date) {
|
|
24
|
+
hasDateCells = true;
|
|
25
|
+
}
|
|
26
|
+
if ('style' in cell && cell.style) {
|
|
27
|
+
const styleKey = (0, util_1.getStyleKey)(cell.style);
|
|
28
|
+
styleMap.set(styleKey, cell.style);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
return Object.assign({ "[Content_Types].xml": (0, content_types_xml_1.generateContentTypesXml)(workbook), "_rels/.rels": (0, _rels_1.generateRels)(), "docProps/app.xml": (0, app_xml_1.generateAppXml)(workbook), "docProps/core.xml": (0, core_xml_1.generateCoreXml)({}), "xl/_rels/workbook.xml.rels": (0, workbook_xml_rels_1.generateWorkBookXmlRels)(workbook), "xl/sharedStrings.xml": (0, sharedStrings_xml_1.generateSharedStrings)(workbook), "xl/styles.xml": (0, styles_xml_1.generateStyleXml)(styleMap, hasDateCells), "xl/theme/theme1.xml": (0, theme1_xml_1.generateTheme1)(), "xl/workbook.xml": (0, workbook_xml_1.generateWorkBookXml)(workbook) }, workbook.sheets.reduce((acc, sheet, idx) => (Object.assign(Object.assign({}, acc), { [`xl/worksheets/sheet${idx + 1}.xml`]: (0, sheet_xml_1.generateSheetXml)(sheet, styleMap, hasDateCells) })), {}));
|
|
18
34
|
};
|
|
19
35
|
var EnvironmentType;
|
|
20
36
|
(function (EnvironmentType) {
|
|
@@ -26,9 +42,10 @@ const generateExcel = (dump, environmentType = EnvironmentType.NODE) => {
|
|
|
26
42
|
const strings = [];
|
|
27
43
|
const sheets = dump.map(({ title, content }) => {
|
|
28
44
|
const rows = content.map(row => {
|
|
29
|
-
const cells =
|
|
45
|
+
const cells = [];
|
|
46
|
+
row.forEach(content => {
|
|
30
47
|
if (typeof content === 'number') {
|
|
31
|
-
|
|
48
|
+
cells.push({ type: index_1.ICellType.number, value: content });
|
|
32
49
|
}
|
|
33
50
|
else if (typeof content === 'string') {
|
|
34
51
|
const type = index_1.ICellType.string;
|
|
@@ -37,16 +54,32 @@ const generateExcel = (dump, environmentType = EnvironmentType.NODE) => {
|
|
|
37
54
|
strings.push(content);
|
|
38
55
|
value = strings.length - 1;
|
|
39
56
|
}
|
|
40
|
-
|
|
57
|
+
cells.push({ type: index_1.ICellType.string, value });
|
|
41
58
|
}
|
|
42
59
|
else if (content instanceof util_1.SkipCell) {
|
|
43
|
-
|
|
60
|
+
for (let i = 0; i < content.getSkipCell(); i++) {
|
|
61
|
+
cells.push({ type: index_1.ICellType.skip, value: undefined });
|
|
62
|
+
}
|
|
44
63
|
}
|
|
45
64
|
else if (content instanceof util_1.Equation) {
|
|
46
|
-
|
|
65
|
+
cells.push({ type: index_1.ICellType.equation, value: content });
|
|
66
|
+
}
|
|
67
|
+
else if (content && typeof content === 'object' && 'type' in content) {
|
|
68
|
+
const cell = content;
|
|
69
|
+
if (cell.type === index_1.ICellType.string && typeof cell.value === 'string') {
|
|
70
|
+
let stringIndex = strings.indexOf(cell.value);
|
|
71
|
+
if (stringIndex === -1) {
|
|
72
|
+
strings.push(cell.value);
|
|
73
|
+
stringIndex = strings.length - 1;
|
|
74
|
+
}
|
|
75
|
+
cells.push(Object.assign(Object.assign({}, cell), { value: stringIndex }));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
cells.push(cell);
|
|
79
|
+
}
|
|
47
80
|
}
|
|
48
81
|
else {
|
|
49
|
-
|
|
82
|
+
cells.push({ type: index_1.ICellType.skip });
|
|
50
83
|
}
|
|
51
84
|
});
|
|
52
85
|
return { cells };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,18 +1,42 @@
|
|
|
1
1
|
import { generateExcel, EnvironmentType } from "./generate-excel";
|
|
2
|
-
import { SkipCell, skipCell, Equation, writeEquation } from "./util";
|
|
2
|
+
import { SkipCell, skipCell, Equation, writeEquation, createBorder, createAllBorders, createTopBorder, createBottomBorder, createLeftBorder, createRightBorder, createStyledCell, createBorderedCell, createDateCell, createBorderedDateCell, createBackgroundCell, createForegroundCell, createColoredCell, createBackgroundDateCell } from "./util";
|
|
3
3
|
declare enum ICellType {
|
|
4
4
|
string = "s",
|
|
5
5
|
number = "n",
|
|
6
|
+
date = "d",
|
|
6
7
|
skip = "skip",
|
|
7
8
|
equation = "equation"
|
|
8
9
|
}
|
|
10
|
+
declare enum BorderStyle {
|
|
11
|
+
none = "none",
|
|
12
|
+
thin = "thin",
|
|
13
|
+
medium = "medium",
|
|
14
|
+
thick = "thick",
|
|
15
|
+
double = "double",
|
|
16
|
+
dotted = "dotted",
|
|
17
|
+
dashed = "dashed"
|
|
18
|
+
}
|
|
19
|
+
interface IBorder {
|
|
20
|
+
top?: BorderStyle;
|
|
21
|
+
right?: BorderStyle;
|
|
22
|
+
bottom?: BorderStyle;
|
|
23
|
+
left?: BorderStyle;
|
|
24
|
+
color?: string;
|
|
25
|
+
}
|
|
26
|
+
interface ICellStyle {
|
|
27
|
+
border?: IBorder;
|
|
28
|
+
backgroundColor?: string;
|
|
29
|
+
foregroundColor?: string;
|
|
30
|
+
}
|
|
9
31
|
interface ICellString {
|
|
10
32
|
type: ICellType.string;
|
|
11
33
|
value: number;
|
|
34
|
+
style?: ICellStyle;
|
|
12
35
|
}
|
|
13
36
|
interface ICellNumber {
|
|
14
37
|
type: ICellType.number;
|
|
15
38
|
value: number;
|
|
39
|
+
style?: ICellStyle;
|
|
16
40
|
}
|
|
17
41
|
interface ICellSkip {
|
|
18
42
|
type: ICellType.skip;
|
|
@@ -20,8 +44,14 @@ interface ICellSkip {
|
|
|
20
44
|
interface ICellEquation {
|
|
21
45
|
type: ICellType.equation;
|
|
22
46
|
value: Equation;
|
|
47
|
+
style?: ICellStyle;
|
|
23
48
|
}
|
|
24
|
-
|
|
49
|
+
interface ICellDate {
|
|
50
|
+
type: ICellType.date;
|
|
51
|
+
value: Date;
|
|
52
|
+
style?: ICellStyle;
|
|
53
|
+
}
|
|
54
|
+
declare type ICell = ICellString | ICellNumber | ICellDate | ICellSkip | ICellEquation;
|
|
25
55
|
interface IRows {
|
|
26
56
|
cells: ICell[];
|
|
27
57
|
}
|
|
@@ -36,9 +66,9 @@ interface IWorkbook {
|
|
|
36
66
|
}
|
|
37
67
|
interface IPage {
|
|
38
68
|
title: string;
|
|
39
|
-
content: (string | number | undefined | SkipCell | Equation)[][];
|
|
69
|
+
content: (string | number | undefined | SkipCell | Equation | ICell)[][];
|
|
40
70
|
}
|
|
41
|
-
export { ICell, ISheet, IWorkbook, IRows, ICellType, IPage };
|
|
71
|
+
export { ICell, ISheet, IWorkbook, IRows, ICellType, IPage, BorderStyle, IBorder, ICellStyle, ICellDate };
|
|
42
72
|
declare const sampleData: ({
|
|
43
73
|
title: string;
|
|
44
74
|
content: (string[] | (number | Equation)[])[];
|
|
@@ -48,5 +78,8 @@ declare const sampleData: ({
|
|
|
48
78
|
} | {
|
|
49
79
|
title: string;
|
|
50
80
|
content: (string | undefined)[][];
|
|
81
|
+
} | {
|
|
82
|
+
title: string;
|
|
83
|
+
content: (string | number | ICell)[][];
|
|
51
84
|
})[];
|
|
52
|
-
export { generateExcel, sampleData, EnvironmentType, skipCell, writeEquation };
|
|
85
|
+
export { generateExcel, sampleData, EnvironmentType, skipCell, writeEquation, createBorder, createAllBorders, createTopBorder, createBottomBorder, createLeftBorder, createRightBorder, createStyledCell, createBorderedCell, createDateCell, createBorderedDateCell, createBackgroundCell, createForegroundCell, createColoredCell, createBackgroundDateCell };
|
package/lib/index.js
CHANGED
|
@@ -1,20 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.writeEquation = exports.skipCell = exports.EnvironmentType = exports.sampleData = exports.generateExcel = exports.ICellType = void 0;
|
|
3
|
+
exports.createBackgroundDateCell = exports.createColoredCell = exports.createForegroundCell = exports.createBackgroundCell = exports.createBorderedDateCell = exports.createDateCell = exports.createBorderedCell = exports.createStyledCell = exports.createRightBorder = exports.createLeftBorder = exports.createBottomBorder = exports.createTopBorder = exports.createAllBorders = exports.createBorder = exports.writeEquation = exports.skipCell = exports.EnvironmentType = exports.sampleData = exports.generateExcel = exports.BorderStyle = exports.ICellType = void 0;
|
|
4
4
|
const generate_excel_1 = require("./generate-excel");
|
|
5
5
|
Object.defineProperty(exports, "generateExcel", { enumerable: true, get: function () { return generate_excel_1.generateExcel; } });
|
|
6
6
|
Object.defineProperty(exports, "EnvironmentType", { enumerable: true, get: function () { return generate_excel_1.EnvironmentType; } });
|
|
7
7
|
const util_1 = require("./util");
|
|
8
8
|
Object.defineProperty(exports, "skipCell", { enumerable: true, get: function () { return util_1.skipCell; } });
|
|
9
9
|
Object.defineProperty(exports, "writeEquation", { enumerable: true, get: function () { return util_1.writeEquation; } });
|
|
10
|
+
Object.defineProperty(exports, "createBorder", { enumerable: true, get: function () { return util_1.createBorder; } });
|
|
11
|
+
Object.defineProperty(exports, "createAllBorders", { enumerable: true, get: function () { return util_1.createAllBorders; } });
|
|
12
|
+
Object.defineProperty(exports, "createTopBorder", { enumerable: true, get: function () { return util_1.createTopBorder; } });
|
|
13
|
+
Object.defineProperty(exports, "createBottomBorder", { enumerable: true, get: function () { return util_1.createBottomBorder; } });
|
|
14
|
+
Object.defineProperty(exports, "createLeftBorder", { enumerable: true, get: function () { return util_1.createLeftBorder; } });
|
|
15
|
+
Object.defineProperty(exports, "createRightBorder", { enumerable: true, get: function () { return util_1.createRightBorder; } });
|
|
16
|
+
Object.defineProperty(exports, "createStyledCell", { enumerable: true, get: function () { return util_1.createStyledCell; } });
|
|
17
|
+
Object.defineProperty(exports, "createBorderedCell", { enumerable: true, get: function () { return util_1.createBorderedCell; } });
|
|
18
|
+
Object.defineProperty(exports, "createDateCell", { enumerable: true, get: function () { return util_1.createDateCell; } });
|
|
19
|
+
Object.defineProperty(exports, "createBorderedDateCell", { enumerable: true, get: function () { return util_1.createBorderedDateCell; } });
|
|
20
|
+
Object.defineProperty(exports, "createBackgroundCell", { enumerable: true, get: function () { return util_1.createBackgroundCell; } });
|
|
21
|
+
Object.defineProperty(exports, "createForegroundCell", { enumerable: true, get: function () { return util_1.createForegroundCell; } });
|
|
22
|
+
Object.defineProperty(exports, "createColoredCell", { enumerable: true, get: function () { return util_1.createColoredCell; } });
|
|
23
|
+
Object.defineProperty(exports, "createBackgroundDateCell", { enumerable: true, get: function () { return util_1.createBackgroundDateCell; } });
|
|
10
24
|
var ICellType;
|
|
11
25
|
(function (ICellType) {
|
|
12
26
|
ICellType["string"] = "s";
|
|
13
27
|
ICellType["number"] = "n";
|
|
28
|
+
ICellType["date"] = "d";
|
|
14
29
|
ICellType["skip"] = "skip";
|
|
15
30
|
ICellType["equation"] = "equation";
|
|
16
31
|
})(ICellType || (ICellType = {}));
|
|
17
32
|
exports.ICellType = ICellType;
|
|
33
|
+
var BorderStyle;
|
|
34
|
+
(function (BorderStyle) {
|
|
35
|
+
BorderStyle["none"] = "none";
|
|
36
|
+
BorderStyle["thin"] = "thin";
|
|
37
|
+
BorderStyle["medium"] = "medium";
|
|
38
|
+
BorderStyle["thick"] = "thick";
|
|
39
|
+
BorderStyle["double"] = "double";
|
|
40
|
+
BorderStyle["dotted"] = "dotted";
|
|
41
|
+
BorderStyle["dashed"] = "dashed";
|
|
42
|
+
})(BorderStyle || (BorderStyle = {}));
|
|
43
|
+
exports.BorderStyle = BorderStyle;
|
|
18
44
|
const sampleData = [
|
|
19
45
|
{
|
|
20
46
|
title: 'Maifee1', content: [
|
|
@@ -26,6 +52,105 @@ const sampleData = [
|
|
|
26
52
|
]
|
|
27
53
|
},
|
|
28
54
|
{ title: 'Maifee2', content: [[1], [1, (0, util_1.skipCell)(3), 2]] },
|
|
29
|
-
{ title: 'Maifee3', content: [['meaw', undefined, "meaw"], ["woof", 'woof']] }
|
|
55
|
+
{ title: 'Maifee3', content: [['meaw', undefined, "meaw"], ["woof", 'woof']] },
|
|
56
|
+
{
|
|
57
|
+
title: 'BorderDemo',
|
|
58
|
+
content: [
|
|
59
|
+
[
|
|
60
|
+
(0, util_1.createBorderedCell)('Product', (0, util_1.createAllBorders)(BorderStyle.thick, '#000000')),
|
|
61
|
+
(0, util_1.createBorderedCell)('Price', (0, util_1.createAllBorders)(BorderStyle.thick, '#000000')),
|
|
62
|
+
(0, util_1.createBorderedCell)('Total', (0, util_1.createAllBorders)(BorderStyle.thick, '#000000'))
|
|
63
|
+
],
|
|
64
|
+
[
|
|
65
|
+
(0, util_1.createBorderedCell)('Apple', (0, util_1.createLeftBorder)()),
|
|
66
|
+
(0, util_1.createBorderedCell)(10, (0, util_1.createTopBorder)()),
|
|
67
|
+
(0, util_1.createBorderedCell)(100, (0, util_1.createRightBorder)())
|
|
68
|
+
],
|
|
69
|
+
[
|
|
70
|
+
(0, util_1.createStyledCell)('Custom', {
|
|
71
|
+
border: {
|
|
72
|
+
left: BorderStyle.double,
|
|
73
|
+
bottom: BorderStyle.thin,
|
|
74
|
+
color: '#0000FF'
|
|
75
|
+
}
|
|
76
|
+
}),
|
|
77
|
+
200,
|
|
78
|
+
'No Border Cell'
|
|
79
|
+
]
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
title: 'DateDemo',
|
|
84
|
+
content: [
|
|
85
|
+
[
|
|
86
|
+
'Event',
|
|
87
|
+
'Date',
|
|
88
|
+
'Bordered Date'
|
|
89
|
+
],
|
|
90
|
+
[
|
|
91
|
+
'Project Start',
|
|
92
|
+
(0, util_1.createDateCell)(new Date('2024-01-01')),
|
|
93
|
+
(0, util_1.createBorderedDateCell)(new Date('2024-01-15'), (0, util_1.createAllBorders)(BorderStyle.thin, '#000000'))
|
|
94
|
+
],
|
|
95
|
+
[
|
|
96
|
+
'Milestone 1',
|
|
97
|
+
(0, util_1.createDateCell)(new Date()),
|
|
98
|
+
(0, util_1.createDateCell)(new Date('2024-12-31'))
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
'Custom Date Style',
|
|
102
|
+
(0, util_1.createDateCell)(new Date('2024-06-15'), {
|
|
103
|
+
border: {
|
|
104
|
+
top: BorderStyle.thick,
|
|
105
|
+
bottom: BorderStyle.double,
|
|
106
|
+
color: '#FF0000'
|
|
107
|
+
}
|
|
108
|
+
}),
|
|
109
|
+
'Mixed Content'
|
|
110
|
+
]
|
|
111
|
+
]
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
title: 'ColorDemo',
|
|
115
|
+
content: [
|
|
116
|
+
[
|
|
117
|
+
'Feature',
|
|
118
|
+
'Background Color',
|
|
119
|
+
'Foreground Color',
|
|
120
|
+
'Both Colors'
|
|
121
|
+
],
|
|
122
|
+
[
|
|
123
|
+
'Yellow Background',
|
|
124
|
+
(0, util_1.createBackgroundCell)('Highlighted', '#FFFF00'),
|
|
125
|
+
(0, util_1.createForegroundCell)('Red Text', '#FF0000'),
|
|
126
|
+
(0, util_1.createColoredCell)('Both', '#00FF00', '#FF0000')
|
|
127
|
+
],
|
|
128
|
+
[
|
|
129
|
+
'More Colors',
|
|
130
|
+
(0, util_1.createBackgroundCell)('Blue BG', '#0000FF'),
|
|
131
|
+
(0, util_1.createForegroundCell)('Green Text', '#00FF00'),
|
|
132
|
+
(0, util_1.createColoredCell)('Purple/White', '#800080', '#FFFFFF')
|
|
133
|
+
],
|
|
134
|
+
[
|
|
135
|
+
'Date Colors',
|
|
136
|
+
(0, util_1.createBackgroundDateCell)(new Date(), '#FFCCCC'),
|
|
137
|
+
(0, util_1.createDateCell)(new Date('2024-12-25'), {
|
|
138
|
+
backgroundColor: '#00FF00',
|
|
139
|
+
foregroundColor: '#FF0000'
|
|
140
|
+
}),
|
|
141
|
+
'Mixed with dates'
|
|
142
|
+
],
|
|
143
|
+
[
|
|
144
|
+
'With Borders',
|
|
145
|
+
(0, util_1.createStyledCell)('Complex', {
|
|
146
|
+
backgroundColor: '#FFFFCC',
|
|
147
|
+
foregroundColor: '#0000FF',
|
|
148
|
+
border: (0, util_1.createAllBorders)(BorderStyle.thick, '#000000')
|
|
149
|
+
}),
|
|
150
|
+
'Plain text',
|
|
151
|
+
42
|
|
152
|
+
]
|
|
153
|
+
]
|
|
154
|
+
}
|
|
30
155
|
];
|
|
31
156
|
exports.sampleData = sampleData;
|
package/lib/util.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IRows } from ".";
|
|
1
|
+
import { IRows, IBorder, BorderStyle, ICellStyle, ICell } from ".";
|
|
2
2
|
declare const indexToVbIndex: (index: number) => number;
|
|
3
3
|
declare const indexToVbRelationIndex: (index: number) => number;
|
|
4
4
|
declare const indexToRowIndex: (index: number) => string;
|
|
@@ -16,4 +16,21 @@ declare class Equation {
|
|
|
16
16
|
constructor(equation: string);
|
|
17
17
|
}
|
|
18
18
|
declare const writeEquation: (equation: string) => Equation;
|
|
19
|
-
|
|
19
|
+
declare const createBorder: (border: IBorder) => IBorder;
|
|
20
|
+
declare const createAllBorders: (style?: BorderStyle, color?: string) => IBorder;
|
|
21
|
+
declare const createTopBorder: (style?: BorderStyle, color?: string) => IBorder;
|
|
22
|
+
declare const createBottomBorder: (style?: BorderStyle, color?: string) => IBorder;
|
|
23
|
+
declare const createLeftBorder: (style?: BorderStyle, color?: string) => IBorder;
|
|
24
|
+
declare const createRightBorder: (style?: BorderStyle, color?: string) => IBorder;
|
|
25
|
+
declare const getBorderKey: (border?: IBorder) => string;
|
|
26
|
+
declare const getStyleKey: (style?: ICellStyle) => string;
|
|
27
|
+
declare const createStyledCell: (value: string | number, style?: ICellStyle) => ICell;
|
|
28
|
+
declare const createBorderedCell: (value: string | number, border: IBorder) => ICell;
|
|
29
|
+
declare const dateToExcelSerial: (date: Date) => number;
|
|
30
|
+
declare const createDateCell: (date: Date, style?: ICellStyle) => ICell;
|
|
31
|
+
declare const createBorderedDateCell: (date: Date, border: IBorder) => ICell;
|
|
32
|
+
declare const createBackgroundCell: (value: string | number, backgroundColor: string) => ICell;
|
|
33
|
+
declare const createForegroundCell: (value: string | number, foregroundColor: string) => ICell;
|
|
34
|
+
declare const createColoredCell: (value: string | number, backgroundColor: string, foregroundColor: string) => ICell;
|
|
35
|
+
declare const createBackgroundDateCell: (date: Date, backgroundColor: string) => ICell;
|
|
36
|
+
export { indexToVbIndex, indexToVbRelationIndex, indexToRowIndex, rowColumnToVbPosition, calculateExtant, SkipCell, skipCell, Equation, writeEquation, createBorder, createAllBorders, createTopBorder, createBottomBorder, createLeftBorder, createRightBorder, getBorderKey, getStyleKey, createStyledCell, createBorderedCell, dateToExcelSerial, createDateCell, createBorderedDateCell, createBackgroundCell, createForegroundCell, createColoredCell, createBackgroundDateCell };
|
package/lib/util.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.writeEquation = exports.Equation = exports.skipCell = exports.SkipCell = exports.calculateExtant = exports.rowColumnToVbPosition = exports.indexToRowIndex = exports.indexToVbRelationIndex = exports.indexToVbIndex = void 0;
|
|
3
|
+
exports.createBackgroundDateCell = exports.createColoredCell = exports.createForegroundCell = exports.createBackgroundCell = exports.createBorderedDateCell = exports.createDateCell = exports.dateToExcelSerial = exports.createBorderedCell = exports.createStyledCell = exports.getStyleKey = exports.getBorderKey = exports.createRightBorder = exports.createLeftBorder = exports.createBottomBorder = exports.createTopBorder = exports.createAllBorders = exports.createBorder = exports.writeEquation = exports.Equation = exports.skipCell = exports.SkipCell = exports.calculateExtant = exports.rowColumnToVbPosition = exports.indexToRowIndex = exports.indexToVbRelationIndex = exports.indexToVbIndex = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
4
5
|
const indexToVbIndex = (index) => index + 1;
|
|
5
6
|
exports.indexToVbIndex = indexToVbIndex;
|
|
6
7
|
const indexToVbRelationIndex = (index) => indexToVbIndex(index) + 2;
|
|
@@ -38,3 +39,113 @@ class Equation {
|
|
|
38
39
|
exports.Equation = Equation;
|
|
39
40
|
const writeEquation = (equation) => new Equation(equation);
|
|
40
41
|
exports.writeEquation = writeEquation;
|
|
42
|
+
const createBorder = (border) => border;
|
|
43
|
+
exports.createBorder = createBorder;
|
|
44
|
+
const createAllBorders = (style = _1.BorderStyle.thin, color = "#000000") => ({
|
|
45
|
+
top: style,
|
|
46
|
+
right: style,
|
|
47
|
+
bottom: style,
|
|
48
|
+
left: style,
|
|
49
|
+
color
|
|
50
|
+
});
|
|
51
|
+
exports.createAllBorders = createAllBorders;
|
|
52
|
+
const createTopBorder = (style = _1.BorderStyle.thin, color = "#000000") => ({
|
|
53
|
+
top: style,
|
|
54
|
+
color
|
|
55
|
+
});
|
|
56
|
+
exports.createTopBorder = createTopBorder;
|
|
57
|
+
const createBottomBorder = (style = _1.BorderStyle.thin, color = "#000000") => ({
|
|
58
|
+
bottom: style,
|
|
59
|
+
color
|
|
60
|
+
});
|
|
61
|
+
exports.createBottomBorder = createBottomBorder;
|
|
62
|
+
const createLeftBorder = (style = _1.BorderStyle.thin, color = "#000000") => ({
|
|
63
|
+
left: style,
|
|
64
|
+
color
|
|
65
|
+
});
|
|
66
|
+
exports.createLeftBorder = createLeftBorder;
|
|
67
|
+
const createRightBorder = (style = _1.BorderStyle.thin, color = "#000000") => ({
|
|
68
|
+
right: style,
|
|
69
|
+
color
|
|
70
|
+
});
|
|
71
|
+
exports.createRightBorder = createRightBorder;
|
|
72
|
+
const getBorderKey = (border) => {
|
|
73
|
+
if (!border)
|
|
74
|
+
return "none";
|
|
75
|
+
const parts = [
|
|
76
|
+
border.top || "none",
|
|
77
|
+
border.right || "none",
|
|
78
|
+
border.bottom || "none",
|
|
79
|
+
border.left || "none",
|
|
80
|
+
border.color || "#000000"
|
|
81
|
+
];
|
|
82
|
+
return parts.join("-");
|
|
83
|
+
};
|
|
84
|
+
exports.getBorderKey = getBorderKey;
|
|
85
|
+
const getStyleKey = (style) => {
|
|
86
|
+
if (!style)
|
|
87
|
+
return "default";
|
|
88
|
+
const parts = [
|
|
89
|
+
getBorderKey(style.border),
|
|
90
|
+
style.backgroundColor || "no-bg",
|
|
91
|
+
style.foregroundColor || "no-fg"
|
|
92
|
+
];
|
|
93
|
+
return parts.join("|");
|
|
94
|
+
};
|
|
95
|
+
exports.getStyleKey = getStyleKey;
|
|
96
|
+
const createStyledCell = (value, style) => {
|
|
97
|
+
if (typeof value === 'string') {
|
|
98
|
+
return {
|
|
99
|
+
type: _1.ICellType.string,
|
|
100
|
+
value: value,
|
|
101
|
+
style
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
return {
|
|
106
|
+
type: _1.ICellType.number,
|
|
107
|
+
value,
|
|
108
|
+
style
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
exports.createStyledCell = createStyledCell;
|
|
113
|
+
const createBorderedCell = (value, border) => {
|
|
114
|
+
return createStyledCell(value, { border });
|
|
115
|
+
};
|
|
116
|
+
exports.createBorderedCell = createBorderedCell;
|
|
117
|
+
const dateToExcelSerial = (date) => {
|
|
118
|
+
const excelEpoch = new Date(1900, 0, 1);
|
|
119
|
+
const diffTime = date.getTime() - excelEpoch.getTime();
|
|
120
|
+
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
|
|
121
|
+
return diffDays + (date >= new Date(1900, 1, 29) ? 2 : 1);
|
|
122
|
+
};
|
|
123
|
+
exports.dateToExcelSerial = dateToExcelSerial;
|
|
124
|
+
const createDateCell = (date, style) => {
|
|
125
|
+
return {
|
|
126
|
+
type: _1.ICellType.date,
|
|
127
|
+
value: date,
|
|
128
|
+
style
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
exports.createDateCell = createDateCell;
|
|
132
|
+
const createBorderedDateCell = (date, border) => {
|
|
133
|
+
return createDateCell(date, { border });
|
|
134
|
+
};
|
|
135
|
+
exports.createBorderedDateCell = createBorderedDateCell;
|
|
136
|
+
const createBackgroundCell = (value, backgroundColor) => {
|
|
137
|
+
return createStyledCell(value, { backgroundColor });
|
|
138
|
+
};
|
|
139
|
+
exports.createBackgroundCell = createBackgroundCell;
|
|
140
|
+
const createForegroundCell = (value, foregroundColor) => {
|
|
141
|
+
return createStyledCell(value, { foregroundColor });
|
|
142
|
+
};
|
|
143
|
+
exports.createForegroundCell = createForegroundCell;
|
|
144
|
+
const createColoredCell = (value, backgroundColor, foregroundColor) => {
|
|
145
|
+
return createStyledCell(value, { backgroundColor, foregroundColor });
|
|
146
|
+
};
|
|
147
|
+
exports.createColoredCell = createColoredCell;
|
|
148
|
+
const createBackgroundDateCell = (date, backgroundColor) => {
|
|
149
|
+
return createDateCell(date, { backgroundColor });
|
|
150
|
+
};
|
|
151
|
+
exports.createBackgroundDateCell = createBackgroundDateCell;
|
package/lib/xl/styles.xml.d.ts
CHANGED
package/lib/xl/styles.xml.js
CHANGED
|
@@ -1,39 +1,139 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateStyleXml = void 0;
|
|
4
|
-
const
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const generateBorderXml = (border) => {
|
|
6
|
+
const getColorXml = (color) => color ? `<color rgb="${color.replace('#', 'FF')}" />` : '';
|
|
7
|
+
const getBorderSideXml = (side, color) => {
|
|
8
|
+
if (!side || side === __1.BorderStyle.none) {
|
|
9
|
+
return '<left />';
|
|
10
|
+
}
|
|
11
|
+
return `<left style="${side}">${getColorXml(color)}</left>`;
|
|
12
|
+
};
|
|
13
|
+
const leftXml = !border.left || border.left === __1.BorderStyle.none
|
|
14
|
+
? '<left />'
|
|
15
|
+
: `<left style="${border.left}">${getColorXml(border.color)}</left>`;
|
|
16
|
+
const rightXml = !border.right || border.right === __1.BorderStyle.none
|
|
17
|
+
? '<right />'
|
|
18
|
+
: `<right style="${border.right}">${getColorXml(border.color)}</right>`;
|
|
19
|
+
const topXml = !border.top || border.top === __1.BorderStyle.none
|
|
20
|
+
? '<top />'
|
|
21
|
+
: `<top style="${border.top}">${getColorXml(border.color)}</top>`;
|
|
22
|
+
const bottomXml = !border.bottom || border.bottom === __1.BorderStyle.none
|
|
23
|
+
? '<bottom />'
|
|
24
|
+
: `<bottom style="${border.bottom}">${getColorXml(border.color)}</bottom>`;
|
|
25
|
+
return `
|
|
26
|
+
<border>
|
|
27
|
+
${leftXml}
|
|
28
|
+
${rightXml}
|
|
29
|
+
${topXml}
|
|
30
|
+
${bottomXml}
|
|
31
|
+
<diagonal />
|
|
32
|
+
</border>`;
|
|
33
|
+
};
|
|
34
|
+
const generateFontXml = (color) => {
|
|
35
|
+
const colorXml = color
|
|
36
|
+
? `<color rgb="${color.replace('#', 'FF')}" />`
|
|
37
|
+
: '<color theme="1" />';
|
|
38
|
+
return `
|
|
39
|
+
<font>
|
|
40
|
+
<sz val="11" />
|
|
41
|
+
${colorXml}
|
|
42
|
+
<name val="Calibri" />
|
|
43
|
+
<family val="2" />
|
|
44
|
+
<scheme val="minor" />
|
|
45
|
+
</font>`;
|
|
46
|
+
};
|
|
47
|
+
const generateFillXml = (color) => {
|
|
48
|
+
if (!color) {
|
|
49
|
+
return `
|
|
50
|
+
<fill>
|
|
51
|
+
<patternFill patternType="none" />
|
|
52
|
+
</fill>`;
|
|
53
|
+
}
|
|
54
|
+
return `
|
|
55
|
+
<fill>
|
|
56
|
+
<patternFill patternType="solid">
|
|
57
|
+
<fgColor rgb="${color.replace('#', 'FF')}" />
|
|
58
|
+
<bgColor indexed="64" />
|
|
59
|
+
</patternFill>
|
|
60
|
+
</fill>`;
|
|
61
|
+
};
|
|
62
|
+
const generateStyleXml = (styleMap, hasDateCells = false) => {
|
|
63
|
+
const styles = Array.from(styleMap.values());
|
|
64
|
+
const styleCount = styles.length;
|
|
65
|
+
const uniqueBorders = new Map();
|
|
66
|
+
const uniqueFonts = new Map();
|
|
67
|
+
const uniqueFills = new Map();
|
|
68
|
+
uniqueBorders.set("none", {});
|
|
69
|
+
uniqueFonts.set("default", "");
|
|
70
|
+
uniqueFills.set("none", "");
|
|
71
|
+
uniqueFills.set("gray125", "");
|
|
72
|
+
styles.forEach(style => {
|
|
73
|
+
if (style.border) {
|
|
74
|
+
uniqueBorders.set(JSON.stringify(style.border), style.border);
|
|
75
|
+
}
|
|
76
|
+
if (style.foregroundColor) {
|
|
77
|
+
uniqueFonts.set(style.foregroundColor, style.foregroundColor);
|
|
78
|
+
}
|
|
79
|
+
if (style.backgroundColor) {
|
|
80
|
+
uniqueFills.set(style.backgroundColor, style.backgroundColor);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
const borderArray = Array.from(uniqueBorders.values());
|
|
84
|
+
const fontArray = Array.from(uniqueFonts.values());
|
|
85
|
+
const fillArray = Array.from(uniqueFills.values());
|
|
86
|
+
const bordersXml = borderArray.map(border => generateBorderXml(border)).join('');
|
|
87
|
+
const fontsXml = fontArray.map(color => generateFontXml(color || undefined)).join('');
|
|
88
|
+
const fillsXml = fillArray.map(color => generateFillXml(color || undefined)).join('');
|
|
89
|
+
const numFmtsXml = hasDateCells
|
|
90
|
+
? `<numFmts count="1">
|
|
91
|
+
<numFmt numFmtId="164" formatCode="mm/dd/yyyy" />
|
|
92
|
+
</numFmts>`
|
|
93
|
+
: '';
|
|
94
|
+
let cellXfsXml = '';
|
|
95
|
+
let cellXfsCount = styleCount;
|
|
96
|
+
if (hasDateCells) {
|
|
97
|
+
cellXfsXml += styles.map((style, index) => {
|
|
98
|
+
const borderIndex = Array.from(uniqueBorders.keys()).indexOf(JSON.stringify(style.border || {}));
|
|
99
|
+
const fontIndex = Array.from(uniqueFonts.keys()).indexOf(style.foregroundColor || "default");
|
|
100
|
+
const fillIndex = Array.from(uniqueFills.keys()).indexOf(style.backgroundColor || "none");
|
|
101
|
+
return `<xf numFmtId="0" fontId="${fontIndex}" fillId="${fillIndex}" borderId="${borderIndex}" xfId="0" />`;
|
|
102
|
+
}).join('\n ');
|
|
103
|
+
cellXfsXml += '\n ';
|
|
104
|
+
cellXfsXml += styles.map((style, index) => {
|
|
105
|
+
const borderIndex = Array.from(uniqueBorders.keys()).indexOf(JSON.stringify(style.border || {}));
|
|
106
|
+
const fontIndex = Array.from(uniqueFonts.keys()).indexOf(style.foregroundColor || "default");
|
|
107
|
+
const fillIndex = Array.from(uniqueFills.keys()).indexOf(style.backgroundColor || "none");
|
|
108
|
+
return `<xf numFmtId="164" fontId="${fontIndex}" fillId="${fillIndex}" borderId="${borderIndex}" xfId="0" />`;
|
|
109
|
+
}).join('\n ');
|
|
110
|
+
cellXfsCount = styleCount * 2;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
cellXfsXml = styles.map((style, index) => {
|
|
114
|
+
const borderIndex = Array.from(uniqueBorders.keys()).indexOf(JSON.stringify(style.border || {}));
|
|
115
|
+
const fontIndex = Array.from(uniqueFonts.keys()).indexOf(style.foregroundColor || "default");
|
|
116
|
+
const fillIndex = Array.from(uniqueFills.keys()).indexOf(style.backgroundColor || "none");
|
|
117
|
+
return `<xf numFmtId="0" fontId="${fontIndex}" fillId="${fillIndex}" borderId="${borderIndex}" xfId="0" />`;
|
|
118
|
+
}).join('\n ');
|
|
119
|
+
}
|
|
120
|
+
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
5
121
|
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac x16r2 xr" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision">
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
<color theme="1" />
|
|
10
|
-
<name val="Calibri" />
|
|
11
|
-
<family val="2" />
|
|
12
|
-
<scheme val="minor" />
|
|
13
|
-
</font>
|
|
122
|
+
${numFmtsXml}
|
|
123
|
+
<fonts count="${fontArray.length}" x14ac:knownFonts="1">
|
|
124
|
+
${fontsXml}
|
|
14
125
|
</fonts>
|
|
15
|
-
<fills count="
|
|
16
|
-
|
|
17
|
-
<patternFill patternType="none" />
|
|
18
|
-
</fill>
|
|
19
|
-
<fill>
|
|
20
|
-
<patternFill patternType="gray125" />
|
|
21
|
-
</fill>
|
|
126
|
+
<fills count="${fillArray.length}">
|
|
127
|
+
${fillsXml}
|
|
22
128
|
</fills>
|
|
23
|
-
<borders count="
|
|
24
|
-
|
|
25
|
-
<left />
|
|
26
|
-
<right />
|
|
27
|
-
<top />
|
|
28
|
-
<bottom />
|
|
29
|
-
<diagonal />
|
|
30
|
-
</border>
|
|
129
|
+
<borders count="${borderArray.length}">
|
|
130
|
+
${bordersXml}
|
|
31
131
|
</borders>
|
|
32
132
|
<cellStyleXfs count="1">
|
|
33
133
|
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" />
|
|
34
134
|
</cellStyleXfs>
|
|
35
|
-
<cellXfs count="
|
|
36
|
-
|
|
135
|
+
<cellXfs count="${cellXfsCount}">
|
|
136
|
+
${cellXfsXml}
|
|
37
137
|
</cellXfs>
|
|
38
138
|
<cellStyles count="1">
|
|
39
139
|
<cellStyle name="Normal" xfId="0" builtinId="0" />
|
|
@@ -50,4 +150,5 @@ const generateStyleXml = () => `<?xml version="1.0" encoding="UTF-8" standalone=
|
|
|
50
150
|
</extLst>
|
|
51
151
|
</styleSheet>
|
|
52
152
|
`;
|
|
153
|
+
};
|
|
53
154
|
exports.generateStyleXml = generateStyleXml;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { ISheet } from "../..";
|
|
2
|
-
declare const generateSheetXml: (sheet: ISheet) => string;
|
|
1
|
+
import { ISheet, ICellStyle } from "../..";
|
|
2
|
+
declare const generateSheetXml: (sheet: ISheet, styleMap: Map<string, ICellStyle>, hasDateCells?: boolean) => string;
|
|
3
3
|
export { generateSheetXml };
|
|
@@ -3,7 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.generateSheetXml = void 0;
|
|
4
4
|
const __1 = require("../..");
|
|
5
5
|
const util_1 = require("../../util");
|
|
6
|
-
const generateSheetXml = (sheet) => {
|
|
6
|
+
const generateSheetXml = (sheet, styleMap, hasDateCells = false) => {
|
|
7
|
+
const styleToIndex = new Map();
|
|
8
|
+
Array.from(styleMap.keys()).forEach((key, index) => {
|
|
9
|
+
styleToIndex.set(key, index);
|
|
10
|
+
});
|
|
7
11
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
8
12
|
<worksheet
|
|
9
13
|
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
|
|
@@ -28,15 +32,37 @@ const generateSheetXml = (sheet) => {
|
|
|
28
32
|
if (cellType !== __1.ICellType.skip) {
|
|
29
33
|
const cellPosition = (0, util_1.rowColumnToVbPosition)(cellIndex, rowIndex);
|
|
30
34
|
const cellValue = cell.value || '';
|
|
35
|
+
let styleIndex = 0;
|
|
36
|
+
let isDateCell = cell.type === __1.ICellType.date;
|
|
37
|
+
if ('style' in cell && cell.style) {
|
|
38
|
+
const styleKey = (0, util_1.getStyleKey)(cell.style);
|
|
39
|
+
const baseStyleIndex = styleToIndex.get(styleKey) || 0;
|
|
40
|
+
if (isDateCell && hasDateCells) {
|
|
41
|
+
styleIndex = baseStyleIndex + styleMap.size;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
styleIndex = baseStyleIndex;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else if (isDateCell && hasDateCells) {
|
|
48
|
+
styleIndex = styleMap.size;
|
|
49
|
+
}
|
|
31
50
|
if (cell.type === __1.ICellType.equation) {
|
|
32
51
|
rowContent += `
|
|
33
|
-
<c r="${cellPosition}" t="n">
|
|
52
|
+
<c r="${cellPosition}" t="n" s="${styleIndex}">
|
|
34
53
|
<f aca="false">${cell.value.getEquation()}</f>
|
|
54
|
+
</c>\n`;
|
|
55
|
+
}
|
|
56
|
+
else if (cell.type === __1.ICellType.date) {
|
|
57
|
+
const excelDateValue = (0, util_1.dateToExcelSerial)(cell.value);
|
|
58
|
+
rowContent += `
|
|
59
|
+
<c r="${cellPosition}" t="n" s="${styleIndex}">
|
|
60
|
+
<v>${excelDateValue}</v>
|
|
35
61
|
</c>\n`;
|
|
36
62
|
}
|
|
37
63
|
else {
|
|
38
64
|
rowContent += `
|
|
39
|
-
<c r="${cellPosition}" t="${cellType}">
|
|
65
|
+
<c r="${cellPosition}" t="${cellType}" s="${styleIndex}">
|
|
40
66
|
<v>${cellValue}</v>
|
|
41
67
|
</c>\n`;
|
|
42
68
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "to-spreadsheet",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "npm package to create spreadsheet in node environment and in browser",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"excel",
|
|
7
|
+
"xlsx",
|
|
8
|
+
"spreadsheet",
|
|
9
|
+
"workbook",
|
|
10
|
+
"worksheet",
|
|
11
|
+
"cell",
|
|
12
|
+
"border",
|
|
13
|
+
"color",
|
|
14
|
+
"date",
|
|
15
|
+
"formula",
|
|
16
|
+
"equation",
|
|
17
|
+
"export",
|
|
18
|
+
"generate",
|
|
19
|
+
"browser",
|
|
20
|
+
"node",
|
|
21
|
+
"typescript",
|
|
22
|
+
"javascript",
|
|
23
|
+
"office",
|
|
24
|
+
"microsoft",
|
|
25
|
+
"csv",
|
|
26
|
+
"data-export",
|
|
27
|
+
"file-generation",
|
|
28
|
+
"styling",
|
|
29
|
+
"formatting"
|
|
30
|
+
],
|
|
5
31
|
"main": "lib/index.js",
|
|
32
|
+
"types": "lib/index.d.ts",
|
|
6
33
|
"scripts": {
|
|
7
34
|
"prepare": "npm run build",
|
|
8
35
|
"build": "tsc",
|
|
@@ -14,18 +41,22 @@
|
|
|
14
41
|
},
|
|
15
42
|
"author": "Maifee Ul Asad <maifeeulasad@gmail.com>",
|
|
16
43
|
"license": "MIT",
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=14.0.0"
|
|
46
|
+
},
|
|
17
47
|
"bugs": {
|
|
18
48
|
"url": "https://github.com/maifeeulasad/to-spreadsheet/issues"
|
|
19
49
|
},
|
|
20
50
|
"homepage": "https://github.com/maifeeulasad/to-spreadsheet#readme",
|
|
21
51
|
"dependencies": {
|
|
22
52
|
"archiver": "^6.0.1",
|
|
23
|
-
"date-fns": "^2.30.0",
|
|
24
53
|
"file-saver": "^2.0.5",
|
|
25
54
|
"jszip": "^3.10.1"
|
|
26
55
|
},
|
|
27
56
|
"files": [
|
|
28
|
-
"lib"
|
|
57
|
+
"lib",
|
|
58
|
+
"README.md",
|
|
59
|
+
"LICENSE"
|
|
29
60
|
],
|
|
30
61
|
"devDependencies": {
|
|
31
62
|
"@types/archiver": "^5.3.3",
|