I have unique ID ('ID1') in column A in sheet1 and I need to compare it with column B in sheet2 where are ID ('ID2') to.
So I need to compare those of ID and check if some of them match, if they are I want to change value of cell in the same row as 'ID1' in sheet1 but in another column, let say C.
function IDValidation() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet1');
var sheet2 = ss.getSheetByName("Sheet2");
var Sheet1range = sheet1.getRange(2,1,sheet1.getLastRow()); //header is row 1
var Sheet2range = sheet2.getRange(2, 2); //header is row 1
var Sheet1cellValue = Sheet1range.getValues();
var Sheet2cellValue = Sheet2range.getValues();//Look here
for(i = 0; i<Sheet1cellValue .length-1; i++){
if(Sheet1cellValue [i][0] == Sheet2cellValue)
{
ss.getSheetByName("Sheet1").getRange(i+2,3).setValue("Close");
}
}
}
var Sheet2range = sheet2.getRange(2, 2);
is only getting one cell. Changing it tovar Sheet2range = sheet1.getRange(2,2,sheet2.getLastRow());
will get all the rows starting at 2,2 as an array so you would have to search i the array. The simple method would be a second for loop inside the first, looping on each item in Sheet2cellValue and comparing it to Sheet1cellValue[i][0]. NOTE that using .getLastRow() will get all the empty rows as well, so any blank rows will match. – Karl_S