exceljs/exceljs

[BUG] Column headers/keys not preserved when writing to a file?

Aperta

#1247 aperta il 1 mag 2020

 (3 commenti) (0 reazioni) (0 assegnatari)JavaScript (1534 fork)batch import
discussionhelp wanted

Metriche repository

Star
 (11.838 stelle)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

I tried writing to a file and then reading from it to update it. But it seems like I can't access the columns by their set keys after I read the file again. However, the data seems to be preserved and can be indexed by column numbers instead of their keys. Here's the code I tried:

(async () => {
	var excel = require('exceljs');

	var w1 = new excel.Workbook();
	var worksheet = w1.addWorksheet('ExampleSheet');

	worksheet.columns = [
		{ header: 'Id', key: 'id', width: 10 },
		{ header: 'Name', key: 'name', width: 32 },
		{ header: 'D.O.B.', key: 'dob', width: 10 }
	];

	console.log('Before Writing:');
	console.log(worksheet.name);
	console.log(worksheet.getRow(1).getCell(1).value);
	console.log(worksheet.getRow(1).getCell('id').value);
	console.log('\n')

	await w1.xlsx.writeFile('test.xlsx');

	var w2 = new excel.Workbook();
	await w2.xlsx.readFile('test.xlsx');
	var worksheet2 = w2.getWorksheet('ExampleSheet');

	console.log('After Reading:')
	console.log(worksheet2.name);
	console.log(worksheet2.getRow(1).getCell(1).value)
	console.log(worksheet2.getRow(1).getCell('id').value);
})();

and here's the log:

$ node test.js
Before Writing:
ExampleSheet
Id
Id


After Reading:
ExampleSheet
Id
(node:19628) UnhandledPromiseRejectionWarning: Error: Out of bounds. Invalid col
umn letter: id

Do let me know if this is my mistake!

Guida contributor