Zapier - 输入数据排序

问题描述 投票:0回答:2

这是发生的事情:

我在Shopify商店中使用了一个非常酷的插件,允许您按照一系列步骤自定义您的产品。

为了说明enter image description here

问题是所有这些信息都没有为Zapier排序。基本上它显示如下:

问题enter image description here

它们作为“行项目属性值”和“行项目属性名称”出现,粘贴时它们作为数组出现。

我需要做的是将这些名称与其值匹配。如果可能的话,可以在Zapier的GUI中进行选择。

所以没有这些字段和值

"Line Properties Names" -> "Project Title","Project Description","Ebook Type"....

"Line Items Properties Values" -> "Sherlock Holmes","A story in London..", "Standard Book"...

拥有这些字段和值:

"Project Title" -> "Sherlock Holmes"
"Project Description" -> "A story in London.."
"Ebook Type" -> "Standard Ebook"

可能吗?

感谢您的时间

UPDATE

为澄清目的

因此,按此顺序有3种不同的产品。由[]分隔。产品中的值可能会有所不同,例如,如果客户决定不填写“项目详细信息”字段,则不会显示项目详细信息键和值。最终导致产品具有不同数量的键和值。

这是一个例子:(正如您所看到的,第一个产品具有与第二个产品不同的一组值)

输入数据

Input data Values: ["1","ebook1524837342394","Ebook~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~ebook","Technical Ebook","Technical 15K - $450.00","All Inclusive Package - $149.00","Cookbook Instant Pot"],["ebook1524837342394"],["Detective Story based in London......","Sherlock Holmes","No Addons","No Package","10000 Words - $270.00","Fiction Book","Ebook~~//www.shappify-cdn.com/images/282516/127828453/001_Ebook Standard 325x325.png~~ebook","ebook1524837304725","1","https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/7ddee14d6e5d6c5e4396981d1702c375.jpg"]

 Input data Keys: ["_master_builder","_builder_id","_builder_info","Ebook Type","Word Count","Upgrade","Project Title"],["_builder_id"],["Project Details","Project Title","Addons","Upgrade","Word Count","Ebook Type","_builder_info","_builder_id","_master_builder","Upload your file here"]

我想做的事

我希望将密钥与其值匹配,并能够在Zapier GUI上选择它们。

建议使用代码的当前输出

Output 1stpart

Output 2ndpart

Output 3rdpart

Output 4thpart

预期产出

[{"_master_builder":"1","_builder_id":"ebook1524837342394","_builder_info":"Ebook~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~ebook"...}]

感谢你的帮助

zapier
2个回答
1
投票

啊!所以这比起初看起来有点棘手,但它并不坏。有两个问题:

  • 您的数据以字符串形式出现
  • 该字符串由多个数组对象组成,但它本身不是有效的数组对象

因此,一旦我们正确地解析它,它就不会太糟糕。

// just used for testing outside zapier
// these are comma separated strings
const inputData = {
  keys: '_master_builder,_builder_id,_builder_info,Ebook Type,Word Count,Upgrade,Project Title,_builder_id,Project Details,Project Title,Addons,Upgrade,Word Count,Ebook Type,_builder_info,_builder_id,_master_builder,Upload your file here',
  values: '1,ebook1524837342394,Ebook~~//www.shappify-cdn.com/images/282516/127828455/001_Ebook Technical 325x325 (1).png~~ebook,Technical Ebook,Technical 15K - $450.00,All Inclusive Package - $149.00,Cookbook Instant Pot,ebook1524837342394,Detective Story based in London......,Sherlock Holmes,No Addons,No Package,10000 Words - $270.00,Fiction Book,Ebook~~//www.shappify-cdn.com/images/282516/127828453/001_Ebook Standard 325x325.png~~ebook,ebook1524837304725,1,https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/7ddee14d6e5d6c5e4396981d1702c375.jpg'
}

// arrays must be the same length
const zipArrays = (a, b) => {
  let res = {}
  a.forEach((val, i) => {
    res[val] = b[i]
  })
  return res
}

// have to convert strings to actual arrays
// this will blow up if any of the data has commas in it
const keys = inputData.keys.split(',')
const vals = inputData.values.split(',')

// now we have real arrays

const result = {}

// copy keys onto the result, overwriting old ones
Object.assign(result, zipArrays(keys, vals))

console.log(result)
/*
  { _master_builder: '1',
    _builder_id: 'ebook1524837304725',
    _builder_info: 'Ebook~~//www.shappify-cdn.com/images/282516/127828453/001_Ebook Standard 325x325.png~~ebook',
    'Ebook Type': 'Fiction Book',
    'Word Count': '10000 Words - $270.00',
    Upgrade: 'No Package',
    'Project Title': 'Sherlock Holmes',
    'Project Details': 'Detective Story based in London......',
    Addons: 'No Addons',
    'Upload your file here': 'https://cdn.shopify.com/s/files/1/0012/8814/2906/uploads/7ddee14d6e5d6c5e4396981d1702c375.jpg' }
*/

// return result

因为,有很多重复的键,所以输出小于输入。如果要以不同方式对输出进行分组,还可以调整此代码以更好地匹配输入。

希望有所帮助!


0
投票

这应该可以使用for循环。试试这个。

example中,将第一个框定义为值,将第二个框定义为键。在运行代码步骤之前删除var inputData...行。

var inputData = {"keys": ["First", "Second", "Third"],
                "values": ["One", "Two", "Three"]
                };
//Remove the lines above before pasting in the Code step. 
//You will need to configure it in the Zap.

var product = {};
for (var i = 0; i < inputData.keys.length; i++) {
  var commonkey = inputData.keys[i];
  product[commonkey] = inputData.values[i];
}
console.log(JSON.stringify([product]));
   
// already available in the zapier scope
output = [product]
© www.soinside.com 2019 - 2024. All rights reserved.