Can someone help please as I am new to Google Scripts.

I have this simple code that copies values from one sheet to another

function copyrevision() {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var destSheet = ss.getSheetByName("Fallas Tecnicas");

  var source = ss.getRange ("D4");
  var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
  source.copyTo (destRange, {contentsOnly: true});

  var source2 = ss.getRange ("C4");
  var destRange = destSheet.getRange(destSheet.getLastRow(),2);
  source2.copyTo (destRange, {contentsOnly: true});

  var source6 = ss.getRange ("E49");
  var destRange = destSheet.getRange(destSheet.getLastRow(),6);
  source6.copyTo (destRange, {contentsOnly: true});

I need this operation to be done only if the report number value does not exist in the column 2 of destination sheet.

How can it be done?

  • In what row is the report number found in column 2? – Joel Reid May 21 '19 at 13:18

First your script should read the values of Column 2

Let say that you use destSheet.getRange(2,2,sheet.getLastRow(),1).getValues() then you could use a JavaScript loop to iterate over all the values or use a Array method like Array.some(callback) that returns true if any of the values meet the callback test.

As getValues() return a 2D array, the statement could look as following

var valueToLookForFound = values.some(function(row){
  return row[0] === valueToLookfor;

valueToLookForFound will be true if the valueToLookFor was found.


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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