简体   繁体   中英

Move row to new tab after sheet updates automatically

I need to move rows from a tab based on a cell including "lep". The problem is that it doesn't read the info already on the sheet when it's opened (since it is a sheet that automatically updates new sales from Shopify via Zapier).

What would be the solution for this? I tried onEdit & onOpen already and neither work.

function onOpen(e) {
  // assumes source data in sheet named SHOPIFY
  // target sheet of move to named ORG
  // test column with yes is col 17 or Q
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = e.source.getActiveSheet();
  var r = e.source.getActiveRange();

  if(s.getName() == "SHOPIFY" && r.getColumn() == 17 && r.getValue().includes("lep")) {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("ORG");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

An onEdit() simple trigger will only run when the user hand edits the spreadsheet.

An onOpen() simple trigger will only run when you open the spreadsheet in a web browser , and its event object does not include information about any recent changes in the spreadsheet.

To detect changes made by Zapier you will need an on change installable trigger . See the moveRowsFromSpreadsheetToSpreadsheet_ script for an example of how to do that.

function moveRows() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('SHOPIFY');
  const tsh = ss.getSheetByName('ORG')
  const vs = sh.getDataRange().getValues();//you may wish to change the range to not include headers
  let d=0;
  vs.forEach((r,i)=>{
    if(r[16].toString().includes("lep")) {
      tsh.appendRow(r);
      sh.deleteRow(i+1-d++);
    }
  });
}

function onMyOpen() {
  //needs installable trigger
  moveRows();
}

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