简体   繁体   中英

How to get the location of an event in Google Calendar through the Google Calendar API in Javascript?

as mentioned in the title I would like to get the location of an event in Google Calendar through the Google Calendar API in Javascript. Therefore I used the Google API sample.

/**
 * Print the summary and start datetime/date of the next ten events in
 * the authorized user's calendar. If no events are found an
 * appropriate message is printed.
 */
function listUpcomingEvents() {
    gapi.client.calendar.events.list({
        'calendarId': 'primary',
        'timeMin': (new Date()).toISOString(),
        'showDeleted': false,
        'singleEvents': true,
        'location': '--4-Bikini Bottom (12)',
        'maxResults': 10,
        'orderBy': 'startTime'


    }).then(function (response) {
        var events = response.result.items;
        appendPre('Upcoming events:');

        if (events.length > 0) {
            for (i = 0; i < events.length; i++) {
                var event = events[i];
                var when = event.start.dateTime;
                if (!when) {
                    when = event.start.date;
                }
                appendPre(event.summary + ' (' + when + ')')
            }
        } else {
            appendPre('No upcoming events found.');
        }
    });
}

Now the part that does not work is 'location'. I am able to get the dates and name of the next 10 calendar events but not the location. Does anyone know how to do this? Thanks in advance!!

Recommendation:

I have replicated your code by following the guide from JavaScript Quickstart and reviewing the additional info from Events . I was able to tweak one of the appendPre() lines by adding event.location to show the location of the upcoming test event that I've created on my calendar, as shown here:

  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('Upcoming events:');

      if (events.length > 0) {
        for (i = 0; i < events.length; i++) {
          var event = events[i];
          var when = event.start.dateTime;
          if (!when) {
            when = event.start.date;
          }
          appendPre(event.summary + ' (' + when + ')\n'+'Location: '+event.location)
        }
      } else {
        appendPre('No upcoming events found.');
      }
    });
  }

Here's the result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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