Google apps date creation from user input

2019-09-06 12:26发布

问题:

function onFormSubmit(e) {

  var d = e.values[1] + " 10:00:00 UTC";
  var start = new Date(d);

  // oncatenating the string as above fails?   

// ... form then sends an email and adds date to a Google Calendar

I'm using a Google spreadsheet to store start, end, title value from a Google Form generated from that spreadsheet. I now want to add those values to a Google Calendar.

Problem is that if I use concatenation to construct the string (so input can accept 4/4/2013) the date fails, "Invalid Date" when I email myself the values.

Maybe this is weird, but the email with the debug also carries a preceding ' as if it is escaping the value or something ...

var emailBody = "Booking enquiry was made on " + timestamp +

  "\n\nThe details they entered were as follows: " +

  "\nFrom: " + start +  " vs original date input : " +  e.values[1] + 

   // etc

Gives: Booking enquiry was made on 11/04/2013 17:53:06

The details they entered were as follows: From: Invalid Date vs original date input : '04/03/2013

See? '04/03/2013

UPDATE

The reply of Serge insas worked, but subsequently I discovered the ' appears only when the Google Form is hosted on my localhost dev server. When the page is put online (containing the iFrame) then the substring removal trick is not needed as no ' is prepended.

回答1:

The ' is used in spreadsheet cells to keep values as strings. If it is removed the the spreadsheet would convert it to a date object.

You can easily remove that ' with a string operator like substring()

var withoutQuote = e.values[1].substring(1);