简体   繁体   中英

The script is working fine, but not in 'google-apps-script'

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) Syntax errors in script: Missing ; before statement. (line 23) 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 :

在此处输入图片说明

Tanaike's solution should solve your problem. But allow me to add some more background information. Google Apps Script has been around since 2009 and it is an implementation of Ecmascript 5. The let statement and the arrow operator are only supported in Ecmascript 6 and up.

There is a feature request for App Script to support EcmaScript 6 on Google's issue tracker. A lot of us in the Apps Script community have been clamoring for Ecmascript 6 support. You can lend your voice to the cause by starring the issue.

Here's a link to it: https://issuetracker.google.com/issues/36764074

If you want to use this script at Google Apps Script, let and the arrow operator cannot be used. So how about the following modification?

Modification points :

  1. Replace from let to var .
  2. Replace from data.forEach(line => { to data.forEach(function(line){ .

Modified script :

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;
}

Result :

{
  "test1": {
    "element1": "price1",
    "element2": "price2",
    "element3": "price3"
  },
  "test2": {
    "anotherele1": "anotherprice1",
    "anotherele2": "anotherprice2",
    "anotherele3": "anotherprice3"
  },
  "test3": {
    "aaa": 123,
    "bbb": 345,
    "ccc": 678
  }
}

Reference :

If I misunderstand your question, I'm sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM