Google 应用脚本 - 日历更新触发 4 次
Google App Script - Calendar Update Trigger Firing 4 Times
目标:当 Google 工作区 1 中的辅助日历事件更新(新建、编辑、删除)时,更新 Google 工作区 1 中的主日历以添加新的、编辑现有事件,或删除现有事件。
注意:Google 工作区 1 中的辅助日历是从 Google 工作区 2 共享的。因此,对事件的实际更新发生在 Google 工作区 2 中的辅助日历中,但是几乎立即反映为 Google 工作区 1.
中的辅助日历中的更新
结果:当 运行 手动时,这是按预期工作的。
问题:如果手动添加触发器 运行 秒后更新日历(不是基于时间的),触发器会触发脚本的四个并发执行。这会在 Google 工作区 1 中的辅助日历上复制日历事件四次。
已尝试解决:我设置了锁定服务。这确实可以防止日历事件重复。但是,这会导致四次执行中的三次失败。这导致向我发送 3 封电子邮件,每次脚本失败时发送一封电子邮件。
问题:如何让这个脚本只运行一次?我认为简单的答案是只放入一个 if/else 如果锁定,则终止脚本。我已经看过但找不到如何编码的示例。
请参阅下面的代码和屏幕截图。
function calEventUpdate(){
var lock = LockService.getScriptLock();
lock.waitLock(15000);
//code here
var id="e48a0bmth3sn787dotb4n2rtjs@group.calendar.google.com"; // CHANGE - id of the secondary calendar to pull events from (Anduin Out)
var today=new Date();
var enddate=new Date();
enddate.setDate(today.getDate()+7); // how many days in advance to monitor and block off time
var secondaryCal=CalendarApp.getCalendarById(id);
var secondaryEvents=secondaryCal.getEvents(today,enddate);
var primaryCal=CalendarApp.getDefaultCalendar();
var primaryEvents=primaryCal.getEvents(today,enddate); // all primary calendar events
var primaryEventTitle="Busy"; // update this to the text you'd like to appear in the new events created in primary calendar
var stat=1;
var evi, existingEvent;
var primaryEventsFiltered = []; // to contain primary calendar events that were previously created from secondary calendar
var primaryEventsUpdated = []; // to contain primary calendar events that were updated from secondary calendar
var primaryEventsCreated = []; // to contain primary calendar events that were created from secondary calendar
var primaryEventsDeleted = []; // to contain primary calendar events previously created that have been deleted from secondary calendar
Logger.log('Number of primaryEvents: ' + primaryEvents.length);
Logger.log('Number of secondaryEvents: ' + secondaryEvents.length);
// create filtered list of existing primary calendar events that were previously created from the secondary calendar
for (pev in primaryEvents)
{
var pEvent = primaryEvents[pev];
if (pEvent.getTitle() == primaryEventTitle)
{ primaryEventsFiltered.push(pEvent); }
}
// process all events in secondary calendar
for (sev in secondaryEvents)
{
stat=1;
evi=secondaryEvents[sev];
Utilities.sleep(1000); // added to prevent "too many updates" error on a busy calendar
// if the secondary event has already been blocked in the primary calendar, update it
for (existingEvent in primaryEventsFiltered)
{
var pEvent = primaryEventsFiltered[existingEvent];
var secondaryTitle = evi.getTitle();
var secondaryDesc = evi.getDescription();
if ((pEvent.getStartTime().getTime()==evi.getStartTime().getTime()) && (pEvent.getEndTime().getTime()==evi.getEndTime().getTime()))
{
stat=0;
pEvent.setTitle(primaryEventTitle);
//pEvent.setDescription(secondaryTitle + '\n\n' + secondaryDesc);
// event.setDescription(evi.getTitle() + '\n\n' + evi.getDescription());
pEvent.setVisibility(CalendarApp.Visibility.PRIVATE); // set blocked time as private appointments in work calendar
pEvent.setColor('9');
primaryEventsUpdated.push(pEvent.getId());
Logger.log('PRIMARY EVENT UPDATED'
+ '\nprimaryId: ' + pEvent.getId() + ' \nprimaryTitle: ' + pEvent.getTitle() + ' \nprimaryDesc: ' + pEvent.getDescription());
}
}
if (stat==0) continue;
var d = evi.getStartTime();
var n = d.getDay();
if (evi.isAllDayEvent())
{
continue; // Do nothing if the event is an all-day or multi-day event. This script only syncs hour-based events
}
else if (n==1 || n==2 || n==3 || n==4 || n==5) // skip weekends. Delete this if you want to include weekends
// if the secondary event does not exist in the primary calendar, create it
{
var newEvent = primaryCal.createEvent(primaryEventTitle,evi.getStartTime(),evi.getEndTime()); // change the Booked text to whatever you would like your merged event titles to be
// alternative version below that copies the exact secondary event information into the primary calendar event
// var newEvent = primaryCal.createEvent(evi.getTitle(),evi.getStartTime(),evi.getEndTime(), {location: evi.getLocation(), description: evi.getDescription()});
//newEvent.setDescription(evi.getTitle() + '\n\n' + evi.getDescription());
newEvent.setVisibility(CalendarApp.Visibility.PRIVATE); // set blocked time as private appointments in work calendar
newEvent.setColor('9');
newEvent.removeAllReminders(); // so you don't get double notifications. Delete this if you want to keep the default reminders for your newly created primary calendar events
primaryEventsCreated.push(newEvent.getId());
Logger.log('PRIMARY EVENT CREATED'
+ '\nprimaryId: ' + newEvent.getId() + '\nprimaryTitle: ' + newEvent.getTitle() + '\nprimaryDesc ' + newEvent.getDescription() + '\n');
}
}
// if a primary event previously created no longer exists in the secondary calendar, delete it
for (pev in primaryEventsFiltered)
{
var pevIsUpdatedIndex = primaryEventsUpdated.indexOf(primaryEventsFiltered[pev].getId());
if (pevIsUpdatedIndex == -1)
{
var pevIdToDelete = primaryEventsFiltered[pev].getId();
Logger.log(pevIdToDelete + ' deleted');
primaryEventsDeleted.push(pevIdToDelete);
primaryEventsFiltered[pev].deleteEvent();
}
}
Logger.log('Primary events previously created: ' + primaryEventsFiltered.length);
Logger.log('Primary events updated: ' + primaryEventsUpdated.length);
Logger.log('Primary events deleted: ' + primaryEventsDeleted.length);
Logger.log('Primary events created: ' + primaryEventsCreated.length);
lock.releaseLock();
}```
[Simultaneous script executions][1]
[Trigger][2]
[Trigger Settings][3]
[1]: https://i.stack.imgur.com/AcbJU.png
[2]: https://i.stack.imgur.com/u6giq.png
[3]: https://i.stack.imgur.com/hG93W.png
您可以通过Lock.hasLock().
判断另一个脚本实例当前是否有锁
请注意,此方法并不总是避免冲突的最佳方法,因为无法判断锁是否是在更新 同一事件 或另一个事件恰好同时触发了一个触发器——正如the documentation所说,“触发器不会告诉你哪个事件改变了或者它是如何改变的."
您可能需要仔细研究Google Calendar events以确定如何同步日历。
或者,share the calendar 而不是尝试同步。
出于文档目的发布此内容。
这可能是一个错误:
您描述的行为(EventUpdateTrigger -as well as EventOpenTrigger 多次触发)可能是一个错误,目前正在 Google 问题跟踪器上进行调查:
请考虑 starring the referenced issue 以跟踪它并帮助确定它的优先级。
解决方法:
As , use Lock.hasLock() 以避免连续触发器执行导致事件重复多次。
目标:当 Google 工作区 1 中的辅助日历事件更新(新建、编辑、删除)时,更新 Google 工作区 1 中的主日历以添加新的、编辑现有事件,或删除现有事件。
注意:Google 工作区 1 中的辅助日历是从 Google 工作区 2 共享的。因此,对事件的实际更新发生在 Google 工作区 2 中的辅助日历中,但是几乎立即反映为 Google 工作区 1.
中的辅助日历中的更新结果:当 运行 手动时,这是按预期工作的。
问题:如果手动添加触发器 运行 秒后更新日历(不是基于时间的),触发器会触发脚本的四个并发执行。这会在 Google 工作区 1 中的辅助日历上复制日历事件四次。
已尝试解决:我设置了锁定服务。这确实可以防止日历事件重复。但是,这会导致四次执行中的三次失败。这导致向我发送 3 封电子邮件,每次脚本失败时发送一封电子邮件。
问题:如何让这个脚本只运行一次?我认为简单的答案是只放入一个 if/else 如果锁定,则终止脚本。我已经看过但找不到如何编码的示例。
请参阅下面的代码和屏幕截图。
function calEventUpdate(){
var lock = LockService.getScriptLock();
lock.waitLock(15000);
//code here
var id="e48a0bmth3sn787dotb4n2rtjs@group.calendar.google.com"; // CHANGE - id of the secondary calendar to pull events from (Anduin Out)
var today=new Date();
var enddate=new Date();
enddate.setDate(today.getDate()+7); // how many days in advance to monitor and block off time
var secondaryCal=CalendarApp.getCalendarById(id);
var secondaryEvents=secondaryCal.getEvents(today,enddate);
var primaryCal=CalendarApp.getDefaultCalendar();
var primaryEvents=primaryCal.getEvents(today,enddate); // all primary calendar events
var primaryEventTitle="Busy"; // update this to the text you'd like to appear in the new events created in primary calendar
var stat=1;
var evi, existingEvent;
var primaryEventsFiltered = []; // to contain primary calendar events that were previously created from secondary calendar
var primaryEventsUpdated = []; // to contain primary calendar events that were updated from secondary calendar
var primaryEventsCreated = []; // to contain primary calendar events that were created from secondary calendar
var primaryEventsDeleted = []; // to contain primary calendar events previously created that have been deleted from secondary calendar
Logger.log('Number of primaryEvents: ' + primaryEvents.length);
Logger.log('Number of secondaryEvents: ' + secondaryEvents.length);
// create filtered list of existing primary calendar events that were previously created from the secondary calendar
for (pev in primaryEvents)
{
var pEvent = primaryEvents[pev];
if (pEvent.getTitle() == primaryEventTitle)
{ primaryEventsFiltered.push(pEvent); }
}
// process all events in secondary calendar
for (sev in secondaryEvents)
{
stat=1;
evi=secondaryEvents[sev];
Utilities.sleep(1000); // added to prevent "too many updates" error on a busy calendar
// if the secondary event has already been blocked in the primary calendar, update it
for (existingEvent in primaryEventsFiltered)
{
var pEvent = primaryEventsFiltered[existingEvent];
var secondaryTitle = evi.getTitle();
var secondaryDesc = evi.getDescription();
if ((pEvent.getStartTime().getTime()==evi.getStartTime().getTime()) && (pEvent.getEndTime().getTime()==evi.getEndTime().getTime()))
{
stat=0;
pEvent.setTitle(primaryEventTitle);
//pEvent.setDescription(secondaryTitle + '\n\n' + secondaryDesc);
// event.setDescription(evi.getTitle() + '\n\n' + evi.getDescription());
pEvent.setVisibility(CalendarApp.Visibility.PRIVATE); // set blocked time as private appointments in work calendar
pEvent.setColor('9');
primaryEventsUpdated.push(pEvent.getId());
Logger.log('PRIMARY EVENT UPDATED'
+ '\nprimaryId: ' + pEvent.getId() + ' \nprimaryTitle: ' + pEvent.getTitle() + ' \nprimaryDesc: ' + pEvent.getDescription());
}
}
if (stat==0) continue;
var d = evi.getStartTime();
var n = d.getDay();
if (evi.isAllDayEvent())
{
continue; // Do nothing if the event is an all-day or multi-day event. This script only syncs hour-based events
}
else if (n==1 || n==2 || n==3 || n==4 || n==5) // skip weekends. Delete this if you want to include weekends
// if the secondary event does not exist in the primary calendar, create it
{
var newEvent = primaryCal.createEvent(primaryEventTitle,evi.getStartTime(),evi.getEndTime()); // change the Booked text to whatever you would like your merged event titles to be
// alternative version below that copies the exact secondary event information into the primary calendar event
// var newEvent = primaryCal.createEvent(evi.getTitle(),evi.getStartTime(),evi.getEndTime(), {location: evi.getLocation(), description: evi.getDescription()});
//newEvent.setDescription(evi.getTitle() + '\n\n' + evi.getDescription());
newEvent.setVisibility(CalendarApp.Visibility.PRIVATE); // set blocked time as private appointments in work calendar
newEvent.setColor('9');
newEvent.removeAllReminders(); // so you don't get double notifications. Delete this if you want to keep the default reminders for your newly created primary calendar events
primaryEventsCreated.push(newEvent.getId());
Logger.log('PRIMARY EVENT CREATED'
+ '\nprimaryId: ' + newEvent.getId() + '\nprimaryTitle: ' + newEvent.getTitle() + '\nprimaryDesc ' + newEvent.getDescription() + '\n');
}
}
// if a primary event previously created no longer exists in the secondary calendar, delete it
for (pev in primaryEventsFiltered)
{
var pevIsUpdatedIndex = primaryEventsUpdated.indexOf(primaryEventsFiltered[pev].getId());
if (pevIsUpdatedIndex == -1)
{
var pevIdToDelete = primaryEventsFiltered[pev].getId();
Logger.log(pevIdToDelete + ' deleted');
primaryEventsDeleted.push(pevIdToDelete);
primaryEventsFiltered[pev].deleteEvent();
}
}
Logger.log('Primary events previously created: ' + primaryEventsFiltered.length);
Logger.log('Primary events updated: ' + primaryEventsUpdated.length);
Logger.log('Primary events deleted: ' + primaryEventsDeleted.length);
Logger.log('Primary events created: ' + primaryEventsCreated.length);
lock.releaseLock();
}```
[Simultaneous script executions][1]
[Trigger][2]
[Trigger Settings][3]
[1]: https://i.stack.imgur.com/AcbJU.png
[2]: https://i.stack.imgur.com/u6giq.png
[3]: https://i.stack.imgur.com/hG93W.png
您可以通过Lock.hasLock().
判断另一个脚本实例当前是否有锁请注意,此方法并不总是避免冲突的最佳方法,因为无法判断锁是否是在更新 同一事件 或另一个事件恰好同时触发了一个触发器——正如the documentation所说,“触发器不会告诉你哪个事件改变了或者它是如何改变的."
您可能需要仔细研究Google Calendar events以确定如何同步日历。
或者,share the calendar 而不是尝试同步。
出于文档目的发布此内容。
这可能是一个错误:
您描述的行为(EventUpdateTrigger -as well as EventOpenTrigger 多次触发)可能是一个错误,目前正在 Google 问题跟踪器上进行调查:
请考虑 starring the referenced issue 以跟踪它并帮助确定它的优先级。
解决方法:
As