XLSX.writeFileAsync attempts to use callback as an options object if options are omitted #2865

Open
opened 2 weeks ago by tsuriga · 1 comments

The documentation says:

XLSX.writeFileAsync(filename, wb, o, cb) attempts to write wb to filename. If o is omitted, the writer will use the third argument as the callback.

But if o is omitted: XLSX.writeFileAsync(filename, wb, cb)
the package takes the callback function as options object and attempts to resolve book type on the callback function. This eventually runs into the error:

TypeError: o.file.lastIndexOf is not a function

The code also first sets the type to file seemingly redundantly, and then later to buffer.

If the documentation is correct and implementation should follow what it says, the function definition should be something along the lines of (disclaimer: not tested):

function writeFileAsync(filename/*:string*/, wb/*:Workbook*/, opts/*:?WriteFileOpts*/, cb/*:?(e?:ErrnoError)=>void*/) {
	var o = cb ? opts || {} : {};
	o.file = filename;
	o.type = 'buffer';

	resolve_book_type(o);

	var _cb = cb;
	if (!(_cb instanceof Function)) {
		if (opts instanceof Function) {
			_cb = opts;
		} else {
			_cb = () => {};
		}
	}

	return _fs.writeFile(filename, writeSync(wb, o), _cb);
}

Version 0.18.5.

The documentation says: > XLSX.writeFileAsync(filename, wb, o, cb) attempts to write wb to filename. If o is omitted, the writer will use the third argument as the callback. But if o is omitted: ```XLSX.writeFileAsync(filename, wb, cb)``` the package takes the callback function as options object and attempts to resolve book type on the callback function. This eventually runs into the error: > TypeError: o.file.lastIndexOf is not a function The code also first sets the type to *file* seemingly redundantly, and then later to *buffer*. If the documentation is correct and implementation should follow what it says, the function definition should be something along the lines of (disclaimer: not tested): ```js function writeFileAsync(filename/*:string*/, wb/*:Workbook*/, opts/*:?WriteFileOpts*/, cb/*:?(e?:ErrnoError)=>void*/) { var o = cb ? opts || {} : {}; o.file = filename; o.type = 'buffer'; resolve_book_type(o); var _cb = cb; if (!(_cb instanceof Function)) { if (opts instanceof Function) { _cb = opts; } else { _cb = () => {}; } } return _fs.writeFile(filename, writeSync(wb, o), _cb); } ``` Version 0.18.5.
Owner

Thanks for reporting! This isn't surprising as there are no tests for that function.

In the interim, it's straightforward to do the write from user space. Sketch:

const buf = XLSX.write(wb, {
  ...opts,
  type: "buffer",
  bookType: require("path").extname(filename).slice(1)
});
require("fs").writeFile(filename, buf, cb);
Thanks for reporting! This isn't surprising as there are no tests for that function. In the interim, it's straightforward to do the write from user space. Sketch: ```js const buf = XLSX.write(wb, { ...opts, type: "buffer", bookType: require("path").extname(filename).slice(1) }); require("fs").writeFile(filename, buf, cb); ```
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#2865
Loading…
There is no content yet.