javascript - Zapier Javascript : Unexpected token ] - working in IDE, Repl.it 和 Node v6.3.1

标签 javascript node.js zapier

解决方案:

感谢@Kunal Mukherjee 的帮助。

const pattern = /\](,)\s{2,}?/gm
let res = inputData.rows.replace(pattern, (match, group1, offset, string) => "]")
            .split(/\s{2,}/gm)
            .map(x => JSON.parse(x));

res = res[0];   //reassign to scrape an array layer
let resultString = '';

for (let i = 0; i < res[0].length; i += 1) {
  let cv = res[0][i];
  if (cv.length === 0) resultString += `    ${res[1][i]}: ${inputData.rows[2][i]}\n`
  else resultString += `${cv}\n    ${res[1][i]}: ${res[2][i]}\n`;
}

output = {KPI: resultString};
<小时/>

原帖:

问题

在 Zapier Zap 中,我从 Google Sheets 中提取数据,并使用 JS 对其进行美化,以便稍后在电子邮件中发送。我遇到错误并显示以下消息:

语法错误:意外的标记]

  stringOfArraysToArrayOfArrays (eval at <anonymous> (/var/task/index.js:52:23), <anonymous>:22:52)
  theFunction (eval at <anonymous> (/var/task/index.js:52:23), <anonymous>:29:18)
  eval (eval at <anonymous> (/var/task/index.js:52:23), <anonymous>:51:20)
  Domain.<anonymous> (/var/task/index.js:53:5)
  Domain.run (domain.js:242:14)
  module.exports.handler (/var/task/index.js:51:5)

我尝试过的

我已在不同的环境(本地 IDE、Repl.It IDE 以及设置为 Node v6.3.1 的在线 IDE)中成功地从最新版本的 Node 运行到 Node v6.3.1。他们都清楚了。我还尝试清除所有 ES6+ 语法的代码(没有示例数据)

数据示例

let inputData = {
  rows: `["Prioritized Tasks", "", "", "", "Operational Tasks", "", "", "", "Eight Dimensions", "", "", "", "", "", "", "", "Burn-Out", "", "", "", "", "", "", "", "", "", "Violations"], 
    ["Completion Rate", "Avg Completed", "Avg Total Scheduled", "Avg Time Spent", "Completion Rate", "Avg Completed", "Avg Total Scheduled", "Avg Time Spent", "Emotional", "Environmental", "Financial", "Intellectual", "Occupational", "Physical", "Social", "Spiritual", "Feeling Stressed", "Feeling Depleted", "Having Trouble Concentrating", "Feeling Forgetful", "Wanting to avoid social situations", "Feeling pessimistic", "Feeling cynical", "Feeling apathetic or disinterested", "Not feeling engaged with my work", "My overall energy level", "Temperance", "Silence", "Order", "Resolution", "Frugality", "Industry", "Sincerity", "Justice", "Moderation", "Cleanliness", "Tranquility", "Chastity", "Humility"], 
    ["70.33", "4", "6.67", "380", "3.67", "3.67", "66.67", "100", "8", "5.33", "5.67", "4.67", "4", "5", "4.67", "6.67", "1.33", "4", "5", "4.67", "3.33", "3.33", "1.33", "5", "6", "5.67", "0.3333333333", "0.3333333333", "0.3333333333", "0", "1", "0", "0", "0", "0", "0.3333333333", "0.3333333333", "0.3333333333", "0.3333333333"]`
}

产生错误的代码

function stringOfArraysToArrayOfArrays(string) {
  let arrayPointers = [0, 1];
  let arrOfArr = [];
  
  for (let i = 0; i < string.length; i += 1) {
    let cv = string[i];
    if (cv === "[") arrayPointers[0] = i;
    else if (cv === "]") {
      arrayPointers[1] = i + 1;
      arrOfArr.push(string.slice(arrayPointers[0], arrayPointers[1]));
      arrOfArr[arrOfArr.length - 1] = eval(arrOfArr[arrOfArr.length - 1]);
    }
  }

  return arrOfArr;
}

inputData.rows = stringOfArraysToArrayOfArrays(inputData.rows);

let resultString = '';

for (let i = 0; i < inputData.rows[0].length; i += 1) {
  let cv = inputData.rows[0][i];
  if (cv.length === 0) resultString += '    ' + inputData.rows[1][i] + ':  ' + inputData.rows[2][i] + '\n';
  else resultString += cv + '\n    ' + inputData.rows[1][i] + ':  ' + inputData.rows[2][i] + '\n';
}

output = {KPI: resultString};

预期结果

我期望代码首先运行,然后我期望 output.KPI 成为一个美化的字符串。

感谢您的时间和帮助:)

最佳答案

这种方法可能有点实用。

您需要首先将每一行中的 ], 替换为空字符串我已经使用了这个 Regex拆分它。

之后,我用超过 2 个字符的空格分割字符串。

最后,我使用 .map 投影分割的项目,将其解析回来并生成一个数组。

const inputData = {
  rows: `["Prioritized Tasks", "", "", "", "Operational Tasks", "", "", "", "Eight Dimensions", "", "", "", "", "", "", "", "Burn-Out", "", "", "", "", "", "", "", "", "", "Violations"], 
    ["Completion Rate", "Avg Completed", "Avg Total Scheduled", "Avg Time Spent", "Completion Rate", "Avg Completed", "Avg Total Scheduled", "Avg Time Spent", "Emotional", "Environmental", "Financial", "Intellectual", "Occupational", "Physical", "Social", "Spiritual", "Feeling Stressed", "Feeling Depleted", "Having Trouble Concentrating", "Feeling Forgetful", "Wanting to avoid social situations", "Feeling pessimistic", "Feeling cynical", "Feeling apathetic or disinterested", "Not feeling engaged with my work", "My overall energy level", "Temperance", "Silence", "Order", "Resolution", "Frugality", "Industry", "Sincerity", "Justice", "Moderation", "Cleanliness", "Tranquility", "Chastity", "Humility"], 
    ["70.33", "4", "6.67", "380", "3.67", "3.67", "66.67", "100", "8", "5.33", "5.67", "4.67", "4", "5", "4.67", "6.67", "1.33", "4", "5", "4.67", "3.33", "3.33", "1.33", "5", "6", "5.67", "0.3333333333", "0.3333333333", "0.3333333333", "0", "1", "0", "0", "0", "0", "0.3333333333", "0.3333333333", "0.3333333333", "0.3333333333"]`
};

const pattern = /\](,)\s{2,}?/gm
const res = inputData.rows.replace(pattern, (match, group1, offset, string) => "]")
            .split(/\s{2,}/gm)
            .map(x => JSON.parse(x));

const output = { KPI: res };

console.log(output);

关于javascript - Zapier Javascript : Unexpected token ] - working in IDE, Repl.it 和 Node v6.3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56081202/

相关文章:

javascript - AJAX 调用和异步

javascript - IE9 的占位符脚本默认情况下不起作用。但在离开输入后工作

javascript - res.render() 函数渲染 ejs 页面不会刷新 UI,但 ejs 页面会被调用

zapier - 在 zapier 的代码循环中使用 fetch

mysql - 在一个 SQL 查询中组合 INSERT 和 SELECT (Zapier)

javascript - 在 Javascript 中创建按键事件

javascript - 从图像 URL 转换时 Base64 图像未打开

node.js - 仅在 NodeJs 上构建网站,并且 Express 阻止通过 http 的请求

javascript - 嵌套的 EJS 标签。不工作

python - 以编程方式更新 Zapier 存储