I have an Array of data in Google Script(around 10 items in the array). How can I search for those array items in column A? (for example: search for items starting from cell a12 till end) and if array values not found in column A then add them to end of column A?
I tried to use
data[n][0].toString().match(values_array[i])==values_array[i]
and the second next time i run the function i see repeated data get entered!
For example the data from A1 to A10 are as follows:(1,2,3,4,5,6,7,8,9,10) and first time i run the function it adds all data to column A(starting a12)
And the second time i run the function with same data from A1 till A10 ,it should not enter anything but it adds 1,2,3,4,5,6,7,8,9,10 again!I don't know why this code is acting like this!
I would appreciate if an expert tell me how to fix this code.Thanks in advance.
function find2(){
var sheet = SpreadsheetApp.getActiveSheet(),
range,
values_array;
range = sheet.getRange('a1:a10');
values_array = range.getValues();
var sh = SpreadsheetApp.getActiveSheet();
var range2 = sh.getRange('a12:a')
data = range2.getValues(); // read all data in the sheet
var lastRowIndex=getLastColumnRow(0)
for (var i = 0; i < values_array.length; i++) {
for(n=0;n<data.length;++n){ // iterate row by row and examine data in column A
if(data[n][0].toString().match(values_array[i])==values_array[i]){
} else {
var lastEmptyRowIndex=1+lastRowIndex+i
SpreadsheetApp.getActiveSheet().getRange('a'+lastEmptyRowIndex).setValue(values_array[i]);
};
}//end of innter for loop
}// end of outer for loop
}
function setUp() {
ScriptProperties.setProperty('active', SpreadsheetApp.getActiveSpreadsheet().getId());
}
function getLastColumnRow(RowNumber) {
var ss = SpreadsheetApp.openById(ScriptProperties.getProperty('active'));
var sheet = ss.getSheetByName("Sheet1");
var data = sheet.getDataRange().getValues();
var numRows = data.length;
// loop from bottom to top for last row in given row "RowNumber"
while( data[numRows - 1][RowNumber] == "" && numRows > 0 ) {
numRows--;
}
return numRows
}