Google Calendar API:如何过滤无实际事件的日期结果
问题
我已经成功通过Google Calendar API拉取事件、日历及多日历数据,但返回结果包含对应月份的所有日期,无论该日期是否存在实际事件。请问需要验证哪个字段或使用何种查询语句来筛选掉这些“无事件日期”?
当前使用的PHP查询参数数组:
$optParams = array( 'maxResults' => 100, 'orderBy' => 'startTime', 'singleEvents' => TRUE, //'timeMin' => date('c'), 'timeMin' => date('c', strtotime('-30 days')) );
“空事件”示例:
Google\Service\Calendar\Event Object ( [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [collection_key:protected] => recurrence [anyoneCanAddSelf] => [attachmentsType:protected] => Google\Service\Calendar\EventAttachment [attachmentsDataType:protected] => array [attendeesType:protected] => Google\Service\Calendar\EventAttendee [attendeesDataType:protected] => array [attendeesOmitted] => [colorId] => [conferenceDataType:protected] => Google\Service\Calendar\ConferenceData [conferenceDataDataType:protected] => [created] => 2022-04-29T17:19:37.000Z [creatorType:protected] => Google\Service\Calendar\EventCreator [creatorDataType:protected] => [description] => [endType:protected] => Google\Service\Calendar\EventDateTime [endDataType:protected] => [endTimeUnspecified] => [etag] => "3412060797546000" [eventType] => workingLocation [extendedPropertiesType:protected] => Google\Service\Calendar\EventExtendedProperties [extendedPropertiesDataType:protected] => [focusTimePropertiesType:protected] => Google\Service\Calendar\EventFocusTimeProperties [focusTimePropertiesDataType:protected] => [gadgetType:protected] => Google\Service\Calendar\EventGadget [gadgetDataType:protected] => [guestsCanInviteOthers] => [guestsCanModify] => [guestsCanSeeOtherGuests] => [hangoutLink] => [htmlLink] => https://www.google.com/calendar/event?eid=Ym0wdGdvMzNiZnV1MnRiZzc3aDg4OTJyaGNfMjAyNDA5MTYgZ3JlZ2dAYmx1bW9vY3JlYXRpdmUuY29t [iCalUID] => bm0tgo33bfuu2tbg77h8892rhc@google.com [id] => bm0tgo33bfuu2tbg77h8892rhc_20240916 [kind] => calendar#event [location] => [locked] => [organizerType:protected] => Google\Service\Calendar\EventOrganizer [organizerDataType:protected] => [originalStartTimeType:protected] => Google\Service\Calendar\EventDateTime [originalStartTimeDataType:protected] => [outOfOfficePropertiesType:protected] => Google\Service\Calendar\EventOutOfOfficeProperties [outOfOfficePropertiesDataType:protected] => [privateCopy] => [recurrence] => [recurringEventId] => bm0tgo33bfuu2tbg77h8892rhc [remindersType:protected] => Google\Service\Calendar\EventReminders [remindersDataType:protected] => [sequence] => 0 [sourceType:protected] => Google\Service\Calendar\EventSource [sourceDataType:protected] => [startType:protected] => Google\Service\Calendar\EventDateTime [startDataType:protected] => [status] => confirmed [summary] => Office [transparency] => transparent [updated] => 2024-01-23T17:19:58.773Z [visibility] => public [workingLocationPropertiesType:protected] => Google\Service\Calendar\EventWorkingLocationProperties [workingLocationPropertiesDataType:protected] => [creator] => Google\Service\Calendar\EventCreator Object ( [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [displayName] => Gregg Moore [email] => myemail@myemail.com [id] => [self] => 1 ) [organizer] => Google\Service\Calendar\EventOrganizer Object ( [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [displayName] => Gregg Moore [email] => myemail@myemail.com [id] => [self] => 1 ) [start] => Google\Service\Calendar\EventDateTime Object ( [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [date] => 2024-09-16 [dateTime] => [timeZone] => ) [end] => Google\Service\Calendar\EventDateTime Object ( [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [date] => 2024-09-17 [dateTime] => [timeZone] => ) [originalStartTime] => Google\Service\Calendar\EventDateTime Object ( [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [date] => 2024-09-16 [dateTime] => [timeZone] => ) [reminders] => Google\Service\Calendar\EventReminders Object ( [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [collection_key:protected] => overrides [overridesType:protected] => Google\Service\Calendar\EventReminder [overridesDataType:protected] => array [useDefault] => ) [workingLocationProperties] => Google\Service\Calendar\EventWorkingLocationProperties Object ( [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [customLocationType:protected] => Google\Service\Calendar\EventWorkingLocationPropertiesCustomLocation [customLocationDataType:protected] => [homeOffice] => [officeLocationType:protected] => Google\Service\Calendar\EventWorkingLocationPropertiesOfficeLocation [officeLocationDataType:protected] => [type] => officeLocation [officeLocation] => Google\Service\Calendar\EventWorkingLocationPropertiesOfficeLocation Object ( [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [buildingId] => [deskId] => [floorId] => [floorSectionId] => [label] => ) ) )
解决方案
1. 通过API查询参数过滤
从示例的空事件可以看到,这类事件的eventType为workingLocation,属于Google Calendar的特殊事件类型(工作地点标记),并非常规日程事件。你可以直接在查询参数中过滤这类事件:
方式一:排除指定事件类型
$optParams = array( 'maxResults' => 100, 'orderBy' => 'startTime', 'singleEvents' => TRUE, 'timeMin' => date('c', strtotime('-30 days')), 'q' => '-eventType:workingLocation' // 排除工作地点类型事件 );
方式二:仅获取默认类型事件
$optParams = array( 'maxResults' => 100, 'orderBy' => 'startTime', 'singleEvents' => TRUE, 'timeMin' => date('c', strtotime('-30 days')), 'eventTypes' => ['default'] // 仅获取常规日程事件 );
2. 拉取结果后过滤
如果API层面的过滤不生效,也可以在获取事件列表后,通过字段判断筛选:
$events = $service->events->listEvents('primary', $optParams); $filteredEvents = []; foreach ($events->getItems() as $event) { // 排除workingLocation类型的事件 if ($event->getEventType() !== 'workingLocation') { $filteredEvents[] = $event; } } // 后续业务逻辑使用$filteredEvents即可
内容的提问来源于stack exchange,提问作者Gregg Moore
相关产品推荐
相关产品推荐

