Skip to content

Instantly share code, notes, and snippets.

@victorwyee
Created April 15, 2020 17:15
Show Gist options
  • Select an option

  • Save victorwyee/492711b8e317a7fafc8de4331943d2b1 to your computer and use it in GitHub Desktop.

Select an option

Save victorwyee/492711b8e317a7fafc8de4331943d2b1 to your computer and use it in GitHub Desktop.
Move a row of data from one worksheet to another based on cell value, with Google Apps Script #googledocs
/**
* Moves row of data to another spreadsheet based on criteria in column 10.
* Each spreadsheet can be a "source" spreadsheet, or the "target" spreadsheet.
* Assumes there as many spreadsheets as there are criteria values, e.g.
* if the criteria are "R", "P", and "D", then there are three spreadsheets
* named "R", "P" and "D".
* Assumes that each spreadsheet has the same structure.
*/
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = SpreadsheetApp.getActiveRange();
// 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.
var colNumber = s.getLastColumn();
// Move row based on criteria in column 10, and if row is not the header.
if (colIndex == 10 && rowIndex != 1) {
// Get value from column 10, in the active row.
var status = s.getRange(rowIndex, colIndex).getValue();
// Do nothing if criteria value is not actually changed to something else.
if (s.getName() != status) {
// The target sheet is the one with the same name as the criteria value.
var targetSheet = ss.getSheetByName(status);
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(rowIndex, 1, 1, colNumber).moveTo(target);
s.deleteRow(rowIndex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment