0
votes

I am using the following code to move an entire row of data from one sheet to another, which works perfectly.

I am using AppSheet for users to access and edit the data which blocks the onEdit function in Sheets. AppSheet have suggested that I change this script to a "time based trigger" so that edits within the app have the same action as editing the column directly in "Sheets"

Is there a way to easily do this?

Thank you in advanced!

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

  if(s.getName() == "Stock" && r.getColumn() == 14 && r.getValue() == "Booked") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Bookings");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}
1

1 Answers

1
votes

As you can see the function name is onEdit() which means it's an edit trigger based function. In other words, it's supposed to get triggered and executed in each edit. But now you want it to be based on time and not on edit. So you can rename the function and create a time based trigger from top menu for this function.

Steps :

  1. Rename onEdit() function. Maybe you can rename it to moveRow()
  2. Select a time based trigger from top menu bar.
  3. Choose suitable schedule and this function from drop down.
  4. Save the changes.

Make sure you have given appropriate permissions. You can check this by trying to run the function once manually(By selecting it from top Select function drop down and clicking run/execute icon).