Home > Back-end >  Same variable with unexpected multiple values
Same variable with unexpected multiple values

Time:11-11

I have no idea why this is happening, but it looks like a variable has two values, one correct and one unexpected value. When I print a variable in the console log, it shows at first the correct variable and right after it prints again another unexpected value. Is it a bug in AppScript, or something off that I cannot see?

Some extract of the code:

function funx(){
    var ss = SpreadsheetApp.openById('xxx');
    var sheet = ss.getSheetByName('Data');
    var list = sheet.getRange(3,5,sheet.getLastRow(),1).getRichTextValues();
    var additionalList = sheet.getRange(3,3,sheet.getLastRow(),2).getValues(); //need another list  because rich text value does not get numbers

for (var i=0;i<list.length;i  ){
  let datex = new Date(additionalList[i][0]);
Logger.log(datex); // this prints only one value and this is the correct value
  let date = getFDate(datex)}

}

function getFDate(rowDate){

  Logger.log(rowDate); // this prints two values. The first one is same as datex. The second one looks like a previous date which run when I called this function in the previous loop iteration.
  var year = rowDate.getFullYear();
  var month = (1   rowDate.getMonth()).toString();
  month = month.length >1 ? month : '0'   month;
  var day = rowDate.getDate().toString();
  day = day.length > 1 ? day : '0'   day;

  var dateReturn = month   '/'   day   '/'   year;
  return dateReturn;

}

The Logger.log prints two values!

5:58:48 PM Info Wed Nov 30 00:00:00 GMT-08:00 2022 -> result of Logger.log(datex). This is the expected value

5:58:48 PM Info Wed Nov 30 00:00:00 GMT-08:00 2022 -> result of Logger.log(rowDate). This is the expected value

5:58:48 PM Info 2022-11-08T22:14:48.343 0000 -> result of Logger.log(rowDate). This is unexpected.

I purposely changed the name of the variable "datex" to make sure it is nowhere else in the code.

_____ REASON OF ISSUE:

In another function as part of an IF statement that was never met, there was a Logger.log(getFDate(...)). I thought it was completely unrelated. But removing that fixed the issue. Looks like calling that function in a Logger.log caused its malfunction.

CodePudding user response:

_____ REASON OF ISSUE:

In another function as part of an IF statement that was never met, there was a Logger.log(getFDate(...)). I thought it was completely unrelated. But removing that fixed the issue. Looks like calling that function in a Logger.log caused its malfunction.

Extraction of that logger.log (it is part of a function which call Jira API and do something if some conditions are met)

Logger.log("DETECTED A CHANGE. Due Date changed by someone else: " data["issues"][0].changelog["histories"][id].displayName " on date: " getFDate(data["issues"][0].changelog["histories"][id].created));

CodePudding user response:

Try this:

function funx() {
  var ss = SpreadsheetApp.openById('xxx');
  var sheet = ss.getSheetByName('Data');
  var list = sheet.getRange(3, 5, sheet.getLastRow() - 2).getValues();// third parameter is the number of rows in the range not the last row in the range
  var additionalList = sheet.getRange(3, 3, sheet.getLastRow() - 2, 2).getValues();
  for (var i = 0; i < list.length; i  ) {
    let datex = new Date(additionalList[i][0]);
    let date = getFDate(datex)
  }
}

I cannot reproduce your results because I do not have your data but several of your ranges were incorrect and getRichTextValues only return richTextValues and my guess is that you did not have any. Please provide a data table if you wish more assistance.

creating markdown tables

  • Related