如何使用javascript更新Google日历中的活动?

时间:2015-10-24 10:10:09

标签: javascript google-calendar-api

我需要更新一些Google日历活动的名称,一旦我通过JavaScript获取它们。我只有Google在其Google Developer网站上显示的示例代码,而我找不到Google Calendar API方法列表以在JavaScript中使用它们。我只在API Reference中看到REST请求。对不起我的英文,谢谢。

4 个答案:

答案 0 :(得分:2)

您必须执行HTTP(PUT)请求。这可以通过AJAX或jQuery.ajax()来完成:

jQuery.ajax

//This is the event data you got, with the changed values, you want to be changed in Calendar API
var changedEventData;

//changing the data of an calendar event
$.ajax({
  url: "https://www.googleapis.com/calendar/v3/calendars/" + calendarId + "/events/" + eventId,
  method: "PUT",
  data: changedEventData
});

请务必实施jQuery

我希望这适合你!

答案 1 :(得分:0)

嗨,我知道它已经很晚了,但我遇到了同样的问题,我找到了一个解决方案,所以我认为分享它会很好。 首先,我创建了事件对象,然后将动态值传递给对象键

var event = {
  'summary': summaryTxt,
  'location': locationTxt,
  'description': ' ',
  'start': {
    'dateTime': datetime,
  },
  'end': {
    'dateTime': datimeEnd,
  },


  'reminders': {
    'useDefault': false,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10}
    ]
  }
};  

我有一个存储在数据库中的Google事件ID

var google_event_id = '728326gghd';

然后我将id变量和事件对象传递给Google Calendar Api更新方法

  var request = gapi.client.calendar.events.update({
                  'calendarId': 'primary',
                  'eventId':google_event_id,
                  'resource': event
                });

然后我执行请求

 request.execute(function(event) {
                    alert('Event updated successfully' );
                });

答案 2 :(得分:0)

        /**
         *  Update an event
         */
        function updateEvent(eventId) {
            if (eventId) {  
                var eventToUpdate = gapi.client.calendar.events.get({
                    "calendarId": 'primary', 
                    "eventId": eventId
                });

                eventToUpdate.summary = $("#update-name").val(); //Replace with your values of course :)
                eventToUpdate.location = $("#update-location").val();
                eventToUpdate.description = $("#update-description").val();
                eventToUpdate.start = {
                    'dateTime': (new Date(2017, 04, 22, 8, 00, 00)).toISOString(), //2017-04-22 08h00m00s
                    'timeZone': 'Europe/Paris'
                };
                eventToUpdate.end = {
                    'dateTime': (new Date(2017, 04, 22, 9, 00, 00)).toISOString(), //2017-04-22 09h00m00s
                    'timeZone': 'Europe/Paris'
                };

                var request = gapi.client.calendar.events.patch({
                    'calendarId': 'primary',
                    'eventId':eventId,
                    'resource': eventToUpdate
                });

                request.execute(function(event) {
                    console.log('Event updated: ' + event.htmlLink);

                    //Action. Maybe refresh your events list ? :)
                });
            }
        }

答案 3 :(得分:0)

在提出这个问题 5 年后,我是这样成功的:

/**
 * changes transparency (i.e. Busy or Available) of event passed
 * 
 * @author dugBarnz
 * @version 1.0
 * @since 2021/01/11
 * @param {string} event id
 */
function zzChangeTransparency_(_calendarId, _eventId)
{
  var event = Calendar.Events.get(_calendarId, _eventId);
  if (event && event.transparency)
    event.transparency = "opaque";
  else
    event.transparency = "transparent";

  Calendar.Events.update(event, _calendarId, _eventId);
}