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

如何通过Google Apps Script从ShipStation获取已发货订单数据?

解决ShipStation已发货订单同步至Google表格的问题

问题根源

  • Host头无效:Google Apps Script的UrlFetchApp会自动根据请求URL生成Host头,手动添加会触发错误,必须移除。
  • 授权格式错误:ShipStation API要求授权头为Basic [Base64编码的API_KEY:SECRET_KEY],而非直接写apiKey字符串。
  • 冗余参数:GET请求无需设置contentType,可删除该参数。

修正后的完整代码

function syncShippedOrdersToSheet() {
  // 替换为你的ShipStation API密钥和密钥密码
  const apiKey = "你的API_KEY";
  const secretKey = "你的SECRET_KEY";
  
  // 生成符合要求的授权字符串
  const authString = Utilities.base64Encode(`${apiKey}:${secretKey}`);
  const apiUrl = "https://ssapi.shipstation.com/shipments";

  const requestOptions = {
    method: "GET",
    headers: {
      "Authorization": `Basic ${authString}`
    }
  };

  try {
    // 调用API获取已发货数据
    const response = UrlFetchApp.fetch(apiUrl, requestOptions);
    const shipmentData = JSON.parse(response.getContentText());
    
    // 写入Google表格(示例:写入当前表格的Sheet1)
    const targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
    if (!targetSheet) {
      Logger.log("未找到指定工作表");
      return;
    }
    
    // 清空现有数据(可选操作)
    targetSheet.clearContents();
    
    // 写入表头
    const headers = Object.keys(shipmentData.shipments[0]);
    targetSheet.appendRow(headers);
    
    // 逐行写入订单数据
    shipmentData.shipments.forEach(shipment => {
      const rowData = headers.map(header => shipment[header] || "");
      targetSheet.appendRow(rowData);
    });
    
    Logger.log("已发货订单数据同步完成");
  } catch (error) {
    Logger.log(`同步出错:${error.message}`);
  }
}

补充说明

  • 若需要筛选特定时间段的已发货订单,可在API URL后添加查询参数,例如:https://ssapi.shipstation.com/shipments?startDate=2024-05-01&endDate=2024-05-31
  • 如需获取订单的更详细信息,可调用ShipStation的/orders接口,通过orderId关联查询。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 09:10:54