将事件从calendarList导入Javascript中的gcal API中的主日历中

问题描述 投票:0回答:1
  • 我正在使用Google Calendar API中的示例文档。我可以看到我所有的日历事件,但是我找不到使用Javascript将calendarList中的数据合并到当前数据的方法。

这是我所拥有的:

function listUpcomingEvents() {
    gapi.client.calendar.events.list({
      'calendarId': 'primary',
      'timeMin': (new Date()).toISOString(),
      'showDeleted': false,
      'singleEvents': true,
      'maxResults': 10,
      'orderBy': 'startTime'
    })
    .then(function(response) {
      var events = response.result.items;
      appendPre('');

      if (events.length > 0) {
        for (i = 0; i < events.length; i++) {
          var event = events[i];
          var when = event.start.dateTime;

          var calEventTitle = event.summary;
          var eventStatus = event.status;
          if (!when) {
            when = event.start.date;
          }

任何帮助将不胜感激。

google-apps-script google-calendar-api
1个回答
0
投票

如果您想merge calendar eventslocal variable,这是一个简单的示例。

  1. 创建calendarEvents数组
  2. 使用calendar.events.list()方法列出日历事件
  3. 将结果数据添加到calendarEvents
  4. 打印您的数据
// 1
let calendarEvents = []; 

function listEvents(auth){

  const calendar = google.calendar({version: 'v3', auth});
// 2
  calendar.events.list({

    calendarId: 'primary',
    timeMin: "2020-1-1T22:00:00.000Z",
    maxResults: 10,
    singleEvents: true,
    orderBy: 'startTime'

  }, (err, res) => {

    if (err) return console.log('Error #1001 - The Calendar API returned an error: ' + err);

// 3
    const events = res.data.items;    
    if (events.length) {
      events.map((event, i) => {
        const start = event.start.dateTime || event.start.date;
        calendarEvents.push(`\n ${start} - ${event.summary}`);
      });
// 4      
    console.log(calendarEvents)

    } else {
      console.log('No upcoming events found.');
    }

  });

}

如果要update calendar event,可以使用calendar.events.update()

这是一个简单的例子:


function updateEvent(auth){

  const calendar = google.calendar({version: 'v3', auth});

  calendar.events.update({
      "calendarId": "primary",
      "eventId": "___eventId___", // how to get eventId, scroll down
      "resource": {
        "start": {
          "dateTime": "2020-01-15T05:00:00-05:00"
        },
        "end": {
          "dateTime": "2020-01-15T06:00:00-05:00"
        },
        "location": "Updated location",
        "summary": "Updated summary",
        "attendees": [
          {
            "email":"[email protected]"
          }
        ]
      }

  }, (err, res) => {

    if (err) return console.log('Error #1002 - The Calendar API returned an error: ' + err);

  });

}


如何获取eventId?

使用events.list()方法,并使用console.log(res.data.items.events)作为响应,这是一个示例:

function listEvents(auth) {

  const calendar = google.calendar({version: 'v3', auth});
  calendar.events.list({
    calendarId: 'primary',
    timeMin: "2020-1-1T22:00:00.000Z",
    maxResults: 10,
    singleEvents: true,
    orderBy: 'startTime',
  }, (err, res) => {

    if (err) return console.log('Error #1001 - The Calendar API returned an error: ' + err);

        const events = res.data.items;
        console.log(events);

  });

}

这是输出,我只有1个事件,

所以id12sv34jssjjov56jq7c8vlbjkd

[
  {
    kind: 'calendar#event',
    etag: '"3157989414186000"',
    id: '12sv34jssjjov56jq7c8vlbjkd',
    status: 'confirmed',
    htmlLink: 'https://www.google.com/calendar/event?eid=blablabla',
    created: '2020-01-14T09:23:46.000Z',
    updated: '2020-01-14T09:38:27.093Z',
    summary: 'Updated summary',
    location: 'Updated location',
    creator: { email: '[email protected]', self: true },
    organizer: { email: '[email protected]', self: true },
    start: { dateTime: '2020-01-15T05:00:00-05:00' },
    end: { dateTime: '2020-01-15T06:00:00-05:00' },
    iCalUID: '[email protected]',
    sequence: 1,
    attendees: [ [Object] ],
    hangoutLink: 'https://meet.google.com/123-4567-890',
    conferenceData: {
      entryPoints: [Array],
      conferenceSolution: [Object],
      conferenceId: '123-4567-890',
      signature: '32ipslWNrbuVDju4rHvyOpVzH/gj'
    },
    reminders: { useDefault: true }
  }
]

我希望这些例子能使事情变得更清楚。如果出现问题,您始终可以使用Google Calendar API Reference页面右侧的Try This Api,直接在浏览器中测试您的请求>

enter image description here

参考:

  • calendar.events.list()
  • calendar.events.update()
  • © www.soinside.com 2019 - 2024. All rights reserved.