0

I am trying to compare values from columns from 2 sheets but it doesn't work. I'm new to this, i think it's the if statement because i tryed logging any possible variable and everything works.

function combineemail() {

var  source = SpreadsheetApp.getActive();
var  source_sheet = source.getSheetByName("Export1");
var  source2_sheet = source.getSheetByName("Sheet1");
var  target_sheet = source.getSheetByName("Merge1");
var  numRows = source_sheet.getLastRow() - 1;
var  source_range = source_sheet.getRange(2, 5, numRows, 1).getValues();
var  numRows2 = source2_sheet.getLastRow() - 1;
var  source2_range = source2_sheet.getRange(2, 2, numRows2, 1).getValues();
var c = "Merge"

for (var i = 0; i < numRows; i++){
for (var j = 0; j < numRows2; j++){
    if (source_range[i] == source2_range[j]) {
      Logger.log(source2_range[j]);
    }; 
};
};

};

Rubén
  • 34,714
  • 9
  • 70
  • 166

1 Answers1

0

getValues() assign a multidimensional array (2D array) to source_range and source2_range . When you do source_range[i] == source2_range[j] your are comparing two arrays but on JavaScript this is complex.

If you are looking to compare cells values of two single column ranges then you should use

source_range[i][0] == source2_range[j][0]
  • The first index corresponds to rows
  • The second index corresponds to columns

Bear in mind that JavaScript indexes are 0 based while SpreadsheetApp row/column indexes are 1 based. Reference

Rubén
  • 34,714
  • 9
  • 70
  • 166