The code below used to work. Now, when comparing two arrays, even though there is a difference, as highlighted below, the code says that there is a duplicate.
I've tried it using .join() as well, but I keep getting the same result.
Script:
function SaveEditedEntry() {
const lock = LockService.getScriptLock();
lock.tryLock(3000);
if (lock.hasLock()) {
var sourceSheet = 'Entries';
var destinationSheet = 'Database';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var LastRowSource = sheet.getLastRow();
//var LastColumnSource = sheet.getLastColumn();
var entryValues = sheet.getRange(7, 1, LastRowSource, 16).getValues();
var dbSheet = ss.getSheetByName(destinationSheet);
var entryDataPushed = [];
var dbData = dbSheet.getRange(2, 1, dbSheet.getLastRow(), 16).getValues();
var dbDataPushed = [];
//var dbDataPushed = new Array();
var company = sheet.getRange("B3").getValue();
var period = sheet.getRange("B4").getValue();
var entryID = sheet.getRange("J5").getValue();
var timeStamp = new Date();
var user = Session.getActiveUser().getEmail();
if (company == '') {
Browser.msgBox('Make sure that you have chosen a valid company.');
return;
}
if (period == '') {
Browser.msgBox('Please, make sure that you have chosen a valid period.');
return;
}
var emptyRow = 0;
for (var i = 0; i < entryValues.length; i++) {
if (entryValues[i][0] != '') {
emptyRow = i + 1;
}
if (emptyRow < 1) {
Browser.msgBox('Please, make sure that you have entered a transaction.');
return;
}
if (entryValues[i][0] != '') {
entryDataPushed.push(entryValues[i]);
}
}
for (var n = 0; n < dbData.length; n++) {
if (dbData[n][1] != '' && dbData[n][0] == company && dbData[n][14] == period) {
dbDataPushed.push(dbData[n]);
}
}
var duplicate = false;
loop1:
for (var x = 0; x < entryValues.length; x++) {
loop2:
for (var j = 0; j < dbDataPushed.length; j++) {
if (JSON.stringify(entryValues[x]) == JSON.stringify(dbDataPushed[j])) {
duplicate = true;
break loop1;
}
}
}
Logger.log("EntryDataPushed: " + entryDataPushed);
Logger.log("dbDataPushed: " + dbDataPushed);
if (duplicate == true) {
Browser.msgBox('No change has been made.');
return;
This is the logs print:
Could anyone point me to the right direction on getting this solved?
Thank you!
Antonio
question from:
https://stackoverflow.com/questions/65831309/why-array-comparison-isnt-working-gas 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…