Google Script: Conditionally copy rows from one spreadsheet to another

前端 未结 3 1943
死守一世寂寞
死守一世寂寞 2021-01-14 21:17

I\'ve found the basic solution https://stackoverflow.com/a/4809413/1438650 but it lacks both the conditional and non-contiguous aspects of my project. I need the the copy to

3条回答
  •  猫巷女王i
    2021-01-14 21:27

    Blockquote

    This is my Coding, I think this would help you.

    function GetData() {
      var source = SpreadsheetApp.getActiveSpreadsheet();
      var target = SpreadsheetApp.openById("0Aj7gIt4f65xidEN1R1lxZksxeFRBYkdHWmVtdE5aOGc");
      var lastRow = source.getLastRow();
      var source_sheet = source.getSheetByName("Source");
      var target_sheet = target.getSheetByName("Target");
    
      var source_range = source_sheet.getDataRange();
      var target_range = target_sheet.getDataRange();
    
      var i = 2;
      while (i <= lastRow) {
      if (source_sheet.getRange("D"+i).getValue() == "Over Due" ) {
        var ItemNo = source_sheet.getRange("A"+i).getValue();
        var UserName = source_sheet.getRange("B"+i).getValue();
        var Location = source_sheet.getRange("C"+i).getValue();
        var Status = source_sheet.getRange("D"+i).getValue();
        var Comments = source_sheet.getRange("E"+i).getValue();
        var data = [ItemNo,UserName,Location,Status,Comments];
        target_sheet.appendRow(data);
        i++;
        } else {
          i++;
          }
        }
      }
    

提交回复
热议问题