Google Calendar API使用Apps脚本批量插入事件 [英] Google Calendar API batch insert events with Apps Script

查看:48
本文介绍了Google Calendar API使用Apps脚本批量插入事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Apps脚本的 UrlFetchApp ,如何使用Google Calendar v3 API在其中插入事件批次?

Using Google Apps Script's UrlFetchApp, how can I use the Google Calendar v3 API to insert events in batches?

Google列出了此示例批处理请求,但我没有不知道如何准确地转换它.

Google lists this example batch request, but I don't understand how exactly to convert it.

POST /batch/farm/v1 HTTP/1.1
Authorization: Bearer your_auth_token
Host: www.googleapis.com
Content-Type: multipart/mixed; boundary=batch_foobarbaz
Content-Length: total_content_length

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item1:12930812@barnyard.example.com>

GET /farm/v1/animals/pony

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item2:12930812@barnyard.example.com>

PUT /farm/v1/animals/sheep
Content-Type: application/json
Content-Length: part_content_length
If-Match: "etag/sheep"

{
  "animalName": "sheep",
  "animalAge": "5"
  "peltColor": "green",
}

--batch_foobarbaz
Content-Type: application/http
Content-ID: <item3:12930812@barnyard.example.com>

GET /farm/v1/animals
If-None-Match: "etag/animals"

--batch_foobarbaz--

推荐答案

我相信您的目标如下.

  • 您想使用带有Google Apps脚本的批处理请求将多个事件插入Google日历.

在这种情况下,在当前阶段,需要按照您的问题所示创建请求正文.可以在官方文档中查看示例请求正文.您的问题中已经提到了这一点.

In this case, in the current stage, it is required to create the request body as shown in your question. The sample request body can be seen at the official document. This has already been mentioned in your question.

创建请求主体并进行请求时,脚本将如下所示.

When the request body is created and request it, the script becomes as follows.

在使用此脚本之前,请在高级Google服务中启用Calendar API .

function myFunction() {
  const calendarId = "###";  // Please set the calendar ID.
  
  // Please set the object for inserting the calendar events. Each request body can be seen at https://developers.google.com/calendar/v3/reference/events/insert
  const events = [
    {start: {date: "2021-01-21"}, end: {date: "2021-01-21"},summary: "sample event 1"},
    {start: {date: "2021-01-22"}, end: {date: "2021-01-22"},summary: "sample event 2"}
  ];

  const boundary = "xxxxxxxxxx";
  const payload = events.reduce((s, e, i, a) => s += `Content-Type: application/http\r\n` +
    `Content-ID: ${i}\r\n\r\n` +
    `POST https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events\r\n` +
    `Content-Type: application/json; charset=utf-8\r\n\r\n` +
    `${JSON.stringify(e)}\r\n` +
    `--${boundary + (i == a.length - 1 ? "--" : "")}\r\n`
  , `--${boundary}\r\n`);
  const params = {
    method: "post",
    contentType: "multipart/mixed; boundary=" + boundary,
    payload: payload,
    headers: {Authorization: `Bearer ${ScriptApp.getOAuthToken()}`},
    muteHttpExceptions: true,
  };
  const res = UrlFetchApp.fetch("https://www.googleapis.com/batch/calendar/v3", params);
  console.log(res)

  // CalendarApp.getCalendarById();  // This is used for automatically detecting the scope of https://www.googleapis.com/auth/calendar
}

  • 在此示例脚本中,示例请求主体用于 events .因此,请根据您的实际情况对其进行修改.
    • In this sample script, the sample request body is used at events. So please modify it for your actual situation.
      • 在当前阶段,批处理请求可以通过一个API调用运行100个请求.因此,当您使用上述脚本请求100个以上的代码时,请对其进行修改.请注意这一点.

      • In the current stage, the batch request can run the 100 requests by one API call. So when you requests more than 100 using above script, please modify it. Please be careful this.

      在这种情况下,我认为创建请求正文时可能会有些复杂.因此,我将其创建为 Google Apps脚本库.使用该库时,上述脚本如下.在此库中,即使请求数超过100,也可以通过内部库中的处理来运行.

      In this case, I thought that when the request body is created, it might be a bit complicate. So I created this as a Google Apps Script library. When this library is used, above script becomes as follows. In this library, even when the number of requests is more than 100, this can be run by processing in the internal library.

        const calendarId = "###";  // Please set the calendar ID.
        const requests = {
          batchPath: "batch/calendar/v3",
          requests: [
            {
              method: "POST",
              endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`,
              requestBody: {start: {date: "2021-01-21"}, end: {date: "2021-01-21"},summary: "sample event 1"},
            },
            {
              method: "POST",
              endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`,
              requestBody: {start: {date: "2021-01-22"}, end: {date: "2021-01-22"},summary: "sample event 2"},
            }
          ]
        };
        const res = BatchRequest.EDo(requests);
        console.log(res);
      

      • 发送批量请求
      • 事件:插入
      • BatchRequest
        • 这是一个用于使用Google Apps脚本(GAS)运行批处理请求的库.

        这篇关于Google Calendar API使用Apps脚本批量插入事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆