我正在使用 Google App Scripts 内置函数 UrlFetchApp.fetch,以便从 URL 请求数据。当您在浏览器中导航到此 URL 时,它只会启动 XLSX 文件的文件下载。如果我console.log响应,它只是一串乱码数据。如何将表示此 XLSX 文件内容的字符串解析为 JSON 数组?
我无法将此文件保存到驱动器并将其读回,我正在寻找一种直接在应用程序脚本中解析响应字符串的方法。以下代码使用 xlsx.full.min.js 库。我收到错误“解析期间出错:不支持的 ZIP 压缩方法 NaN”
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);
}
}
它可以在从存储库获取的 GAS 中导入和评估:
eval(UrlFetchApp.fetch('https://raw.githubusercontent.com/Stuk/jszip/refs/heads/main/dist/jszip.js').getContentText());
并且不一定符合 V8 引擎的最新功能,因此您需要一些尝试和错误才能找到每个库使用的正确版本。虽然 JSZlib 最新版本到目前为止运行良好,但我发现您至少需要返回到 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://raw.githubusercontent.com/Stuk/jszip/refs/heads/main/dist/jszip.js').getContentText());
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' } ]