1
- import { utils as XlsxUtils , writeFile } from 'xlsx/xlsx.js' ;
1
+ import { utils as XlsxUtils , writeFile } from 'xlsx/xlsx.js' ;
2
2
import { saveAs } from 'file-saver' ;
3
3
4
4
/**
@@ -120,6 +120,7 @@ export function createWorkbookFromJSON(jsonData) {
120
120
return workbook ;
121
121
}
122
122
123
+ /*
123
124
function writeWorkbook(workbook, baseName, ext, opt = {}) {
124
125
if (ext === 'xlsx' || ext === 'xls') {
125
126
// can support multiple sheets in one file
@@ -144,6 +145,7 @@ function writeWorkbook(workbook, baseName, ext, opt = {}) {
144
145
});
145
146
}
146
147
}
148
+ */
147
149
148
150
/**
149
151
* Download matrix to file.
@@ -158,111 +160,110 @@ function writeWorkbook(workbook, baseName, ext, opt = {}) {
158
160
* @param {String } ext Extension of downloaded file.
159
161
*/
160
162
export function exportWorkbook ( workbook , baseName , ext ) {
161
- switch ( ext ) {
162
- case 'xlsx' :
163
- case 'xls' :
164
- writeWorkbook ( workbook , baseName , ext ) ;
165
- break ;
166
- case 'csv' :
167
- processAndSave (
168
- workbook ,
169
- baseName ,
170
- 'csv' ,
171
- ',' ,
172
- 'text/plain;charset=UTF-8'
173
- ) ;
174
- break ;
175
-
176
- case 'csv (UTF-16)' :
177
- processAndSave (
178
- workbook ,
179
- baseName ,
180
- 'csv' ,
181
- ',' ,
182
- 'text/plain;charset=UTF-16LE'
183
- ) ;
184
- break ;
185
-
186
- case 'tsv' :
187
- processAndSave (
188
- workbook ,
189
- baseName ,
190
- 'tsv' ,
191
- '\t' ,
192
- 'text/plain;charset=UTF-8'
193
- ) ;
194
- break ;
195
-
196
- case 'tsv (UTF-16)' :
197
- processAndSave (
198
- workbook ,
199
- baseName ,
200
- 'tsv' ,
201
- '\t' ,
202
- 'text/plain;charset=UTF-16LE'
203
- ) ;
204
- break ;
205
-
206
- case 'csv (UTF-8, no BOM)' :
207
- processAndSave (
208
- workbook ,
209
- baseName ,
210
- 'csv' ,
211
- ',' ,
212
- 'text/plain;charset=UTF-8' ,
213
- false
214
- ) ;
215
- break ;
216
-
217
- case 'csv (ASCII)' :
218
- processAndSave (
219
- workbook ,
220
- baseName ,
221
- 'csv' ,
222
- ',' ,
223
- 'text/plain;charset=us-ascii' ,
224
- false
225
- ) ;
226
- break ;
227
- }
228
- }
229
163
230
- function processAndSave (
231
- workbook ,
232
- baseName ,
233
- ext ,
234
- delimiter ,
235
- mimeType ,
236
- includeBOM = true
237
- ) {
164
+ // Often just one sheet, but if multiple, then each gets file name + _ + template (class) name
238
165
const sheets = workbook . SheetNames ;
239
166
sheets . forEach ( ( sheetName ) => {
240
- let data = '' ;
241
-
242
167
const worksheet = workbook . Sheets [ sheetName ] ;
168
+ const fileName = `${ baseName } ${ sheets . length > 1 ? `_${ sheetName } ` : '' } .${ ext . split ( ' ' ) [ 0 ] } ` ;
169
+ var data = '' ;
170
+
171
+ switch ( ext ) {
172
+ case 'xlsx' :
173
+ case 'xls' :
174
+ // Note, mimeType always set to application/zip in these cases.
175
+ writeFile ( workbook , `${ baseName } .${ ext } ` ) ; //, opt
176
+ break ;
177
+
178
+ /* Notes:
179
+ See
180
+ - https://docs.sheetjs.com/docs/api/write-options/
181
+ - https://docs.sheetjs.com/docs/api/utilities/csv#csv-output
182
+ * We want more accurate mimeTypes, so saveBlob() allows this.
183
+ * writeFile(bookType: 'csv'...) output includes the UTF-8 byte order
184
+ * mark ("BOM").
185
+ * writeFile(bookType: 'tsv'...) output will NOT include the BOM ???
186
+ * sheet_to_csv() will return JavaScript strings without the UTF-8 BOM.
187
+ * sheet_to_txt(): If encoding support is available, the output will be
188
+ * encoded in CP1200 and the UTF-16 BOM will be added. If encoding
189
+ * support is not available, the output will be encoded as a standard
190
+ * string.
191
+ * So is encoding support available?
192
+ */
193
+ case 'csv' :
194
+ // writeFile(workbook, fileName, {bookType: 'csv', FS: ','});
195
+ data = XlsxUtils . sheet_to_csv ( worksheet , { FS : ',' } ) ;
196
+ data = '\uFEFF' + data ; //BOM
197
+ saveBlob ( data , fileName , 'text/plain;charset=UTF-8' ) ;
198
+ break ;
199
+
200
+ case 'csv (UTF-16)' :
201
+ //writeFile(workbook, fileName, {bookType: 'txt', FS: ','});
202
+ data = XlsxUtils . sheet_to_csv ( worksheet , { FS : ',' } ) ;
203
+ data = '\uFEFF' + data ; //BOM
204
+ saveBlob ( data , fileName , 'text/plain;charset=UTF-16LE' ) ;
205
+ break ;
206
+
207
+ case 'tsv' : // BOM version
208
+ //writeFile(workbook, fileName, {bookType: 'csv', FS: '\t'});
209
+ data = XlsxUtils . sheet_to_csv ( worksheet , { FS : '\t' } ) ;
210
+ data = '\uFEFF' + data ; //BOM
211
+ saveBlob ( data , fileName , 'text/plain;charset=UTF-8' ) ;
212
+ break ;
213
+
214
+ case 'tsv (UTF-16)' :
215
+ data = XlsxUtils . sheet_to_txt ( worksheet , { FS : '\t' } ) ;
216
+ saveBlob ( data , fileName , 'text/plain;charset=UTF-16LE' ) ;
217
+ break ;
218
+
219
+ case 'csv (UTF-8, no BOM)' :
220
+ data = XlsxUtils . sheet_to_csv ( worksheet , { FS : ',' } ) ;
221
+ saveBlob ( data , fileName , 'text/plain;charset=UTF-8' ) ;
222
+ break ;
223
+
224
+ case 'csv (ASCII)' : // no BOM
225
+ data = XlsxUtils . sheet_to_csv ( worksheet , { FS : ',' } ) ;
226
+ saveBlob ( data , fileName , 'text/plain;charset=us-ascii' ) ;
227
+ break ;
228
+ }
229
+ } )
230
+ } ;
231
+
232
+ // Saves workbook which may have multiple sheets into one or more files.
233
+ // ext: csv, csv (UTF-16), tsv, tsv (UTF-16)
234
+ // ext no BOM: csv (UTF-8, no BOM), csv (ASCII)
235
+ // This script can enhance file type with mimeType - but is that something sheetJS can't do?
236
+ function saveBlob (
237
+ data ,
238
+ fileName ,
239
+ mimeType
240
+ ) {
241
+
242
+ /*
243
243
const sheetData = XlsxUtils.sheet_to_json(worksheet, { header: 1 });
244
244
245
245
const formattedData = sheetData
246
246
.map((row) => row.join(delimiter))
247
247
.join('\n');
248
248
data += formattedData + '\n';
249
249
250
+ // Insert BOM character.
250
251
if (includeBOM && mimeType.includes('UTF-8')) {
251
252
data = '\uFEFF' + data;
252
253
}
254
+ */
253
255
256
+ // https://docs.sheetjs.com/docs/api/utilities/csv#csv-output
257
+ // "If encoding support is available, the output will be encoded in CP1200 and the UTF-16 BOM will be added. If encoding support is not available, the output will be encoded as a standard string.""
258
+
259
+ // Enhancing with mimeType
254
260
const blob = new Blob ( [ data ] , { type : mimeType } ) ;
255
- saveAs (
256
- blob ,
257
- `${ baseName } ${ sheets . length > 1 ? `_${ sheetName } ` : '' } .${
258
- ext . split ( ' ' ) [ 0 ]
259
- } `
260
- ) ;
261
- } ) ;
262
- }
261
+ saveAs ( blob , fileName ) ;
262
+ } ;
263
263
264
264
// TODO: refactor to export matrix
265
265
export function exportFile ( matrix , baseName , ext ) {
266
+ console . log ( "running exportFile" , matrix , baseName , ext )
266
267
const worksheet = XlsxUtils . aoa_to_sheet ( matrix ) ;
267
268
const workbook = XlsxUtils . book_new ( ) ;
268
269
XlsxUtils . book_append_sheet ( workbook , worksheet , DEFAULT_SHEETNAME ) ;
0 commit comments