Re: [Apps-Script] Importing Google Form to Calendar not Working

242 views
Skip to first unread message

CBMServices Web

unread,
Aug 16, 2022, 5:44:41 PM8/16/22
to google-apps-sc...@googlegroups.com
Hi Randle,

The problems you are experiencing are because of the way you are handling the date object.

Google forms records the date in the correct format, so you can take it and manipulate it directly as a date object. The time though is also a date object so you can't just take the time input and use it, you will need to merge the time with the date to get a correct start date/time for your calendar event creation.

I am assuming here that you did configure the Google form to take a date and time as inputs. If you did not do any input validation, my input above is not accurate.

On Tue., Aug. 16, 2022, 11:41 a.m. Randee Frankel, <class...@gmail.com> wrote:
I am new to coding and feel like I am so close to getting this. I have created a form and the script should take the form data, create a calendar event, and send an email. Every value returns correctly through the logger. 

However the debugger has the following values undefined: appEnd, begin, arr1, start, timezone, end, end2, arr2, event, and endDate. Everything else display the correct information.

The script runs and never ends. No event is created and no email is sent. I would appreciate any advice you have. I have spent three days on this already.

 function createEvent() {
//connects calendar and form
var form = FormApp.openById('1Y2FnxrX_Z40qkva57Bt_NjYCfhZWciig-wWxbgmGwOM');
var cal = CalendarApp.getCalendarsByName('Paws Appointment Sample');
//gets responses from form
var responses = form.getResponses();
var len = responses.length;
var last = len -1 ;
var items = responses[last].getItemResponses();
//var email = responses[last].getRespondentEmail(); this did not return email
var email = items[0].getResponse();
var type = items[1].getResponse();
var date = items[2].getResponse();
var startTime = items[3].getResponse();
var animalName = items[4].getResponse();
var animalNumber = items[5].getResponse();
var fosterName = items[7].getResponse();

//code for body of email to be sent
var subject = "Foster Walk-In Visit: "+animalName+ '-'+type;
var body = "You can view your scheduled visit here "+url;
//combines the date and start time together and converts to date format
var appt = date +' '+ startTime;
const startDate = new Date (appt);
//creates an end date that is 30 minute after the start date
const endHours = new Date (appt);
var apptEnd = new Date(startDate.getTime() + (30 * 60 * 1000));
/*converts the start date to a string, splits it and puts in the required format YYYY-MM-DDTHH:MM:SS+00:00*/
var begin = startDate.toISOString();
var arr1 = begin.split('.');
var timezone = '-04:00';
var start = arr1[0]+timezone;

/*converts the end date to a string, splits it and puts in the required format YYYY-MM-DDTHH:MM:SS+00:00*/
var end = apptEnd.toISOString();
var end2 = apptEnd.toISOString();
var arr2 = end2.split('.');
var timezone = '-04:00';
var endDate = arr2[0]+timezone;

//tests and format is correct
Logger.log('start date '+start);
Logger.log('end date '+endDate);

//creates the calendar event
var event = createEvent(animalName+' '+animalNumber+' Foster Walk-In Visit',start,endDate);
addGuest(email);
setDescription(fosterName);

//sends the email
GmailApp.sendEmail(email, subject, body);
}

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/34ef8177-0100-4aac-ac24-c6dc954c2ba7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages