简体   繁体   English

该脚本可以正常运行,但不能在“ google-apps-script”中使用

[英]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. 每当我在计算机上运行以下JS脚本时,它们都可以正常运行。 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) 但是,如果我尝试在Adwords上以Preview (某种Adwords-console)的身份运行,则会出现synthax错误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) . Syntax errors in script: Missing ; before statement. (line 23) Be aware that line 23 is simply let currentGroupName = ''; 请注意,第23行只是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. 该代码适用于在您自己的计算机以及google-apps-scripts上进行测试。

Here is a picture of my problem : 这是我的问题的照片:

在此处输入图片说明

Tanaike's solution should solve your problem. Tanaike的解决方案应该可以解决您的问题。 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. Google Apps脚本自2009年以来一直存在,它是Ecmascript 5的实现。仅Ecmascript 6及更高版本支持let语句和arrow运算符。

There is a feature request for App Script to support EcmaScript 6 on Google's issue tracker. Google的问题跟踪器对App Script提出了功能要求,以支持EcmaScript 6。 A lot of us in the Apps Script community have been clamoring for Ecmascript 6 support. Apps Script社区中的许多人都强烈要求Ecmascript 6支持。 You can lend your voice to the cause by starring the issue. 您可以通过为该问题加注星标来表达自己的看法。

Here's a link to it: https://issuetracker.google.com/issues/36764074 这是它的链接: 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. 如果要在Google Apps脚本中使用此脚本,则不能使用let和箭头运算符。 So how about the following modification? 那么下面的修改呢?

Modification points : 修改要点:

  1. Replace from let to var . let替换为var
  2. Replace from data.forEach(line => { to data.forEach(function(line){ . data.forEach(line => {替换为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. 如果我误解了您的问题,对不起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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