【问题标题】:Finding an open time slot in Google Calendar in Chrome Extension在 Chrome 扩展程序中查找 Google 日历中的开放时间段
【发布时间】:2022-11-16 21:55:34
【问题描述】:

我想知道在谷歌日历中找到一个开放时间段的聪明方法是什么。 目前我正在使用对 Google 日历 API 的 HTTP 请求,特别是 freeBusy 调用。

我需要专门寻找一个 30 分钟的窗口,所以我想到的一种蛮力方法是获取当前时间,检查窗口,然后通过将当前时间增加 5 分钟来遍历当天剩余的时间。不过这看起来很简陋。

这是我必须检查以找到 30 分钟空闲时间块的当前代码:

var date = {
    value: "2022-11-15",
  };
  var startTime = {
    value: "22:30",
  };
  var endTime = {
    value: "23:00",
  };

  var dateObj = {
    timeMin: date.value + "T" + startTime.value + ":00-08:00",
    timeMax: date.value + "T" + endTime.value + ":00-08:00",
    items: [
      {
        id: calendarId,
      },
    ],
    timeZone: "America/Vancouver",
  };
  console.log(dateObj);
  const test = {
    method: "POST",
    async: true,
    headers: {
      Authorization: "Bearer " + token,
      "Content-Type": "application/json",
    },
    body: JSON.stringify(dateObj),
  };
  
  await fetch(
    "https://www.googleapis.com/calendar/v3/freeBusy",

    test
  )
    .then((response) => response.json()) // Transform the data into json
    .then((data) => console.log(data))
    .catch((err) => console.log(err));

【问题讨论】:

    标签: javascript google-chrome-extension google-calendar-api


    【解决方案1】:

    我认为您只需要调用一次 freeBusy 端点。

    这将是我的方法:

    1. 我在工作时间打电话给freeBusy
      {
       "timeMax": "2022-11-17T08:00:00.000",
       "timeMin": "2022-11-17T17:00:00.000Z",
       "items": [{"id": "primary"}]
      }
      
      1. 在响应中我有用户不可用的时间。
      ...
       "calendars": {
        "primary": {
         "busy": [
          {
           "start": "2022-11-17T15:00:00Z",
           "end": "2022-11-17T15:30:00Z"
          },
          {
           "start": "2022-11-17T16:45:00Z",
           "end": "2022-11-17T17:15:00Z"
          }
         ]
        }
      ... 
      
      1. 有了这些信息并假设在我的前端我有一个表单,用户可以在其中输入日期并按照RFC3339 进行格式化。
      const busyInfo = [
        {
          "start": "2022-11-17T15:00:00Z",
          "end": "2022-11-17T15:30:00Z"
        },
        {
          "start": "2022-11-17T16:45:00Z",
          "end": "2022-11-17T17:50:00Z"
        }
      ]
      // 30 min in ms
      const gap = 1.8e6
      /*
      / Check if a user is busy 
      / @param {string} date in ms ex: "2022-11-17T17:11:00Z"
      / @param {boolean} if the customer is busy
      / @returns {boolean}
      */
      function checkBusy(clientDate = "2022-11-17T16:46:00Z", isBusy = false) {
        clientDate = Date.parse(clientDate)
        for (const bi of busyInfo) {
          const [start, end] = [Date.parse(bi.start), Date.parse(bi.end)]
          if (start >= clientDate && clientDate + gap <= end) {
            isBusy = true
          }
        }
        return isBusy
      }
      

      接下来的步骤是在前端控制您的用户的操作。

      文档:

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      相关资源
      最近更新 更多