【问题标题】:Extract data from Shopify into a Google Sheet using Google Apps Script使用 Google Apps 脚本将 Shopify 中的数据提取到 Google 表格中
【发布时间】:2022-10-13 02:35:39
【问题描述】:

我正在尝试获取 JSON 对象的特定产品字段以发送到谷歌表。日志告诉我 'console.log(productTitle)' 未定义,并且 'Error: TypeError: products.forEach' 不是函数。 Shopify 对象的结构如下。

    function getProducts() {

  //set up url
  const url = 'https://*****.myshopify.com/admin/'
  const endpoint = 'products.json'

  //set up parameters

  const params = {
    headers: {
      'Content-Type': 'application/json',
      'X-Shopify-Access-Token': '*****'

    },
    muteHttpExceptions: true

  }

  try {

    //call the url to fetch access token

    const response = UrlFetchApp.fetch(url + endpoint, params)

    //parse the response and get the access token

    const products = JSON.parse(response.getContentText())
    console.log(response)
    console.log(products)

    products.forEach(product => {

      const productTitle = product.products_title
      console.log(productTitle)

      const productId = product.products_id
      const productStatus = product.products_status



    })

    return result
  }

  catch (e) {
    console.log('Error: ' + e)
  }
}


    /* { products: 
       [ { id: 121345678910,
      title: 'Title',
      body_html: 'Body Text',
      vendor: 'Vendor Name',
      product_type: 'candyrack_generated',
      created_at: '2021-07-18T11:04:34-05:00',
      handle: 'extended-warranty-1',
      updated_at: '2022-10-11T09:15:18-05:00',
      published_at: '2021-07-18T11:04:34-05:00',
      template_suffix: 'water-pump',
      status: 'active',
      published_scope: 'web',
      tags: '',
      admin_graphql_api_id: 'gid://shopify/Product/121345678910',
      variants: 
       [ { product_id: 121345678910,
           id: 9876543210,
           title: 'Default Title',
           price: '9.95',
           sku: '',
           position: 1,
           inventory_policy: 'continue',
           compare_at_price: null,
           fulfillment_service: 'manual',
           inventory_management: null,
           option1: 'Default Title',
           option2: null,
           option3: null,
           created_at: '2021-07-20T08:43:11-05:00',
           updated_at: '2022-10-11T09:14:17-05:00',
           taxable: true,
           barcode: '',
           grams: 0,
           image_id: null,
           weight: 0,
           weight_unit: 'kg',
           inventory_item_id: 24681012,
           inventory_quantity: -708,
           old_inventory_quantity: -708,
           requires_shipping: false,
           admin_graphql_api_id: 'gid://shopify/ProductVariant/987654' } ],
      options: 
       [ { product_id: 121345678910,
           id: 909000,
           name: 'Title',
           position: 1,
           values: [Object] } ],
      images: 
       [ { product_id: 121345678910,
           id: 3693336,
           position: 1,
           created_at: '2022-04-03T08:43:29-05:00',
           updated_at: '2022-04-03T08:43:32-05:00',
           alt: null,
           width: 1080,
           height: 1080,
           src: 'http://cdn.shopify.com/s/files/1/0541/4132/13/products/freereplacements.png?v=164899',
           variant_ids: [],
           admin_graphql_api_id: 'gid://shopify/ProductImage/369333' } ],
      image: 
       { product_id: 121345678910,
         id: 3693336,
         position: 1,
         created_at: '2022-04-03T08:43:29-05:00',
         updated_at: '2022-04-03T08:43:32-05:00',
         alt: null,
         width: 1080,
         height: 1080,
         src: 'http://cdn.shopify.com/s/files/1/0541/4132/13/products/freereplacements.png?v=1648993',
         variant_ids: [],
         admin_graphql_api_id: 'gid://shopify/ProductImage/3693336' } }
    
    */

我想将不同的键拉入不同的列以填充所有产品的行。我还想知道如何访问财务报告以拉入表格。我确实成功返回了所有产品数据 'const products = JSON.parse(response.getContentText())' ,无法分离数据。谢谢你。

【问题讨论】:

    标签: javascript google-apps-script shopify shopify-app


    【解决方案1】:

    日志告诉我 'console.log(productTitle)' 未定义

    Google Apps 脚本代码不在浏览器中运行。它在 Google 服务器上的沙盒环境中运行,因此日志记录的工作方式与您在浏览器中可能习惯的工作方式略有不同。有关 Apps 脚本,请参阅 Logging 上的文档。

    “错误:TypeError:products.forEach”不是函数

    再看一下 Shopify 响应。它的结构如下:

    {
       products: [...]
    }
    

    因此,您的代码 products = JSON.parse(...) 实际上是引用响应对象,而不是其中的 products 数组。尝试这样的事情:

    const responseObj = JSON.parse(response.getContentText())
    const products = responseObj.products
    

    我想将不同的键拉入不同的列以填充所有产品的行。我还想知道如何访问财务报告以拉入表格。我确实成功返回了所有产品数据 'const products = JSON.parse(response.getContentText())' ,无法分离数据。

    使用forEach 循环遍历products 数组中的产品(一旦您解决了上述问题)以获取每个对象的各个属性,您就走在了正确的轨道上。

    要将数据写入 Google 表格,您需要探索 setValues() API。

    简而言之,您需要将 Shopify 响应中的数据结构化为二维数组,以将数据写入工作表。如果您不熟悉此概念,请在 Google 上搜索 two-dimensional array javascript;二维数组只是一种结构化表格数据的方式,就像电子表格中的那样。获得二维数组后,您将使用setValues() 将其写回 Google 表格。

    【讨论】:

    • 感谢@Adam 的回复和澄清。我仍然不确定如何在产品的 JSON 对象中提取特定的键或字段?
    • @Maicol道歉,我错过了你问题的最后一部分。我在上面编辑了我的答案以提供一些指导。
    猜你喜欢
    • 2021-10-19
    • 2015-12-29
    • 2022-10-25
    • 1970-01-01
    • 2022-10-18
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多