2

I'm brand new to Google API of any kind and relatively new to code in general. I'm trying to make a website that will allow me to select multiple days from a list of checkboxes and create Google calendar events on those days for me.

I'm using an ASP.net webpage for display and checkbox logic and sending the checkbox results to Javascript as JSON. Then handling the JSON and google calendar stuff in Javascript.

Just by copying the boilerplate code from some examples, I'm able to get authenticated through Google and successfully add an event to my calendar with hardcoded event details. The problem begins when I try to populate the event details with variables rather than hardcoded details.

I believe I'm real close on getting this working and there's just some small formatting issue keeping it from working.

This works with the hardcoded details in "var resource"

function makeApiCall3() {
            for (var i = 0; i < arrayOfEvents.length; i++)
            {

                var t1 = "{\"summary\":\"" + arrayOfEvents[i].summary + "\", \"start\":" + JSON.stringify(arrayOfEvents[i].start) + ", \"end\":" + JSON.stringify(arrayOfEvents[i].end) + "}"; 
                gapi.client.load('calendar', 'v3', function () {
                    var resource = {
                        "summary": "Appointment",
                        "location": "Somewhere",
                        "start": {
                            "dateTime": "2017-05-29T10:00:00.000-07:00"
                        },
                        "end": {
                            "dateTime": "2017-05-29T10:00:00.000-07:00"
                        }

                    };
                    var request = gapi.client.calendar.events.insert({
                        'calendarId': 'primary',
                        'resource': resource
                    });
                    request.execute(function (resp) {
                        console.log(resp);
                    });
                    alert(t1);
                });

            }

            alert("End of Calendar Call");
        }

This does not work when I try to replace the content of "resource" with my created details "t1".

function makeApiCall3() {
            for (var i = 0; i < arrayOfEvents.length; i++)
            {

                var t1 = "\"summary\":\"" + arrayOfEvents[i].summary + "\", \"start\":" + JSON.stringify(arrayOfEvents[i].start) + ", \"end\":" + JSON.stringify(arrayOfEvents[i].end); 
                gapi.client.load('calendar', 'v3', function () {
                    var resource = { t1
                 //       "summary": "Appointment",
                 //       "location": "Somewhere",
                 //       "start": {
                 //           "dateTime": "2017-05-29T10:00:00.000-07:00"
                  //      },
                  //      "end": {
                  //          "dateTime": "2017-05-29T10:00:00.000-07:00"
                  //      }

                    };
                    var request = gapi.client.calendar.events.insert({
                        'calendarId': 'primary',
                        'resource': resource
                    });
                    request.execute(function (resp) {
                        console.log(resp);
                    });
                    alert(t1);
                });

            }

            alert("End of Calendar Call");
        }

This does not work either where I just replace the "var request" "resource" portion to "t1".

function makeApiCall3() {
            for (var i = 0; i < arrayOfEvents.length; i++)
            {

                var t1 = "{\"summary\":\"" + arrayOfEvents[i].summary + "\", \"start\":" + JSON.stringify(arrayOfEvents[i].start) + ", \"end\":" + JSON.stringify(arrayOfEvents[i].end) + "}"; 
                gapi.client.load('calendar', 'v3', function () {
                    var resource = {
                        "summary": "Appointment",
                        "location": "Somewhere",
                        "start": {
                            "dateTime": "2017-05-29T10:00:00.000-07:00"
                        },
                        "end": {
                            "dateTime": "2017-05-29T10:00:00.000-07:00"
                        }

                    };
                    var request = gapi.client.calendar.events.insert({
                        'calendarId': 'primary',
                        'resource': t1
                    });
                    request.execute(function (resp) {
                        console.log(resp);
                    });
                    alert(t1);
                });

            }

            alert("End of Calendar Call");
        }

For debugging I've been making alerts and the t1 alert result looks as if I've formatted the data exactly as the boilerplate code was, yet it isn't working. (Photo here->) alert(t1) result

I tried the batch method discussed here but couldn't make that work either. Google Calendar javascript api - Add multiple events

Thanks for any help.

Andy Adams
  • 31
  • 4
  • What errors are you getting? Haven't tried your usecase yet but did you try using [JSON.stringify()](https://www.w3schools.com/js/js_json_stringify.asp)? – ReyAnthonyRenacia May 24 '17 at 16:36
  • I'm not seeing any error, just nothing gets added to the calendar. I do have JSON.stringify() in the t1 variable. Where else are you suggesting? – Andy Adams May 24 '17 at 21:24

1 Answers1

0

I have resolved the issue. Prior to this issue, I was unaware that I could "debug" while within the browser by using the developer console. After discovering the developer console, I was able to step through several small issues I had and get it resolved.

The main issue I had was Google Calendar didn't seem to like the date format I had used where the time zone offset is included in the dateTime string. By separating that out and inserting it separately as a time zone region instead, I had success.

Andy Adams
  • 31
  • 4