所有功能均同时执行

时间:2019-07-02 03:56:18

标签: google-apps-script google-sheets

我正在尝试从一张纸复制数据,将其粘贴到另一张纸,然后删除附有“标志”的行(D列)。当我执行此代码时,它只是删除标志而不是整个行。这里发生了什么?这些功能似乎以错误的顺序执行。

使用Utilities.sleep()以及

  • 将两者分离为不同的功能
  • 使用for循环写出我要删除的所有列
  • 在执行下一个操作之前将flush应用到这些功能
    //copies the code
    function myFunction() {
        var url = 'https://docs.google.com/spreadsheets/d/1ie2Fyj2piVV8XC0XUGX-fTAHcQ0UUUdec4Mtr1QdXtQ/edit#gid=293227072'
        var ss = SpreadsheetApp.openByUrl(url)
        var sheet = ss.getSheetByName('Bad Emails Input')
        var range = sheet.getRange("K:N")
        var values = range.getValues()
        var target = ss.getSheetByName("worklist")
        target.getRange("A:D").setValues(values)
        SpreadsheetApp.flush()
        Utilities.sleep(5 * 1000)
    }


    //deletes the rows with flags in them
    function myFunction2() {
        var url = 'https://docs.google.com/spreadsheets/d/1ie2Fyj2piVV8XC0XUGX-fTAHcQ0UUUdec4Mtr1QdXtQ/edit#gid=293227072'
        var ss = SpreadsheetApp.openByUrl(url)
        var sheet = ss.getSheetByName('Bad Emails Input')
        var range = sheet.getRange("K:N")
        var values = range.getValues()
        var target = ss.getSheetByName("worklist")
        Utilities.sleep(6 * 1000)
        var oldnews = target.getRange("D:D")
        for (i = oldnews.length; i > 0; i--){
            if (oldnews[i] !== undefined) {
                target.getRange(i,1)
                target.getRange(i,2)
                target.getRange(i,3)
                target.getRange(i,4)
            } 
        }
        SpreadsheetApp.flush()
    }

2 个答案:

答案 0 :(得分:1)

不清楚您要问什么。所以我只是猜中了一些。

function myFunction() {
  var ss = SpreadsheetApp.openById("id");
  var sheet = ss.getSheetByName('Bad Emails Input')
  var range = sheet.getRange(1,11,sheet.getLastRow(),4);
  var values = range.getValues()
  var target = ss.getSheetByName("worklist")
  target.getRange(1,1,values.length,values[0].length).setValues(values);
  SpreadsheetApp.flush();
  var d=0;
  for(var i=0;i<values.length;i++) {
    if(values[i][3]) {
      target.deleteRow(i+1-d++);
    }
  }
}

答案 1 :(得分:1)

尝试以下代码:

function myFunction() {
    var url = 'https://docs.google.com/spreadsheets/d/1ie2Fyj2piVV8XC0XUGX-fTAHcQ0UUUdec4Mtr1QdXtQ/edit#gid=293227072'
    var ss = SpreadsheetApp.openByUrl(url);
    var sheet = ss.getSheetByName('Bad Emails Input');
    var range = sheet.getRange("K:N");
    var values = range.getValues();
    var target = ss.getSheetByName("worklist");
    target.getRange("A:D").setValues(values);
    SpreadsheetApp.flush();
    //If you have a header row change the below to A2:D and count to 2
    var data=ss.getSheetByName("worklist").getRange("A:D").getValues(),count=1;
    for (i in data) {
    var rowdata=data[i];
    if (!rowdata[0] || rowdata[0]==''){break;}
    if (rowdata[3] == 'flag') {//change the flag to however it is in column D
    ss.getSheetByName("worklist").deleteRow(count);
    count++;
    }
    }
}