メインコンテンツにスキップ

BatchRequest

This is a library for running Batch Requests using Google Apps Script (GAS).

AI Summary

BatchRequest

GAS library for executing batch requests to Google APIs.

Target Users

Intermediate GAS developers seeking to save API quotas with bulk Drive v3, Calendar, Gmail API calls.

Problems Solved

GAS Google API v3 calls use 1 quota each. Batch enables multiple per quota but multipart/mixed via UrlFetchApp is complex.

Script ID

  1. In GAS Editor: Click "Libraries +" → Paste into "Script ID" field → Click "Look up"
  2. "BatchRequest" will appear in the search results
  3. Select the latest version (highest number) from "Version" dropdown
  4. Click "Add"

Tags

Main Features
1
Quota Saving

Run up to 100+ API calls per batch to reduce quota usage significantly.

2
Easy Setup

Pass requests object for automatic multipart/mixed batch request handling.

3
Parsed Results

EDo parses batch results into array objects. Supports Blob export option.

Examples

Main Functions

FunctionDescription
BatchRequest.DoSimple batch up to 100 calls.
BatchRequest.EDoEnhanced batch with parsing, >100.
BatchRequest.getBatchPathGet batch path by API name.

Examples

Get Batch Path
/**
 * バッチパス取得
 * ・API名指定: BatchRequest.getBatchPath("drive")
 * ・バージョン指定: BatchRequest.getBatchPath("drive", "v2")
 * ・結果ログ出力: Logger.log(res)
 */
function getBatchPathExample() {
  var res = BatchRequest.getBatchPath("drive");
  Logger.log(res); // batch/drive/v3
}

Rename Files with Do
/**
 * ファイル名変更(Do)
 * ・リクエスト配列: requests: [{method, endpoint, requestBody}, ...]
 * ・バッチパス設定: batchPath: "batch/drive/v3"
 * ・ライブラリ呼び出し: BatchRequest.Do(requests)
 */
function renameFilesDo() {
  var requests = {
    batchPath: "batch/drive/v3",
    requests: [
      {
        method: "PATCH",
        endpoint: "https://www.googleapis.com/drive/v3/files/### file ID1 ###?fields=name",
        requestBody: { name: "sample1" },
        accessToken: ScriptApp.getOAuthToken()
      },
      {
        method: "PATCH",
        endpoint: "https://www.googleapis.com/drive/v3/files/### file ID2 ###?fields=name",
        requestBody: { name: "sample2" }
      }
    ]
  };
  var result = BatchRequest.Do(requests);
  Logger.log(result);
}

Rename Files with EDo
/**
 * ファイル名変更(EDo)
 * ・リクエスト定義: {method: "PATCH", endpoint: "...", requestBody: {...}}
 * ・バッチパス指定: batchPath: "batch/drive/v3"
 * ・解析結果取得: BatchRequest.EDo(requests)
 */
function renameFilesEDo() {
  var requests = {
    batchPath: "batch/drive/v3",
    requests: [
      {
        method: "PATCH",
        endpoint: "https://www.googleapis.com/drive/v3/files/### file ID1 ###?fields=name",
        requestBody: { name: "sample1" },
        accessToken: ScriptApp.getOAuthToken()
      },
      {
        method: "PATCH",
        endpoint: "https://www.googleapis.com/drive/v3/files/### file ID2 ###?fields=name",
        requestBody: { name: "sample2" }
      }
    ]
  };
  var result = BatchRequest.EDo(requests);
  Logger.log(result);
}

Sample Code

Post a Sample

No sample codes for this library yet

Be the first to post a sample!

Post a Sample