简体   繁体   English

如何使用 javascript 更新 Google 日历中的事件?

[英]How to update an event in Google Calendar using javascript?

I need to update the name of some Google Calendar events, once I get them through JavaScript.一旦我通过 JavaScript 获取它们,我需要更新一些 Google 日历事件的名称。 I only have the sample code which Google shows in their Google Developer site, and I cannot find a Google Calendar API methods list to use them in JavaScript.我只有谷歌在他们的谷歌开发者网站上展示的示例代码,我找不到谷歌日历 API 方法列表来在 JavaScript 中使用它们。 I only see REST requests in the API Reference.我只在 API 参考中看到 REST 请求。 Sorry my english, thanks.对不起我的英语,谢谢。

You have to perform an HTTP (PUT) Request.您必须执行 HTTP (PUT) 请求。 This could maybe done be done with AJAX or jQuery.ajax():这可以通过 AJAX 或 jQuery.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
});

Remember to implement jQuery记得实现jQuery

I hope this works for you!我希望这对你有用!

Hi I know it's late but i face the same issue i found a solution so i thought sharing it will be good.嗨,我知道已经晚了,但我遇到了同样的问题,我找到了解决方案,所以我认为分享它会很好。 First i created the event object then pass the dynamic values to object keys首先我创建了事件对象,然后将动态值传递给对象键

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}
    ]
  }
};  

I have a google event id stored in database我有一个谷歌事件 ID 存储在数据库中

var google_event_id = '728326gghd';

Then i pass the id variable and event object to Google Calendar Api update method然后我将 id 变量和事件对象传递给 Google Calendar Api 更新方法

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

Then i execute the request然后我执行请求

 request.execute(function(event) {
                    alert('Event updated successfully' );
                });
        /**
         *  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 ? :)
                });
            }
        }

5+ years after the question was asked, here is how I succeeded:在提出这个问题 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);
}

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

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