Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-forms

Google Apps Script randomly fails to calculate the age


I wrote a Google Apps Scripts script to generate a PDF from a bunch of data entered in to a Google Form. The form asks for the date of birth. I want the user to input their DOB and the PDF has a field for DOB and the current age. The script some times calculates the age properly. Some times it fails to calculate and outputs "NaN" as the age. Here is the code

I want the age to be in years

//this is a function to calculate the age. It will be called later
  function calculateAge(birthdate) {
    var today = new Date();
    var current_year = today.getFullYear();
    var birth = new Date(birthdate);
    var birth_year = birth.getFullYear();
    var age = current_year - birth_year;

    Logger.log("Current year is:" + current_year);
    Logger.log("Year of birth is: " + birth_year);
    
    return age;
  }

function autoFillGoogleFromForm(e){
 

  //e.values is an array of form values
  var timestamp = e.values[0];
  var sex = e.values[4];
  var dob = e.values[5];
  var date = e.values[9];
  

  //this function calculates the age in years by taking the dob as one argument
  var ageInYears = calculateAge(dob);

  // logs the dob and age for debugging
  Logger.log("Entered DOB is:" + dob);
  Logger.log("Calculated age is:" + ageInYears);

  //file is the template file. It is selected by get by ID
  var templateFile = DriveApp.getFileById("ID_of-file");

  //This is where the responces are saved as a doc
  var temlateResponceFolder = DriveApp.getFolderById("ID_of_folder");



  var body = doc.getBody();

  //replace the texts in the template
  body.replaceText("{{Date}}", date);
  body.replaceText("{{Sex}}", sex);
  body.replaceText("{{DOB}}", dob);
  body.replaceText("{{Age}}", ageInYears);


  //save and close the document
  doc.saveAndClose();

}





Some times i get the expected output as age. Some times the age is printed as "NaN" in the pdf. I have tried logging the variables in question. Here is the log

Case 1
    Info    Current year is:2023
    Info    Year of birth is: NaN
    Info    Entered DOB is:27/09/1953
    Info    Calculated age is:NaN

Case 2
    Info    Current year is:2023
    Info    Year of birth is: 2002
    Info    Entered DOB is:03/01/2002
    Info    Calculated age is:21

Case 3
    Info    Current year is:2023
    Info    Year of birth is: NaN
    Info    Entered DOB is:30/10/1977
    Info    Calculated age is:NaN

Solution

  • Failing to compute Age:

    I understand that you are having a NaN value on some of your test cases. For starters I would like to refer to this article of what are accepted Date Formats. Your Formats falls outside accepted Date formats in Javascript does shows "Invalid Date" therefore cannot be translated to a new Date. For clarification that is the reason why 03/01 still works and 27/09 are not as the first one can still be read as Mar 01 YYYY.

    Options:

    First Option: You can change how your users were supposed to put in their DOB.

    Second Option: After getting the data, on your calculateAge function. Use split function to put it into chunks and use "/" as your delimiter. Then you can create a new value for your New Date using their Index.

    Note:If you have any more questions please feel free to comment on this answer community will be happy to put you in the right direction.

    References:

    Date Formats