Trying to send email from google sheets with apps script, but blank cells give unwanted output

Issue

I’m trying to send some values in table to an email, the problem starts when it sends the date, I want it in this format "dd-mm-yy"

var fecha = new Date(registro.getRange("c16").getValue());
var fechaF = Utilities.formatDate(fecha, "GMT","DD-MM-YYYY");

The above code is used to transform the date, but when a cell that is a date is empty it sends me a date totally random.

It is table with multiple dates, and if I write the first it sends exactly the same, but if there is nothing it sends me a random date, and I want to send nothing, because having these dates in the email could be confusing.

What can I do to set the format, but also to not send anything if it is empty

enter image description here

Here is an example what is send to the email

Solution

Try this:

var fechaF = registro.getRange("c16").isBlank() ? "No Date" : Utilities.formatDate(new Date(registro.getRange("c16").getValue()), "GMT","DD-MM-YYYY");

Answered By – Cooper

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.