Essentially, the user will input data for a new item on the NovoItem sheet. Upon pressing the Save Button (yet to be added), the code should check the ArquivoItens sheet to see if the item already exists. If it does, the code should halt its operation. However, currently, the code is not halting when a match is found on the ArquivoItens sheet:
function copyrange() {
var sourceSheet = 'Novo Item';
var destinationSheet = 'ArquivoItens';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var ActiveUser = Session.getActiveUser();
//This part adds the current user and a timestamp to the last two columns of sheet1.
var val = sheet.getRange("Q12:Q")
.getValues();
for (var i = 0; i < val.length; i++) {
if (val[i] > 0) {
sheet.getRange(12, 34, i + 1, 1)
.setValue(new Date());
sheet.getRange(12, 35, i + 1, 1)
.setValue(ActiveUser)
}
}
var LastRowSource = sheet.getLastRow();
var LastColumnSource = sheet.getLastColumn();
var values = sheet.getRange(11,1,LastRowSource,LastColumnSource).getValues();
var csh = ss.getSheetByName(destinationSheet);
var data = [];
for (var i = 1; i < values.length; i++) {
if ( values[i][0] != '') {
data.push(values[i]);
//sheet.deleteRow(i+1)
}
}
var dataNovoItem = sheet.getRange("B12:B").getValues();
var dataArquivoItens = csh.getRange("B2:B").getValues();
for(var n=1; n < dataNovoItem.length ; n++){
for(var j=1; j< dataArquivoItens.length ; j++){
if (dataNovoItem[n] != 0 && dataArquivoItens[j] != 0) {
if(dataNovoItem[n] == dataArquivoItens[j]) {
break;
}
}
}
}
Logger.log("Novo Item" + dataNovoItem);
Logger.log("ArquivoItens" + dataArquivoItens);
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Here's the log output I'm seeing: https://i.sstatic.net/Zdp8T.jpg
Any assistance would be greatly appreciated.