generated from sindresorhus/node-module-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
117 lines (110 loc) · 2.5 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import fs from 'node:fs';
import JSZip from 'jszip';
import accessibleSpreadsheetCreator from './index.js';
//import test from 'ava';
//test('main', t => {
// t.throws(() => {
// unicornFun(123);
// }, {
// instanceOf: TypeError,
// message: 'Expected a string, got number',
// });
//
// t.is(unicornFun('unicorns'), 'unicorns & rainbows');
//});
const odsData = {
coverSheetTitle: 'Some pretend data',
coverSheetContents: [
'## Subtitle 1',
'Text 1a',
'Text 1b',
'## Subtitle 2',
'Text 2a',
'Text 2b',
'[Office for National Statistics](https://www.ons.gov.uk)',
],
notes: [
{name: 'note_1', text: 'Here is a note'},
{name: 'another_note', text: 'Here is another note'},
{name: 'note C', text: 'Another note'},
],
sheets: [
{
sheetName: 'Age',
tableName: 'Age',
sheetIntroText: ['Some introductory text [[another_note]][[note_1]]'],
columns: [
{
style: 'text',
heading: 'Category [[note_1]]',
values: [
'First category [[note_1]]',
'Second category',
]
},
{
style: 'number_with_commas',
heading: 'Selected group',
values: [
12_345, 1_234_567
]
},
{
style: 'number_1dp',
heading: 'England and Wales with a really long title',
values: [
45.6, 45.6
]
},
]
},
{
sheetName: 'Ethnicity',
tableName: 'Ethnicity',
columns: [
{
style: 'text',
heading: 'Category',
values: [
'First category',
'Second category',
'Really long looooooong category'
]
},
{
style: 'number_1dp',
heading: 'Selected group',
values: [
12.3, 12.3, 78.9
]
},
{
style: 'number_2dp',
heading: 'England and Wales with a really long title\n(units)',
allowNulls: true,
values: [
45.6, null, 45.678
]
},
]
},
],
};
for (let i=3; i<6; i++) {
odsData.coverSheetContents.push('Here is a cell with lots and lots and lots of words. '.repeat(i * 3));
}
const zipFiles = accessibleSpreadsheetCreator(odsData);
const zip = new JSZip();
for (const {filename, contents} of zipFiles) {
zip.file(filename, contents, {compression: filename === 'mimetype' ? 'STORE' : 'DEFLATE'});
}
zip.generateNodeStream({type: 'nodebuffer', streamFiles: true})
.pipe(fs.createWriteStream('out.zip'))
.on('finish', () => {
console.log('out.zip written.');
});
zip.generateNodeStream({type: 'nodebuffer', streamFiles: true})
.pipe(fs.createWriteStream('out.ods'))
.on('finish', () => {
console.log('out.ods written.');
});