callback for writeFile? #396

Closed
opened 2016-04-13 16:19:54 +00:00 by barbalex · 3 comments
barbalex commented 2016-04-13 16:19:54 +00:00 (Migrated from github.com)

First of all: thanks for this great tool!

XLSX.writeFile can take quite some time to finish. And of course an error could occur.

In either case the user should be informed about the outcome. Or maybe the created file should then be opened.

I must be missing something extremely obvious, but I can't find a way to pass a callback and receive error and result.

I have tried adding a callback as fourth parameter. But it seems like this is ignored.

(How) can this be done?

First of all: thanks for this great tool! `XLSX.writeFile` can take quite some time to finish. And of course an error could occur. In either case the user should be informed about the outcome. Or maybe the created file should then be opened. I must be missing something extremely obvious, but I can't find a way to pass a callback and receive error and result. I have tried adding a callback as fourth parameter. But it seems like this is ignored. (How) can this be done?
lucdetellis commented 2016-06-05 21:18:19 +00:00 (Migrated from github.com)

+1
This is a JavaScript module. Callbacks are a standard these days (especially with Node.js).

+1 This is a JavaScript module. Callbacks are a standard these days (especially with Node.js).
dhavalocked commented 2017-01-30 14:52:41 +00:00 (Migrated from github.com)

Is there any progress on this issue? @barbalex @lucdetellis were you guys able use callback? please help if you have any information.

Is there any progress on this issue? @barbalex @lucdetellis were you guys able use callback? please help if you have any information.
SheetJSDev commented 2017-02-21 08:20:30 +00:00 (Migrated from github.com)

@barbalex @lucdetellis @mrwhite1 you can just wrap XLSX.write.

XLSX.writeFile(wb, filename, opts) basically

  • sets opts.bookType to the appropriate output type based on the file name (so if the filename ends in .xlsx then opts.bookType = "xlsx"

  • sets opts.type to buffer and calls XLSX.write(wb, opts)

  • calls fs.writeFileSync on the output of the write.

So it should be straightforward to write something similar: (warning: untested)

function writeFileAsync(wb, filename, opts, cb) {
    	// set up bookType and type options
    	var o = opts||{}; o.type = 'buffer';
	if(!o.bookType) switch(filename.substr(-5).toLowerCase()) {
		case '.xlsx': o.bookType = 'xlsx'; break;
		case '.xlsm': o.bookType = 'xlsm'; break;
		case '.xlsb': o.bookType = 'xlsb'; break;
	default: switch(filename.substr(-4).toLowerCase()) {
		case '.xls': o.bookType = 'biff2'; break;
		case '.ods': o.bookType = 'ods'; break;
	}}
    	// generate output data
	var out;
	try {
		out = XLSX.write(wb, o);
	} catch(e) {
    	    	// call the callback if there's an error
		return cb(e, null);
	}
    	// success! now just write the file asynchronously
	fs.writeFile(filename, out, cb);
}
@barbalex @lucdetellis @mrwhite1 you can just wrap `XLSX.write`. `XLSX.writeFile(wb, filename, opts)` basically - sets `opts.bookType` to the appropriate output type based on the file name (so if the filename ends in `.xlsx` then `opts.bookType = "xlsx"` - sets `opts.type` to buffer and calls `XLSX.write(wb, opts)` - calls `fs.writeFileSync` on the output of the write. So it should be straightforward to write something similar: (warning: untested) ```js function writeFileAsync(wb, filename, opts, cb) { // set up bookType and type options var o = opts||{}; o.type = 'buffer'; if(!o.bookType) switch(filename.substr(-5).toLowerCase()) { case '.xlsx': o.bookType = 'xlsx'; break; case '.xlsm': o.bookType = 'xlsm'; break; case '.xlsb': o.bookType = 'xlsb'; break; default: switch(filename.substr(-4).toLowerCase()) { case '.xls': o.bookType = 'biff2'; break; case '.ods': o.bookType = 'ods'; break; }} // generate output data var out; try { out = XLSX.write(wb, o); } catch(e) { // call the callback if there's an error return cb(e, null); } // success! now just write the file asynchronously fs.writeFile(filename, out, cb); } ```
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#396
No description provided.