1

I could use IMPORTRANGE, but I want to actually place the values themselves into the cells, instead. For sheets with many rows, a lot of IMPORTRANGE formulas can cause lag and slow down performance.

Is there a way to copy data from a row in another sheet that has the same ID number?

1 Answer 1

1

Here is an example of how to do this. When "update" or "edit" is selected from the dropdown boxes, data from the other, designated tab's rows are copied into the current sheet.

Here is the script:

function onEdit(e) {

////UPDATE ROWS WITH DATA FROM REMOTE SHEET VIA SCRIPT
///the script below is adapted from https://mashe.hawksey.info/2015/03/google-sheets-move-a-row-of-data-to-another-sheet-based-on-cell-value/
  // see Sheet event objects docs
  // https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
var ss = e.source;
var s = ss.getSheetByName("Animals List");
var r = e.range;
var remoteSheet = ss.getSheetByName("List B");

  // to let you modify where the action and move columns are in the form responses sheet
var actionCol = 1;

  // 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;

var curNumberCell = s.getRange(rowIndex,2, 1, 1);
var curIDNumber =  curNumberCell.getValue();
var curDCell = s.getRange(rowIndex,4, 1, 1);
var curECell = s.getRange(rowIndex,5, 1, 1);

 // if our action/status col is changed to update or edit, do stuff
if (e.value == "update" || "edit" && colIndex == actionCol) {

 //THE FOLLOWING REPLACES THE FORMULAS OF THE ROW WITH THEIR GENERATED VALUES - UNCOMMENT TO USE:
 //var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
 //sourceRange.setValues(sourceRange.getValues());


///Retrieve data from remote tab/sheet that has matching cell value
var values = remoteSheet.getDataRange().getValues(); //IN REMOTE SHEET WE NEED TO SEARCH ENTIRE SHEET

for (var i = 0; i < values.length; i++) {
   if (values[i][0] == curIDNumber )
   {
   curDCell.setValue(values[i][1] );
   curECell.setValue(values[i][2] );
   }
   }    
} //end retrieve remote data
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.