简体   繁体   English

Google Calendar JavaScript API-添加多个事件

[英]Google Calendar javascript api - Add multiple events

I've been facing a problem when I'm trying to add multiple events to a Google Calendar via javascript v3 api. 当我尝试通过javascript v3 api将多个事件添加到Google日历时遇到了一个问题。

I have an array which entries are events like these: 我有一个数组,条目是这样的事件:

 newEvent = { "summary": response[i].name+" BDay!!", "start": { "dateTime": date }, "end": { "dateTime": date } }; events[i]=newEvent; 

After, I make a call to Google Calendar api in order to add the events: 之后,我打电话给Google Calendar API以添加事件:

var request;
for(var j = 0; j<events.length; j++) {

  console.log(events[j]);

  request = gapi.client.calendar.events.insert({
    'calendarId': calendarId,
    'resource': events[j]
  });
  request.execute(function(resp) {
   console.log(resp);
 });
}

However, it turns out that all the events are placed in the same date into the calendar(which actually is the last date in array events[]). 但是,事实证明,所有事件都放在日历的同一日期(实际上是数组events []中的最后一个日期)。 I believe that it could be because the requests are callback functions, but I'm not sure. 我相信可能是因为请求是回调函数,但我不确定。

Would appreciate the help! 希望能有所帮助!

If you want to insert multiple events at once, you should use batch. 如果要一次插入多个事件,则应使用批处理。

var batch = gapi.client.newBatch();
batch.add(gapi.client.calendar.events.insert({
    'calendarId': 'primary',
    'resource': events[0]
}));
batch.add(gapi.client.calendar.events.insert({
    'calendarId': 'primary',
    'resource': events[1]
}));
batch.add(gapi.client.calendar.events.insert({
    'calendarId': 'primary',
    'resource': events[2]
}));
            ......

batch.then(function(){
    console.log('all jobs done!!!')
});

events[j] is being rebound on each iteration of the for loop. 在for循环的每次迭代中, events[j]都会反弹。 Try using an anonymous function to bind to the correct event: 尝试使用匿名函数绑定到正确的事件:

var request;
for(var j = 0; j<events.length; j++) {

  console.log(events[j]);

  request = function(resource) {  // Function that returns a request.
    return gapi.client.calendar.events.insert({
      'calendarId': calendarId,
      'resource': resource
    });
  }(events[j]);  // Bind to the current event.
  request.execute(function(resp) {
    console.log(resp);
  });
}

See the following question for more details on JavaScript arrays and closures: JavaScript closure inside loops – simple practical example 有关JavaScript数组和闭包的更多详细信息,请参见以下问题: 循环内的JavaScript闭包–简单的实际示例

Here is an easier-to-read version of the code above that moves all the processing into a function: 这是上面代码的易于阅读的版本,它将所有处理移到一个函数中:

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

for(var j = 0; j<events.length; j++) {
  makeRequest(events[j]);
}

As soredive, Mentioned, you need to use batch to add multiple events 作为提及,您需要使用批处理来添加多个事件

why batch? 为什么要批处理? The primary reason to use the batch API is to reduce network overhead and thus increase performance. 使用批处理API的主要原因是为了减少网络开销,从而提高性能。

example: 例:

createMultipleEvents() {
    const events = [ {
      'summary': 'sample test events1',
      'location': 'coimbatore',
      'start': {
          'date': '2018-08-29',
          'timeZone': 'America/Los_Angeles'
      },
      'end': {
          'date': '2018-08-29',
          'timeZone': 'America/Los_Angeles'
      }
  },
  {
    'summary': 'sample test events2',
    'location': 'coimbatore',
    'start': {
        'date': '2018-08-29',
        'timeZone': 'America/Los_Angeles'
    },
    'end': {
        'date': '2018-08-29',
        'timeZone': 'America/Los_Angeles'
    }
},
];
const batch = gapi.client.newBatch();
events.map((r, j) => {
  batch.add(gapi.client.calendar.events.insert({
    'calendarId': 'primary',
    'resource': events[j]
  }))
})
batch.then(function(){
  console.log('all jobs now dynamically done!!!')
});
  }

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

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