如何通过Google Apps Script获取Google Calendar中的工作位置信息
解决方案
你在Calendar官方文档找不到对应接口是因为用户设置的非事件关联的工作位置数据,不归Calendar API管理,而是存储在Google账号个人资料中,需要调用Google People API拉取。
前置配置
- 打开Google Apps Script编辑器,点击左侧「服务」→「添加服务」,找到
People API,选择v1版本添加 - 脚本需要配置个人资料读取权限,首次运行时按提示授权即可
可直接运行的代码示例
function getMyWorkingLocations() { // 请求当前用户的locations字段数据 const userProfile = People.People.get('people/me', { personFields: 'locations' }); // 过滤出所有工作位置条目 const allWorkingLocations = userProfile.locations.filter(item => item.type === 'workingLocation'); // 按需求处理结果,以下是打印示例 allWorkingLocations.forEach(workLoc => { const dateInfo = workLoc.currentStartDate ? `${workLoc.currentStartDate.year}年${workLoc.currentStartDate.month}月${workLoc.currentStartDate.day}日起生效` : '默认固定工作位置'; console.log(`${dateInfo}:${workLoc.value}`); // 若需要完整结构化地址可以取 workLoc.locationAddress 字段 }); return allWorkingLocations; }
注意事项
- 单次调用默认返回当前生效及未来最长6周的工作位置数据,超过这个范围的配置无法直接拉取
- 若需要修改/新增工作位置,可调用
People.People.updateContact方法,传入对应结构的workingLocation类型locations字段即可 - 如果是企业域账号,需要管理员开启了工作位置功能权限才能正常拉取,个人账号无此限制
内容的提问来源于stack exchange,提问作者Lucile
相关产品推荐
相关产品推荐

