Pages

Sunday, February 24, 2019

Move Google Sheet row to anther sheet using script


Moving a Google Sheet row to another sheet using script

This lesson will explain you how to move a row in a google sheet to anther sheet with the help of a script.

Example

Example Google Sheet contains three sheets called Main,Pass and Fail.It is required to move row  from Main sheet to either Pass sheet or Fail sheet based on the selection make from the Drop-Down list "Status". 


Image 01

According to the above image when you select "PASS" from the drop-down list then press the Purple button to run the following script.Then the A3 row moves to the pass sheet and it is deleted from the current sheet "Main".
You can automate this task by adding timestamp this script and it is up to you.Please see below to get the script for this task.

Once you go to the script editor copy and paste following code inside your script editor.

function myFunction() {
 // moves a row from a sheet to another when a magic value is entered in a column
 // adjust the following variables to fit your needs
  var sheetNameToWatch ='Main';
 var columnNumberToWatch = 8;
 // column A = 1, B = 2, etc…
 var valueToWatch = 'PASS';
 var sheetNameToMoveTheRowTo = 'PASS';
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = SpreadsheetApp.getActiveSheet();
 var range = sheet.getActiveCell();
 if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
   var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
   var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
   sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).copyTo(targetRange);
   // sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
   sheet.deleteRow(range.getRow());
 }
}

From this blog it is expected to give basic understanding about copy to/move to rows between sheet with the help of java script.you can modify this code in various ways and try to develop further.

No comments:

Post a Comment

Find records in multiple sheets

This lesson will explain to you how to use query function to search specific record from multiple sheets. According to our example we h...

Contact Form

Name

Email *

Message *