我正在使用 Google App Scripts 内置函数 UrlFetchApp.fetch,以便从 URL 请求数据。当您在浏览器中导航到此 URL 时,它只会启动 XLSX 文件的文件下载。如果我 console.log 响应,它只是二进制数据的字符串表示形式。如何读取这个二进制 XLSX 文件内容,并将其输出为 JSON?
我无法将此文件保存到驱动器中并将其读回,我正在寻找一种直接在内存中的应用程序脚本中解析响应数据的方法。以下代码使用 xlsx.full.min.js 库.
跑步时:
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);
}
}
我收到以下错误:
"Error during parsing: Unsupported ZIP Compression method NaN"
两种解决方案
1.
const data = XLSX.read(byteArray, { type: 'array' });
更改为
const data = XLSX.read(byteArray, { type: 'buffer' });
HernanATN 的答案也解决了我的问题,并且使用与 GAS 更兼容的不同版本的 xlsx 似乎更值得信赖
它实际上实现了 ECMAScript 标准的定制子集,不一定符合 V8 引擎的最新功能(在现代浏览器和运行时中以 Node、Deno、Bun 的形式出现)。所以你不能假设现代的库会工作:幸运的是,对于足够旧的库来说,一点点的尝试和错误,你就会找到一个合适的版本来使用! 我发现 XLSX 0.17.0 在 GAS 中工作,因此,您应该获取并评估该版本:
eval(UrlFetchApp.fetch('https://cdn.sheetjs.com/xlsx-0.17.0/package/dist/xlsx.full.min.js').getContentText());
function parseExcelToJson() {
eval(UrlFetchApp.fetch('https://cdn.sheetjs.com/xlsx-0.17.0/package/dist/xlsx.full.min.js').getContentText());
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(e.stack);
Logger.log("Error during parsing: " + e.message);
}
}
console.log(parseExcelToJson())
输出:
3:17:11 Información [ { Item: 1,
Symbol: 'CBO',
'Symbol Name': 'CBO (Listing Market - NYSE - Networks A/E) Common Stock',
Product: 'CTS' },
{ Item: 2,
Symbol: 'CBX',
'Symbol Name': 'CBX (Listing Market NYSE Networks AE) Common Stock',
Product: 'CTS' },
{ Item: 3,
Symbol: 'IBO',
'Symbol Name': 'IBO (Listing Market - NYSE Amex Network B F) Common Stock',
Product: 'CTS' },
{ Item: 4,
Symbol: 'IGZ',
'Symbol Name': 'IGZ (Listing Market NYSE Arca Network B F) Common Stock',
Product: 'CTS' },
{ Item: 5,
Symbol: 'NTEST',
'Symbol Name': 'NYSE Tick Pilot Test Sym-Control',
Product: 'CTS' },
{ Item: 6,
Symbol: 'ZBZX',
'Symbol Name': 'BATS BZX Exchange test issue',
Product: 'CTS' },
{ Item: 7,
Symbol: 'ZEXIT',
'Symbol Name': 'IEX Test Company Test Symbol Two for IEX',
Product: 'CTS' },
{ Item: 8,
Symbol: 'ZIEXT',
'Symbol Name': 'IEX Test Company Test Symbol One for IEX',
Product: 'CTS' },
{ Item: 9,
Symbol: 'ZTEST',
'Symbol Name': 'BATS BZX Exchange Common Stock (test issue)',
Product: 'CTS' },
{ Item: 10,
Symbol: 'ZVV',
'Symbol Name': 'NYSE ARCA test stock',
Product: 'CTS' },
{ Item: 11,
Symbol: 'ZXIET',
'Symbol Name': 'IEX Test Company Test Symbol Three for IEX',
Product: 'CTS' },
{ Item: 12,
Symbol: 'ZAZZT',
'Symbol Name': 'Tick Pilot Test Stock Class A Common Stock',
Product: 'UTP' },
{ Item: 13,
Symbol: 'ZBZZT',
'Symbol Name': 'Test Pilot Test Stock Class B Common Stock',
Product: 'UTP' },
{ Item: 14,
Symbol: 'ZCZZT',
'Symbol Name': 'Tick Pilot Test Stock Class C ',
Product: 'UTP' },
{ Item: 15,
Symbol: 'ZVZZC',
'Symbol Name': 'NASDAQ TEST STOCK Nextshares Test Security',
Product: 'UTP' },
{ Item: 16,
Symbol: 'ZVZZT',
'Symbol Name': 'NASDAQ TEST STOCK',
Product: 'UTP' },
{ Item: 17,
Symbol: 'ZWZZT',
'Symbol Name': 'NASDAQ TEST STOCK',
Product: 'UTP' },
{ Item: 18,
Symbol: 'ZXZZT',
'Symbol Name': 'NASDAQ TEST STOCK',
Product: 'UTP' } ]