0

I've been trying to set the values in a certain column and for some reason that i cant figure out Its says that my array isn't defined.

function myFunction() {
  var Date1 = new Date(); // Today's date
  var Sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var arr = Sheet.getRange("AB2:AB6").getValues(); // Recruitment Dates
  var Rowlimit = arr.length;
  var DaysArr = [new Array()];
  for(i = 0 ; i< Rowlimit; i++)
  {
    var TodayTime = Date1.getTime();
    var RecDayTime =  (new Date(arr[i][0])).getTime(); //converting from string to Date Type, and converting to the miliseconds number.
    var DaysBetween = Math.abs(Math.floor((TodayTime-RecDayTime)/(24*3600*1000))); // Getting the difference in miliseconds and converting the miliseconds to days
    Logger.log(DaysBetween);
    DaysArr[0].push(DaysBetween);
  }
  Logger.log(DaysArr);
  
Logger.log(DaysArr[0]);
Sheet.getRange("AA2:AA6").setValues(Daysarr);
}

below is the execution log, sorry for the wierd language, its just that the drive is set on hebrew

11:03:34    הודעה   ההפעלה התחילה
11:03:33    מידע    95.0
11:03:33    מידע    529.0
11:03:33    מידע    382.0
11:03:33    מידע    200.0
11:03:33    מידע    180.0
11:03:33    מידע    [[95.0, 529.0, 382.0, 200.0, 180.0]]
11:03:33    מידע    [95.0, 529.0, 382.0, 200.0, 180.0]
11:05:25    שגיאה   ReferenceError: Daysarr is not defined
0

1 Answer 1

1

Modification points:

  • In your script, DaysArr is declared. But at Sheet.getRange("AA2:AA6").setValues(Daysarr);, Daysarr is used. I thought that the reason of your error message is due to this.
  • As another modification point, from your script, Logger.log(DaysArr[0]); is [95.0, 529.0, 382.0, 200.0, 180.0]. But in your script, Sheet.getRange("AA2:AA6").setValues(Daysarr);. In this case, I think that it is required to transpose the value.

When above points are reflected to your script, it becomes as follows.

From:

Sheet.getRange("AA2:AA6").setValues(Daysarr);

To:

Sheet.getRange("AA2:AA6").setValues(DaysArr[0].map(e => [e]));

Reference:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.