擴充屬性
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Events 資源的欄位涵蓋與活動相關的最常見資料,例如地點、開始時間等,但應用程式可能想儲存特定用途的其他中繼資料。Calendar API 可讓您為活動設定隱藏的鍵/值組合,稱為擴充屬性。擴充屬性可讓您輕鬆儲存事件的應用程式專屬資料,不必使用外部資料庫。
顯示設定
擴充屬性有兩種:私人和共用。
活動的所有參與者都能查看及編輯共用屬性,而私人屬性則會設定在活動的「副本」中,只有該參與者能存取。具體來說,私人屬性專屬於要求中使用的 calendarId
和 eventId
,而共用屬性則會顯示,無論要求中使用哪個 calendarId
都一樣。
新增及更新房源
擴充屬性是在 Events 資源上設定,與其他欄位一樣,可以在 insert、update 和 patch 要求中設定。建議使用 PATCH 要求,因為這樣您就能操作部分屬性,同時保留其他屬性。如果新增的屬性與現有屬性的鍵相同,系統會覆寫現有屬性。以下範例顯示如何設定私有屬性:
PATCH https://www.googleapis.com/calendar/v3/calendars/calendarId/events/eventId
{
"extendedProperties": {
"private": {
"petsAllowed": "yes"
}
}
}
刪除屬性
更新要求中未納入的任何屬性都會遭到刪除,但較好的做法是發出修補要求,將值設為空值。以下範例說明如何刪除私有屬性:
PATCH https://www.googleapis.com/calendar/v3/calendars/calendarId/events/eventId
{
"extendedProperties": {
"private": {
"petsAllowed": null
}
}
}
搜尋屬性
您可以使用 Events.list 要求,根據擴充屬性的值搜尋活動。將「privateExtendedProperty」privateExtendedProperty或「sharedExtendedProperty」sharedExtendedProperty欄位設為 propertyName=value
格式的限制,分別搜尋私人和共用屬性。以下範例會傳回具有私有屬性 petsAllowed=yes
的事件:
GET https://www.googleapis.com/calendar/v3/calendars/calendarId/events
?privateExtendedProperty=petsAllowed%3Dyes
您可以多次重複這些欄位,且限制會以 OR 運算子合併,因此只要事件符合其中一項限制,就會傳回。以下範例會找出具有私有屬性 petsAllowed=yes
或 isOutside=yes
的事件:
GET https://www.googleapis.com/calendar/v3/calendars/calendarId/events
?privateExtendedProperty=petsAllowed%3Dyes
&privateExtendedProperty=isOutside%3Dyes
請注意,私人和共用屬性的限制會以 AND 運算子合併,因此事件必須符合兩組限制才會傳回。
以下範例會找出具有私有屬性 petsAllowed=yes
和公開屬性 createdBy=myApp
的事件:
GET https://www.googleapis.com/calendar/v3/calendars/calendarId/events
?privateExtendedProperty=petsAllowed%3Dyes
&sharedExtendedProperty=createdBy%3DmyApp
限制
- 屬性鍵的大小上限為 44 個字元,如果屬性鍵超過這個長度,系統會自動捨棄。
- 屬性值的長度上限為 1024 個字元,超過此長度的屬性值會遭到截斷。
- 一個事件最多可有 300 個屬性,總大小上限為 32 KB (鍵大小 + 值大小)。這 300 個屬性包括活動所有「副本」的共用和私人屬性。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間:2025-08-29 (世界標準時間)。"],[],[],null,["# Extended properties\n\nThe fields of the [Events resources](/workspace/calendar/v3/reference/events)\ncover the most common data associated with an\nevent, such as location, start time, etc, but applications may want to store\nadditional metadata specific to their use case. The Calendar API provides the\nability to set hidden key-value pairs with an event, called\n[extended properties](/workspace/calendar/v3/reference/events#extendedProperties).\nExtended properties make it easy to store application-specific data for an event\nwithout having to utilize an external database.\n\nVisibility\n----------\n\nThere are two types of extended properties available: private and shared.\nShared properties are visible and editable by all attendees of an event, while\nprivate properties are set on one attendee's local \"copy\" of the event. More\nconcretely, private properties are specific to the `calendarId` and `eventId`\nused in the request, while shared properties will be shown regardless of the\n`calendarId` used in the request.\n\nAdd \\& update properties\n------------------------\n\nExtended properties are set on the Events resource, and like other fields can be\nset in [insert](/workspace/calendar/v3/reference/events/insert),\n[update](/workspace/calendar/v3/reference/events/update), and\n[patch](/workspace/calendar/v3/reference/events/patch) requests.\nUsing patch requests is the preferred\nmethod, as it allows you to manipulate some properties while leaving others\nuntouched. Adding a new property with the same key will overwrite any existing\nproperties with the same key. The following example shows setting a private\nproperty: \n\n```text\nPATCH https://www.googleapis.com/calendar/v3/calendars/calendarId/events/eventId\n``` \n\n {\n \"extendedProperties\": {\n \"private\": {\n \"petsAllowed\": \"yes\"\n }\n }\n }\n\nDelete properties\n-----------------\n\nAny properties not included in an update request will be deleted, but a better\napproach is to make a patch request to set the value to null. The following\nexample shows deleting a private property: \n\n```text\nPATCH https://www.googleapis.com/calendar/v3/calendars/calendarId/events/eventId\n``` \n\n {\n \"extendedProperties\": {\n \"private\": {\n \"petsAllowed\": null\n }\n }\n }\n\nSearch properties\n-----------------\n\nYou can search events based on the values on their extended properties using an\n[Events.list](/workspace/calendar/v3/reference/events/list) request.\nSet the field\n[privateExtendedProperty](/workspace/calendar/v3/reference/events/list#privateExtendedProperty)\nor\n[sharedExtendedProperty](/workspace/calendar/v3/reference/events/list#sharedExtendedProperty)\nto a constraint in the format `propertyName=value`,\nwhich searches against private and shared properties respectively. The following\nexample returns events with the private property `petsAllowed=yes`: \n\n```\nGET https://www.googleapis.com/calendar/v3/calendars/calendarId/events\n ?privateExtendedProperty=petsAllowed%3Dyes\n```\n\nYou can repeat these fields multiple times and the constraints are OR'ed\ntogether, so events only need to match one of the constraints to be returned.\nThe following example finds events with either the private property\n`petsAllowed=yes` or `isOutside=yes`: \n\n```\nGET https://www.googleapis.com/calendar/v3/calendars/calendarId/events\n ?privateExtendedProperty=petsAllowed%3Dyes\n &privateExtendedProperty=isOutside%3Dyes\n```\n\nBe aware that constraints on private and shared properties are AND'ed together\nhowever, so events must match both sets of constraints in order to be returned.\nThe following example finds events with the private property `petsAllowed=yes`\nand the public property `createdBy=myApp`: \n\n```\nGET https://www.googleapis.com/calendar/v3/calendars/calendarId/events\n ?privateExtendedProperty=petsAllowed%3Dyes\n &sharedExtendedProperty=createdBy%3DmyApp\n```\n\nLimits\n------\n\n1. The maximum size of a property's key is 44 characters, and properties with longer keys will be silently dropped.\n2. The maximum size of a property's value is 1024 characters, and properties with longer values will be silently truncated.\n3. An event can have up to 300 properties totaling up to 32kB in size (keys size + value size). These 300 properties include shared and private properties, across all \"copies\" of the event."]]