简体   繁体   中英

Google Scripts Timestamp - Format Output on Email

I'm not sure what I'm doing wrong here, and this is probably really simple... After scouring the net and trying hundreds of different examples found, I've come up empty handed on getting the format needed. My script works, sends the email, all values are there, but the date formats are not what we are after. So, here is what's up:

I've got a basic script that sends an HTML email from a template when a respondent submits a form. The timestamp, start (date and time), end (time only) value is needed, and is printed in the HTML email, but it's showing a full-blown timestamp output such as: "Tue Nov 03 2020 11:39:28 GMT-0700 (Mountain Standard Time)"

What I am trying to do is format the timestamp value shown in the email to this: "Tue Nov 03 2020 HH:mm"

Here is the script I am using:

function onFormSubmit(e) {
  var htmlBody = HtmlService.createTemplateFromFile('email');  
  var rng = SpreadsheetApp.getActiveSheet().getActiveRange();
  var timestamp = Utilities.formatDate(new Date(), "MST" , "MM-dd-yyyy | HH:mm:ss");
  var email = rng.getValues()[0];
  var body = HtmlService.createTemplateFromFile("email");
  var to = 'foo@bar';
  var subject = 'Activity Report ' + email[0] + '';
  
  htmlBody.timestamp = email[0];
  htmlBody.start = email[1];
  htmlBody.end = email[2];
  htmlBody.activityobserved = email[3];
  htmlBody.summary = email[4];
  htmlBody.actiontaken = email[5];
  htmlBody.attachments = email[6];
  
  var email_html = htmlBody.evaluate().getContent();
  
  MailApp.sendEmail({
    to: to,
    subject: subject,
    htmlBody: email_html,
    replyTo:'bar@foo',
  });
}

Solution:

You don't include variable timestamp in the htmlBody object. Instead you are using the original source value of it.

Replace :

htmlBody.timestamp = email[0];

with :

htmlBody.timestamp = timestamp;

Update based on your comment:

Im a little confused on how to format the start and end times though. They are still displaying the full output.

Assuming that you have date objects in your sheet,

Replace :

 htmlBody.start = email[1];
 htmlBody.end = email[2];

with

htmlBody.start = Utilities.formatDate(new Date(email[1]), "MST" , "EEE MMM dd yyyy HH:mm");
htmlBody.end = Utilities.formatDate(new Date(email[2]), "MST" , "EEE MMM dd yyyy HH:mm");

Don't forget to actually use timestamp . To get the "Tue" part in your date, you can use "EEE". I set the date formatting below as you specified in your question.

function onFormSubmit(e) {
  // ...
  var timestamp = Utilities.formatDate(new Date(), "MST" , "EEE MMM dd yyyy HH:mm");
  // ...
  htmlBody.timestamp = timestamp;
  htmlBody.start = Utilities.formatDate(email[1], "MST" , "EEE MMM dd yyyy HH:mm");
  htmlBody.end = Utilities.formatDate(email[2], "MST" , "EEE MMM dd yyyy HH:mm");
  // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM