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

时间:2017-11-08 22:06:02

标签: javascript google-apps-script google-adwords

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 :

enter image description here

2 个答案:

答案 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和箭头运算符。那么下面的修改呢?

修改要点:

  1. let替换为var
  2. data.forEach(line => {替换为data.forEach(function(line){
  3. 修改后的脚本:

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

    参考:

    如果我误解了你的问题,我很抱歉。