Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callback for writeFile? #396

Closed
barbalex opened this issue Apr 13, 2016 · 3 comments
Closed

callback for writeFile? #396

barbalex opened this issue Apr 13, 2016 · 3 comments

Comments

@barbalex
Copy link

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
Copy link

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

@dhavalocked
Copy link

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

@SheetJSDev
Copy link
Contributor

@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);
}

saarCiklum pushed a commit to Folcon/js-xlsx that referenced this issue Aug 19, 2020
- `aoa_to_sheet` function (fixes SheetJS#314 h/t @fonzy2013 @rvdwijngaard)
- `writeFileAsync` function (fixes SheetJS#396 h/t @barbalex)
- `sheet_to_json` tests + docs + blankrows (fixes SheetJS#602 h/t @EEaglehouse)
- write number format scan now includes every index >= 50
- propagate SSF IE8 fixes (fixes protobi#171 h/t @SheetJSDev)
- update shim for extendscript (see SheetJS#603 h/t @firas3d)
- more flow type definitions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants