Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

google apps script - Why array comparison isn't working? (GAS)

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

question from:https://stackoverflow.com/questions/65831309/why-array-comparison-isnt-working-gas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...