I am using Google App Scripts built in function, UrlFetchApp.fetch, in order to request data from a URL. When you navigate to this URL in the browser, it just initiates a file download of an XLSX file. If I console.log the response, it is just a string representation of binary data. How can I read this binary XLSX file contents, and output it as JSON?
I am not able to save this file to drive and read it back in, I’m looking for a way that parses the response data directly within App Script, in-memory. The following code uses the xlsx.full.min.js library.
When running:
function parseExcelToJson() {
try {
// Step 1: Fetch the Excel file from the URL
const response = UrlFetchApp.fetch('https://www.nasdaqtrader.com/Content/ProductsServices/TRADING/TRF-Chicago-Test-Securities.xlsx');
// Step 2: Convert the response to a Blob (in memory)
const blob = response.getBlob();
// Step 3: Convert the Blob to a byte array (needed for xlsx.js)
const byteArray = blob.getBytes();
// Step 4: Use xlsx.js to parse the byte array
const data = XLSX.read(byteArray, { type: 'array' });
// Step 5: Access the first sheet
const sheet = data.Sheets[data.SheetNames[0]];
// Step 6: Convert the sheet to JSON format
const json = XLSX.utils.sheet_to_json(sheet);
// Step 7: Return the JSON result
return json;
} catch (e) {
// Log any errors during parsing
Logger.log("Error during parsing: " + e.message);
}
}
I receive the following error: "Error during parsing: Unsupported ZIP Compression method NaN"
3
Answers
Two solutions
1.
changed to
The answer from HernanATN also solved my problem and seemed to be more trustworthy to go with a different version of xlsx that is more compatible with GAS
So the thing is, while Google Apps Script is a JS runtime, it does not implement all of JavaScript features:
It actually implements a bespoke subset of the ECMAScript standard, and not necesarilly compliant with latest features from V8 engine (present in modern browsers and runtimes as Node,Deno, Bun). So you can’t assume that modern libraries will work: luckily for old-enough libraries, a little of trail and error and you’ll find a suitable version to use! I’ve found that XLSX 0.17.0 works inside GAS, so, you should fetch and evaluate that version:
Then, the following script works as intended
Output:
There are Two solutions
The first Solution is
and second solution is change to
The reply from HernanATN too illuminated my issue and appeared to be more dependable to go with a diverse form of xlsx that’s more consistent with GAS.