0

I have been trying to find the right method to pre-fill the start date field in my Google Forms with the current date and time, but I failed to achieve this.

I think the createResponse(hour, minute) would be the best method, however I have absolutely no idea on where to include it in my script:

    function book1() {
var form = FormApp.getActiveForm();
var responses = form.getResponses();
var len = responses.length;
var last = len - 1;
var items = responses[last].getItemResponses();
////// var current= items[2].createResponse(10, 00);
var email = responses[last].getRespondentEmail();
var equipment = items[1].getResponse();
var datestart = items[2].getResponse();
var dateend = items[3].getResponse();
var cal = CalendarApp.getCalendarsByName(equipment)[0];
Logger.log('current '+current);
Logger.log(datestart);
Logger.log(dateend);
var start = new Date(datestart);
var end = new Date(dateend);
Logger.log('start '+start);
Logger.log('end '+end);

var allEvents = CalendarApp.getCalendarsByName(equipment)[0].getEvents(start, end);//Returns an array of events

if (allEvents.length < 1) {

var event = cal.createEvent(equipment, start, end)
.addGuest(email);

MailApp.sendEmail({
    to: email,
    subject: "Equipment " +equipment+ " booking confirmed",
    htmlBody: "Equipment " +equipment+ " available, please return it by " +dateend+ " and scan the QR code when returning it.",
  });

}

else {
  var blob = HtmlService.createHtmlOutputFromFile("calendariframe").getBlob();
  MailApp.sendEmail({
    to: email,
    subject: "Equipment  " +equipment+ " not available",
    htmlBody: blob.getDataAsString(),
  });
};
}

In my latest attempt, I encountered that error;TypeError: items[2].createResponse is not a function

Would you know how to make it work?

Knowing that pre-filling the date and time only simplifies the process of the form users, but that in some cases they will not use the current date and time as a starting time for booking.

Thanks a lot in advance!

  • Does this answer your question? [Is it possible to 'prefill' a google form using data from a google spreadsheet?](https://stackoverflow.com/questions/20108511/is-it-possible-to-prefill-a-google-form-using-data-from-a-google-spreadsheet) – Nikko J. Mar 22 '22 at 23:39
  • Sorry, I did not precise that I cannot use pre-filled URL method, as my links are set as QR codes (if the time changes, the URL changes hence the QR code changes too, which is not desired). – user10879705 Mar 23 '22 at 09:01

0 Answers0