Thanks to @Kunal Mukherjee for the help.
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};
In a Zapier Zap, I'm extracting data from Google Sheets and using JS to prettify it to later send in an email. I'm bumping into an error with the following message:
SyntaxError: Unexpected token ]
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)
I've successfully run this code from the most current version of Node back to Node v6.3.1 in different environments - local IDE, Repl.It IDE, and an online IDE set to Node v6.3.1. They all clear. I've also tried clearing the code of all ES6+ syntax (sans the example data)
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};
I'm expecting for the code to run, first off, then I'm expecting output.KPI
to be a prettified string.
Thanks for the time & help :)
This approach may be a little functional.
You need to first replace the ],
in every row to an empty string I have used this Regex to split it.
After that, I split the strings by whitespaces which are more than 2 characters.
Then finally I used .map
to project over the splitted items to parse it back and make an array of array.
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);