function main() {
var data = [['test1', 'element1', 'price1'], ['', 'element2', 'price2'], ['', 'element3', 'price3'], ['','' ,'' ], ['test2', 'anotherele1', 'anotherprice1'], ['', 'anotherele2', 'anotherprice2'], ['', 'anotherele3', 'anotherprice3'], ['', '', ''], ['test3', 'aaa', 123.0], ['', 'bbb', 345.0], ['', 'ccc', 678.0], ['', '', ''], ['','' , '']]
var test = parseData(data)
Logger.log(test)
}
function isBlank(line) {
return line[0].trim() === '' && line[1].trim() === '';
}
function parseData(data) {
const output = {};
let currentGroupName = '';
data.forEach(line => {
if (isBlank(line)){
return;
}
if (line[0].trim().length > 0) {
currentGroupName = line[0].trim();
}
output[currentGroupName] = output[currentGroupName] || {};
output[currentGroupName][line[1]] = line[2];
});
return output;
}
The following JS script is working perfectly fine whenever I run it on my computer. However, if I try to run as Preview
(sort of Adwords-console) on Adwords, I got an synthax error Syntax errors in script: Missing ; before statement. (line 23)
. Be aware that line 23 is simply let currentGroupName = '';
. Is there a clean way to fix that? This code is adapted to be testing out on your own computer as well as with google-apps-scripts.
Here is a picture of my problem :
答案 0 :(得分:2)
Tanaike的解决方案应该可以解决您的问题。但请允许我添加更多背景信息。 Google Apps脚本自2009年开始出现,它是Ecmascript 5的一个实现。let
语句和箭头运算符仅在Ecmascript 6及更高版本中受支持。
App Script的功能请求支持Google问题跟踪器上的EcmaScript 6。我们在Apps脚本社区中的很多人一直在呼吁为Ecmascript 6提供支持。你可以通过主演这个问题来为这个事业发声。
这是一个指向它的链接: https://issuetracker.google.com/issues/36764074
答案 1 :(得分:1)
如果您想在Google Apps脚本中使用此脚本,则无法使用let
和箭头运算符。那么下面的修改呢?
let
替换为var
。data.forEach(line => {
替换为data.forEach(function(line){
。function main() {
var data = [['test1', 'element1', 'price1'], ['', 'element2', 'price2'], ['', 'element3', 'price3'], ['','' ,'' ], ['test2', 'anotherele1', 'anotherprice1'], ['', 'anotherele2', 'anotherprice2'], ['', 'anotherele3', 'anotherprice3'], ['', '', ''], ['test3', 'aaa', 123.0], ['', 'bbb', 345.0], ['', 'ccc', 678.0], ['', '', ''], ['','' , '']]
var test = parseData(data)
Logger.log(test)
}
function isBlank(line) {
return line[0].trim() === '' && line[1].trim() === '';
}
function parseData(data) {
const output = {};
var currentGroupName = '';
data.forEach(function(line){
if (isBlank(line)){
return;
}
if (line[0].trim().length > 0) {
currentGroupName = line[0].trim();
}
output[currentGroupName] = output[currentGroupName] || {};
output[currentGroupName][line[1]] = line[2];
});
return output;
}
{
"test1": {
"element1": "price1",
"element2": "price2",
"element3": "price3"
},
"test2": {
"anotherele1": "anotherprice1",
"anotherele2": "anotherprice2",
"anotherele3": "anotherprice3"
},
"test3": {
"aaa": 123,
"bbb": 345,
"ccc": 678
}
}
如果我误解了你的问题,我很抱歉。