sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

Sheetnames using URL.createObjectURL does not work

Open aiwha-dev opened this issue 7 years ago • 3 comments

I'm using XLSX to extract the sheetnames and it returns "Sheet1"

So when i try to extract the data (using alasql) i get the error "Cannot read !ref of undefined" (or something like that)

The excell file is made in a dutch excell though and apparently it should get "Blad1" instead of "Sheet1"

let fileAsObject = URL.createObjectURL( file );
let workbook = XLSX.read(fileAsObject);
console.log(workbook.SheetNames);

Does anyone know how to fix this?

Edit: It's really weird, if i use Blad1 it starts to read it, but if i console.log the workbook i get the following

SheetNames: Array(1)
	0: "Sheet1"
Sheets:
	Sheet1:
		!ref: "A1"
			A1:
				t: "s"
				v: "nZ†Ûi³ÿæÉÖ¢³'(Ê&uëõýÿ\Û͝áÎz㆛ëÎZ۞¼ë~}wä"

aiwha-dev avatar Jan 03 '19 08:01 aiwha-dev

Ok i'm still diving into this. And now it seems that even if I make an XLSX with sheetnames, it never returns them but always returns the above.

aiwha-dev avatar Jan 04 '19 16:01 aiwha-dev

Ok i might need to read it differently, don't know how, it works for alaSql to use file as object.

I'm still using that for alaSql. But to get the sheetnames i've changed it to:

reader.onload = function() {
	let workbook = XLSX.read(reader.result, {type: 'binary'});
	console.log(workbook.SheetNames);
};
reader.readAsBinaryString(file);

aiwha-dev avatar Jan 07 '19 10:01 aiwha-dev

To read from an object URL, you have to use XHR or fetch to get the data as an array buffer:

fetch(url).then((blob) =>> blob.arrayBuffer()).then((ab) => {
    var u8 = new Uint8Array(ab);
    let workbook = XLSX.read(ab, {type: 'array'});
    console.log(workbook.SheetNames);
});

SheetJSDev avatar Jan 23 '19 20:01 SheetJSDev