当尝试为“事件”数组中的第一个 CalendarEvent
设置标签时,出现“不允许操作”错误。 CalendarEvent
的其他属性,包括getStartTime()
、getTag()
、setLocation()
等都完美功能性的。
我也是这个日历事件的所有者,所以我不明白为什么我不能标记这个事件。我也尝试过不同的事件,包括我拥有的和我不拥有的,但没有一个允许我设置标签。
function myFunction() {
var myCal = CalendarApp.getDefaultCalendar();
var now = new Date();
var events = myCal.getEvents(new Date(now.valueOf() - 14*24*3600*1000), new Date(now.valueOf() + 14*24*3600*1000))
var temp = events[0];
temp.setTag('key', 'pair')
}
最佳答案
我遇到了同样的问题。
根本原因似乎是如果您是事件的创建者,则只能在 calendarApp 事件上设置标签。现在,我正在测试的日历插件正在使用我的用户上下文中的 OAuth 范围。也许有一种方法可以使用工作区管理员的上下文,但不确定,我是使用 Google API 的新手。
重现:
- 创建您自己的 Google 日历事件。
- 让其他人创建 Google 日历事件并邀请您参加。
- 在您的事件上运行
setTag('foo', 'bar')
,它应该(?)有效 - 在其他人的事件上运行
setTag('foo', 'bar')
,它应该(?)失败
我正在为 CalendarApp 插件使用 Google Apps 脚本,至少,我认为您还需要以下 OAuth 范围:https://www.googleapis.com/auth/日历”
这是一个简单的例子:
run_tests();
function run_tests() {
test_setTag('your_email@example.com', 'event-id-that-you-created');
test_setTag('other_person@example.com', 'event-id-that-other-person-created');
}
function test_setTag(calendarId, eventId) {
var event = CalendarApp.getCalendarById(calendarId).getEventById(eventId);
event.setTag('foo', 'bar');
}
编辑 - 更完整的答案:
更新了理解。
CalendarApp
是对低级 Calendar
API 的高级抽象。虽然更易于使用,但 CalendarApp 并未公开低级 API 的全部功能。
日历 API 中的事件具有 extendedProperties
,它们只是可选的元数据,用户和应用可以使用它们来扩展功能。 extendedProperties 中有 shared
和 private
属性,其行为如下:
shared
= 受邀参加事件的所有用户共享一个值。受邀用户可以读取这些值(具有适当的 OAuth 范围),但只有事件创建者可以修改它们。private
= 每个受邀参加事件的用户都可以创建和维护自己独立的私有(private)属性。其他用户无法读取或写入这些值。
我遇到的问题(也许你也是?)是 CalendarApp
“标签”是 shared
extendedProperties 的抽象...即您不能在您不是创建者或所有者的事件上调用 setTag
。
通过使用低级日历 API,我能够成功地为我不拥有的事件设置 private 属性,但(正如预期的那样)尝试设置共享属性失败(有很多顺便说一下,比 CalendarApp
更清晰的错误代码。
const calendarId = 'your-calendar-id';
const eventId = 'an-event-someone-else-made';
var currentProperties = Calendar.Events.get(calendarId, eventId).extendedProperties || {};
console.log('Properties before patch: ', JSON.stringify(currentProperties, null, 2));
var schemaOfPatch = {
extendedProperties: {
private: {
foo: 'bar'
}
}
};
Calendar.Events.patch(schemaOfPatch, calendarId, eventId);
var patchedProperties = Calendar.Events.get(calendarId, eventId).extendedProperties || {};
console.log(`Properties after patch: ${patchedProperties}`);
// The null will remove the private property 'foo'
var schemaOfPatch = {
extendedProperties: {
private: {
foo: null
}
}
};
Calendar.Events.patch(schemaOfPatch, calendarId, eventId);
以上结果:
successful patch of private property to someone else's event
如果您将 private
更改为shared
,您将看到此错误:
patch fails if made against shared property on event you do not own
关于google-apps-script - CalendarEvent 中的 SetTag() 导致 "Action not allowed"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32002336/