1

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: enter image description here

Could anyone point me to the right direction on getting this solved?

Thank you! Antonio

onit
  • 2,275
  • 11
  • 25

1 Answers1

1

You were comparing entryValues vs dbDataPushed but is logging entryDataPushed and dbDataPushed. Is this expected?

Anyways, since you are logging the variables, I assume the variables logged are the ones you need to compare.

Convert them into JSON string by wrapping the variables with JSON.stringify(). I use this all the time when I encounter an issue similar to yours right now.

var duplicate = false;
if (JSON.stringify(entryDataPushed) == JSON.stringify(dbDataPushed)) {
  duplicate = true;
}

Output: output

See resource below for other ways to properly compare arrays if the above code doesn't work.

Resources:

NightEye
  • 10,634
  • 2
  • 5
  • 24
  • I've tried it, but it shows the same result = duplicate, even though one value is different. Thanks, anyway! – onit Jan 21 '21 at 16:18
  • Hi @AntonioSantos, Sorry that I forgot that you actually traverse per element of both arrays. I have updated the answer and try that instead. – NightEye Jan 21 '21 at 16:19
  • 1
    Hi, @NaziA! Still the same. I'll share the rest of the code to see if there's any stealth flaw I may have missed. – onit Jan 21 '21 at 16:26
  • If that won't be a hassle, that would be better. Thanks @AntonioSantos. If you can, kindly provide the array values to so that I can test it on my side, it would be much better. – NightEye Jan 21 '21 at 16:28
  • 1
    Here's a copy of the spreadsheet with the dummy data: https://docs.google.com/spreadsheets/d/1S0KOStgG8kM8py_WaudAdcXnSFpertaWwfej8P-lfC4/edit?usp=sharing Appreciate your time! – onit Jan 21 '21 at 16:32
  • 1
    It is working on my side @AntonioSantos when using the updated answer. I'll add the proof to the answer where it shows that a change has been made – NightEye Jan 21 '21 at 16:42
  • 1
    There may be an error with the loops on my side. Thanks a lot! – onit Jan 21 '21 at 16:46
  • Hi @AntonioSantos, You were comparing `entryValues` vs `dbDataPushed` but is logging `entryDataPushed` and `dbDataPushed`. Anyways, since you are logging `entryDataPushed` and `dbDataPushed`, I assume the variables logged are the ones you need to compare. It might have been the reason but didn't check since the condition is already working. Glad to be of help. Good luck – NightEye Jan 21 '21 at 16:48
  • 1
    Your attention and will to help are incredible. Thanks a million! Problem solved! – onit Jan 21 '21 at 17:09