Skip to content

Commit ea47cdc

Browse files
committed
Simplifying exportWorkbook()
saveBlob() replaces complexity of processAndSave()
1 parent 1dd4766 commit ea47cdc

File tree

1 file changed

+88
-87
lines changed

1 file changed

+88
-87
lines changed

lib/utils/files.js

+88-87
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { utils as XlsxUtils, writeFile } from 'xlsx/xlsx.js';
1+
import { utils as XlsxUtils, writeFile} from 'xlsx/xlsx.js';
22
import { saveAs } from 'file-saver';
33

44
/**
@@ -120,6 +120,7 @@ export function createWorkbookFromJSON(jsonData) {
120120
return workbook;
121121
}
122122

123+
/*
123124
function writeWorkbook(workbook, baseName, ext, opt = {}) {
124125
if (ext === 'xlsx' || ext === 'xls') {
125126
// can support multiple sheets in one file
@@ -144,6 +145,7 @@ function writeWorkbook(workbook, baseName, ext, opt = {}) {
144145
});
145146
}
146147
}
148+
*/
147149

148150
/**
149151
* Download matrix to file.
@@ -158,111 +160,110 @@ function writeWorkbook(workbook, baseName, ext, opt = {}) {
158160
* @param {String} ext Extension of downloaded file.
159161
*/
160162
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-
}
229163

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
238165
const sheets = workbook.SheetNames;
239166
sheets.forEach((sheetName) => {
240-
let data = '';
241-
242167
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+
/*
243243
const sheetData = XlsxUtils.sheet_to_json(worksheet, { header: 1 });
244244
245245
const formattedData = sheetData
246246
.map((row) => row.join(delimiter))
247247
.join('\n');
248248
data += formattedData + '\n';
249249
250+
// Insert BOM character.
250251
if (includeBOM && mimeType.includes('UTF-8')) {
251252
data = '\uFEFF' + data;
252253
}
254+
*/
253255

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
254260
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+
};
263263

264264
// TODO: refactor to export matrix
265265
export function exportFile(matrix, baseName, ext) {
266+
console.log("running exportFile", matrix, baseName, ext)
266267
const worksheet = XlsxUtils.aoa_to_sheet(matrix);
267268
const workbook = XlsxUtils.book_new();
268269
XlsxUtils.book_append_sheet(workbook, worksheet, DEFAULT_SHEETNAME);

0 commit comments

Comments
 (0)