I'm currently working on writing a prebuild script for a Nextjs project that will download some data and parse it into a json file.
I found the SheetJS package which seems to do everything I need. The issue is, the ODS file I'm trying to parse is large (40k+ records) and I'm getting the following error when trying to run my script:
'Error: Cannot create a string longer than 0x1fffffe8 characters'
I've done some reading online, and the solution seems to be 'Stream reading'.
There's an example about quarter of the way down the Sheetjs readme 'Explicitly concatenating streams' which I've used as a base to create the following code.
It's running perfectly on a small file with a single line of data in the spreadsheet, but when I try and run it with the full sized spreadsheet, I'm still getting the error.
I'm guessing I might be going wrong with the stream reading part? Any advice would be much appreciated.
var XLSX = require('xlsx');
var fs = require('fs');
var parseData = async () => {
var file = fs.createReadStream('./data/data.ods');
function process_RS(stream, cb) {
var buffers = [];
stream.on('data', function (data) {
buffers.push(data);
});
stream.on('end', function () {
var buffer = Buffer.concat(buffers);
var workbook = XLSX.read(buffer, { type: 'buffer' });
/* DO SOMETHING WITH workbook IN THE CALLBACK */
cb(workbook);
});
}
process_RS(file, (workbook) => {
var data = XLSX.utils.sheet_to_json(workbook['Sheets']['Cars'], {
range: 7,
header: ['Make', 'Model'],
});
console.log(data);
});
};
parseData();