【发布时间】:2022-11-03 01:04:33
【问题描述】:
我想计算每个月底我在 Google 日历中的休息天数。
通过以下简单的公式,
total days off = out of office days + bank holidays
在 Apps Script 中使用日历服务,我使用#dayoff 标签技巧提出了该解决方案,并且仅在周一至周五过滤。
编辑
我按照 Kessy 的高级日历 API 提示更新到此解决方案,但仍然无法获得单日外出活动。
const howManyDays = () => {
WEEK_DAYS = [1,2,3,4,5]
// https://stackoverflow.com/a/25672779/1360476
const countWeekDays = (start, end) => {
const ndays = 1 + Math.round((end-start)/(24*3600*1000))
const sum = (a,b) => a + Math.floor((ndays + (start.getDay()+6-b) % 7)/7)
return WEEK_DAYS.reduce(sum, 0)
}
const diffDays = (start, end) => {
return Math.ceil(Math.abs(new Date(end) - new Date(start)) / (1000 * 60 * 60 * 24))
}
const date = new Date()
const currentMonth = date.getMonth()
const currentYear = date.getFullYear()
const start = new Date(currentYear, currentMonth, 1)
var end = new Date(currentYear, currentMonth + 1, 0)
const workDays = countWeekDays(start, end)
const myCalendar = Calendar.Events.list('primary')
const outOfOffice = myCalendar.items
.filter(x => x.eventType == "outOfOffice")
.map(x => {
const {summary, start, end} = x
return {summary, start: new Date(start.dateTime), duration: diffDays(start.dateTime,end.dateTime)}
})
.filter(x => start <= x.start && x.start <= end)
.map(x => x.duration) // FIXME: we only have duration >= 2 here
.reduce((x, y) => x + y)
// const singleEvents = CalendarApp.getDefaultCalendar().getEvents(start, end, {search: '#dayoff'})
//.filter(x => WEEK_DAYS.includes(x.getStartTime().getDay()))
// Logger.log(singleEvents)
// shows up one-day out of office events that don't get captured by the advanced Calendar API
const holidays = CalendarApp.getCalendarsByName('Holidays in France')[0].getEvents(start, end)
.filter(x => WEEK_DAYS.includes(x.getStartTime().getDay()))
.length
message = ` ${workDays} working days from ${start.toLocaleDateString()} to ${end.toLocaleDateString()} \n`
+ `- ${outOfOffice} out of office days \n`
+ `- ${holidays} bank holidays \n`
MailApp.sendEmail('your.email@provider.com', 'My timesheet', message)
Logger.log(message)
}
我想与我的同事分享该脚本并删除我的#dayoff 技巧,如何直接从 API 获取“不在办公室”信息,以及如何处理多天的“不在办公室”事件?
【问题讨论】: