使用Google Apps Script导入Shopify报告至Google Sheets遇JSON解析错误求助
将Shopify报告自动导入Google Sheets的问题与解决方案
问题描述
尝试通过Shopify API自动导入店铺报告到Google Sheets,使用的Google Apps Script代码如下:
function importShopifyReport() { var shopUrl = 'shop.myshopify.com'; var apiKey = 'xxx'; var apiPassword = 'xxx'; var spreadsheetId = 'spreadsheet'; var sheetName = 'test'; //var store_url = 'https://shop.myshopify.com/admin/reports/2739306761' var url = 'https://shop.myshopify.com/admin/reports/2739306761'; var response = UrlFetchApp.fetch(url, { "method": "GET", 'contentType': 'application/json', 'header': { "Authorization": "Basic " + Utilities.base64Encode("apiKey:apiPassword") } }); var data = JSON.parse(response.getContentText()); var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName); var headers = Object.keys(data.results[0]); var values = data.results.map(function(item) { return headers.map(function(header) { return item[header]; }); }); sheet.getRange(1, 1, 1, headers.length).setValues([headers]); sheet.getRange(2, 1, values.length, values[0].length).setValues(values); }
运行时出现错误:
SyntaxError: Unexpected token '<', "<html> "... is not valid JSON
错误原因分析
- 请求URL错误:直接访问后台报告页面(
/admin/reports/xxx)返回的是HTML页面,而非API接口的JSON数据。Shopify API需要使用带版本号的端点格式:https://{shop_url}/admin/api/{api_version}/reports/{report_id}.json - 授权字符串错误:代码中直接拼接了字符串
"apiKey:apiPassword",没有使用实际的变量值,导致授权失败,返回HTML登录页面 - 请求头拼写错误:
header应为headers(复数形式),否则请求头无法正确识别 - 未指定API版本:Shopify API要求必须指定有效版本号(如
2024-07),否则会返回重定向或错误页面
修正后的代码
function importShopifyReport() { var shopUrl = 'shop.myshopify.com'; var apiKey = 'xxx'; // 替换为你的API Key var apiPassword = 'xxx'; // 替换为你的API密码 var apiVersion = '2024-07'; // 使用最新稳定API版本 var reportId = '2739306761'; var spreadsheetId = '你的表格ID'; // 替换为Google Sheets的ID var sheetName = 'test'; // 构建正确的API请求URL var url = `https://${shopUrl}/admin/api/${apiVersion}/reports/${reportId}.json`; var response = UrlFetchApp.fetch(url, { method: "GET", contentType: 'application/json', headers: { // 修正为复数headers "Authorization": "Basic " + Utilities.base64Encode(apiKey + ":" + apiPassword) // 使用变量拼接授权字符串 } }); var data = JSON.parse(response.getContentText()); var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName); if (!sheet) { sheet = SpreadsheetApp.openById(spreadsheetId).insertSheet(sheetName); } // 清空现有内容(可选) sheet.clearContents(); // 处理报告数据:Shopify Reports API返回的结构是data.report var reportData = data.report; // 提取表头和数据行(根据报告类型调整,这里以表格型报告为例) var headers = reportData.column_names; var values = reportData.rows; // 写入表头 sheet.getRange(1, 1, 1, headers.length).setValues([headers]); // 写入数据行 if (values.length > 0) { sheet.getRange(2, 1, values.length, values[0].length).setValues(values); } }
其他优质解决方案
- 使用Shopify内置集成:在Shopify后台的报告页面,直接点击「导出」→「Google Sheets」,无需编写代码即可实现自动同步
- 采用GraphQL API:如果需要自定义复杂报告,使用Shopify GraphQL Admin API可以更灵活地查询数据,适合个性化需求
- 设置定时触发:在Google Apps Script中配置时间驱动触发器,让脚本按天/周自动执行,实现定期同步报告
- 使用第三方工具:如Zapier、Make等自动化工具,通过可视化配置连接Shopify和Google Sheets,快速搭建同步流程
内容的提问来源于stack exchange,提问作者AoT_1503
相关产品推荐
相关产品推荐

