I am wondering if you can help me modiy I script I came across a while ago that uses the onEdit trigger. The script basically looks at a cell value and moves the data to another worksheet on the same spreadsheet. This would be so helpful for my googlespreadsheet that stores data entered on a form. What I am looking for is to move data to another sheet based on updates made through the form. I have a workflow that requires approval. When my row is updated through the form with an Approved status, I'd like for the row to 1st be moved to my Archive Sheet and then delete the row.
Here is the script I found, that works well if I manually update the contents on column 9 (Column 9 has the Approval Decision). Column 41 has the name of the sheet to where to copy the row to. In my case, I named it Archive. Again, the script works fine if I manually change the status, but it does not work when the cell is updated through the form on submit.
/**
* Moves row of data to another spreadsheet based on criteria in column 9 to sheet with same name as the value in column 42. We have a formula that evalues whether a form submission has been approved. If it is in status of blank or Denied, the cell in Column41 will be blank. If the status of the form submission is approved, the formula is set to update the cell with "Archive". This Archive is the name of the worksheet where the row will be moved to. The source row will then be deleted, thus cleaning up the active FormMule sheet removing the "already processed" data.
*/
function onEdit(e) {
// see Sheet event objects docs
var ss = e.source;
var s = ss.getActiveSheet();
var r = e.range;
// to let you modify where the action and move columns are in the form responses sheet
var actionCol = 9; // This is the Action you want or the cell that will trigger the move of row to new spreadsheet. When this cell is updated/changed to approve, this will be the trigger to move the data to new sheet.
var nameCol = 41; // This is the column that you want to check (i.e.: We want to move the rows where in Column X? or where the Approval Decision = Approved. For our process, this means it has been approved and we can archive
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
// -1 to drop our action/status column
var colNumber = s.getLastColumn()-1;
// if our action/status col is changed to Approved do stuff
if (e.value == "Approved" && colIndex == actionCol) {
// get our target sheet name - in this example we are using the priority column
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
// if the sheet exists do more stuff
if (ss.getSheetByName(targetSheet)) {
// set our target sheet and target range
var targetSheet = ss.getSheetByName(targetSheet);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
// get our source range/row
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
// new sheets says: 'Cannot cut from form data. Use copy instead.'
sourceRange.copyTo(targetRange);
// ..but we can still delete the row after
s.deleteRow(rowIndex);
// or you might want to keep but note move e.g. r.setValue("moved");
}
}
}