You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Apps Script导入Shopify数据到Google Sheet遇问题求助

Shopify数据导入Google Sheet问题解决与实现方案

1. 修复产品字段提取的两类错误

错误原因分析

  • TypeError: products.forEach不是函数:你的products变量并非数组。Shopify产品列表API的返回结构是顶层对象包含products数组,若直接把整个响应解析结果赋值给products,会导致它是对象而非数组,无法调用forEach方法。
  • console.log(productTitle)返回undefined:你访问的字段名错误,Shopify产品的标题字段是title,而非productTitle。

修正后的完整代码示例

function importShopifyProducts() {
  // 配置Shopify信息
  const shopDomain = 'your-shop-name.myshopify.com';
  const apiKey = 'your-api-key';
  const password = 'your-api-password';
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('产品列表');
  
  // 调用Shopify产品API
  const url = `https://${apiKey}:${password}@${shopDomain}/admin/api/2024-07/products.json`;
  const response = UrlFetchApp.fetch(url, {
    headers: { 'Content-Type': 'application/json' }
  });
  
  // 解析响应,正确取出products数组
  const data = JSON.parse(response.getContentText());
  const products = data.products; // 这里才是真正的产品数组
  
  // 准备要写入的数据(按需提取字段)
  const outputData = [['标题', 'SKU', '价格', '库存']]; // 表头
  products.forEach(product => {
    // 正确访问产品字段
    const title = product.title;
    const sku = product.variants[0].sku; // 取第一个变体的SKU
    const price = product.variants[0].price;
    const inventory = product.variants[0].inventory_quantity;
    
    outputData.push([title, sku, price, inventory]);
  });
  
  // 写入Google Sheet
  sheet.clearContents();
  sheet.getRange(1, 1, outputData.length, outputData[0].length).setValues(outputData);
}

关键注意点

  • API版本:示例使用2024-07版本,需和你的Shopify后台API版本保持一致,避免兼容性问题。
  • 变体处理:若产品有多变体,需循环product.variants数组获取每个变体信息,示例仅取第一个变体。
  • 权限配置:确保Shopify API密钥拥有read_products权限。

2. Shopify财务报表导入Google Sheet的方法

方法一:用Reports API自动导入

Shopify Admin API提供Reports端点,可获取各类财务报表数据,示例代码如下:

function importShopifyFinancialReport() {
  const shopDomain = 'your-shop-name.myshopify.com';
  const apiKey = 'your-api-key';
  const password = 'your-api-password';
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('财务报表');
  
  // 调用销售报表API(可替换为其他报表类型)
  const url = `https://${apiKey}:${password}@${shopDomain}/admin/api/2024-07/reports.json?report_type=sales_over_time&start_date=2024-01-01&end_date=2024-06-30`;
  const response = UrlFetchApp.fetch(url, {
    headers: { 'Content-Type': 'application/json' }
  });
  
  const data = JSON.parse(response.getContentText());
  const report = data.reports[0];
  
  // 整理报表数据
  const outputData = [['日期', '总销售额', '订单数']];
  report.data.rows.forEach(row => {
    outputData.push([row.date, row.total_sales, row.orders_count]);
  });
  
  // 写入Sheet
  sheet.clearContents();
  sheet.getRange(1, 1, outputData.length, outputData[0].length).setValues(outputData);
}
  • 报表类型:可替换report_type参数,比如financial_summary(财务汇总)、sales_by_product(按产品销售)等。
  • 日期筛选:通过start_date和end_date参数指定所需时间段的数据。

方法二:手动导出CSV后导入

若无需自动化,可直接在Shopify后台操作:

  1. 进入Shopify后台报告页面,选择目标财务报表。
  2. 点击右上角导出,选择CSV格式下载。
  3. 在Google Sheet中点击文件 > 导入 > 上传,选择下载的CSV文件,配置导入选项后完成导入。

内容的提问来源于stack exchange,提问作者Maicol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 11:55:18