简体   繁体   中英

Google Sheet Apps Script: How do I edit the Apps Script below to only copy over data from Column A to L and P to S?

How do I edit the Apps Script below to only copy over data from Column A to L and P to S? (Basically, I want it to skip Column M, N, O and all the columns after S.

function onEdit(event) {
  // assumes source data in sheet named Tab 1
  // target sheet of move to named Tab 2
  // test column with yes/no is col 20 or T
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  if(s.getName() == "Tab 1" && r.getColumn() == 20 && r.getValue() == "yes") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Tab 2");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

Thank you in advance, Best regards, Robert

Copy both ranges ( AL , PS ) separately to the different target ranges, as shown here:

function onEdit(e) {
  //e.source.toast("Entry");
  //console.log(JSON.stringify(e));
  const sh=e.range.getSheet();
  if(sh.getName()=="Sheet1" && e.range.columnStart==20 && e.value=="TRUE") {
    const tsh=e.source.getSheetByName('Sheet2');
    const nr=tsh.getLastRow()+1;
    sh.getRange(e.range.rowStart,1,1,12).moveTo(tsh.getRange(nr,1,1,12));
    sh.getRange(e.range.rowStart,16,1,4).moveTo(tsh.getRange(nr,16,1,4));
    sh.deleteRow(e.range.rowStart);
  }
}

Note : I used 'Sheet1' and 'Sheet2' and I also used standard values for check boxes instead of 'yes' and 'no'. I also used 'e' instead of 'event' and I used the data that is available in the event object.

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