简体   繁体   English

Google Calendar API Javascript-添加事件格式

[英]Google Calendar API Javascript - Add event formatting

I'm brand new to Google API of any kind and relatively new to code in general. 我对任何种类的Google API都是陌生的,并且对代码而言相对较新。 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. 我正在尝试创建一个网站,使我可以从复选框列表中选择多天,并在这些天为我创建Google日历活动。

I'm using an ASP.net webpage for display and checkbox logic and sending the checkbox results to Javascript as JSON. 我将ASP.net网页用于显示和复选框逻辑,并将复选框结果作为JSON发送到Javascript。 Then handling the JSON and google calendar stuff in Javascript. 然后使用Javascript处理JSON和Google日历。

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. 只需复制一些示例中的样板代码,我就可以通过Google进行身份验证,并使用硬编码的事件详细信息将事件成功添加到我的日历中。 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" 这适用于“ 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". 当我尝试用创建的详细信息“ 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". 这在我只是将“ var request”“ resource”部分替换为“ 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. 为了进行调试,我一直在发出警报,并且t1警报结果看起来像是按照样板代码的格式正确格式化了数据,但是它没有用。 (Photo here->) alert(t1) result (照片在这里->) 警报(t1)结果

I tried the batch method discussed here but couldn't make that work either. 我尝试了此处讨论的批处理方法,但也无法使该方法起作用。 Google Calendar javascript api - Add multiple events Google Calendar JavaScript API-添加多个事件

Thanks for any help. 谢谢你的帮助。

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. 我遇到的主要问题是Google日历似乎不喜欢我使用的日期格式,其中dateTime字符串中包含时区偏移量。 By separating that out and inserting it separately as a time zone region instead, I had success. 通过将其分离出来,并将其作为时区区域分别插入,我获得了成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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