【问题标题】:Counting days "Out of office" events with Apps Script Calendar Service使用 Apps 脚本日历服务计算“不在办公室”事件的天数
【发布时间】: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 获取“不在办公室”信息,以及如何处理多天的“不在办公室”事件?

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    您可以使用Calendar API with Apps ScriptEvents: list 方法,您将收到一个Event 对象。在那里,您可以按eventType 属性过滤以获取outOfOffice

    【讨论】:

    • 我尝试使用日历 API,但 Calendar.Events.list('primary').items.filter(x =&gt; x.eventType == "outOfOffice").map(x =&gt; {const {summary, start, end} = x; return {summary, start, end}}) 不包含单日不在办公室的事件,而只包含多天
    • 有关我的基本实现,请参阅我的答案。
    • 请不要写一个不是最终的答案,而是更新问题,以免其他用户感到困惑。另外,您能否分享更多关于您无法离开办公室的问题以及新代码的复制步骤的详细信息?
    • 完毕。要重现它很简单,您可以在日历中创建一个为期两天的外出活动,它确实会被上面的filter(x =&gt; x.eventType == "outOfOffice") 捕获,但是当您创建一个一整天的活动时,它不会。
    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 2014-11-16
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多