Luke
Luke

Reputation: 193

Getting a date object to show as plain text

I currently have working code that takes today's date and pastes it in a cell if that student is absent. The code then appends another date in the same cell if that student is absent another day. All works well for the first date. It displays as:

09/21

But when I run it again the next day it shows:

Mon Sep 21 2020 01:00:00 GMT-0400 (Eastern Daylight Time); 09/22

It should say:

09/21; 09/22

Here is the relevant section of code:

var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var emailCol = getColNum("Student Email")
var absentCol = getColNum("Absences");
var tardyCol = getColNum("Tardies");
var timezone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var date = Utilities.formatDate(new Date(), timezone, "MM/dd")

if (saveAb == "Yes") {

  for (i = 0; i < 40; i++) {

    var stuEmail = tAtt.getRange(2 + i, 30).getValue();
    var stuRow = getRowNum(stuEmail, emailCol);
    var stuAbData = sheet.getRange(stuRow, absentCol).getValue();
    Logger.log(stuAbData);
    var abToDisplay = date



    if (stuAbData == "") {
      var stringToDisplay = abToDisplay
    } else {
      var stringToDisplay = stuAbData + "; " + abToDisplay;
    }

    sheet.getRange(stuRow, absentCol).setValue(stringToDisplay);

  }
}

if (saveAb == "Yes" || saveTa == "Yes") {
  ss.toast("Attendance Saved");
}


}

Any way I can get it to spit out the stuAbData variable as plain text or prevent if from reformatting? I've tried several things such as using this variable instead in the .setValue field. Doesn't work.

var newTest = Utilities.formatDate(stuAbData, timezone, "MM/dd");

Upvotes: 0

Views: 184

Answers (1)

Diego
Diego

Reputation: 9571

Use getDisplayValue() instead.

var stuAbData = sheet.getRange(stuRow, absentCol).getDisplayValue();

The issue with getValue() is that Google will interpret it and return it to you as a Date object. This should only be a problem on the second run when you have the value "09/21", as that would be interpreted as a date.

Upvotes: 2

Related Questions