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

BatchRequest

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

AI Summary

BatchRequest

A Google Apps Script library that simplifies executing Google API batch requests.

Target Users

Intermediate-to-advanced Apps Script developers building automation tools who need to optimise large numbers of Google API calls.

Problems Solved

Calling Google APIs one-by-one wastes quota and manually building multipart/mixed batch requests with UrlFetchApp is cumbersome.

Tags

Main Features

1
Run up to 100 requests with a single method

The Do() method hides multipart/mixed complexity and submits up to 100 requests in one batch.

2
Automatic splitting & parsing for >100 requests

EDo() splits large request sets, optionally uses fetchAll for parallel calls, and returns parsed results.

3
Automatic batch path retrieval

getBatchPath() fetches the current batch path (e.g., batch/drive/v3) from Discovery API with just the API name.

4
Flexible access-token handling

Supports per-request or global access tokens, including those obtained via service accounts.

5
Automatic scope installation

Required scopes (Drive, Calendar, Gmail, etc.) are injected into the manifest automatically when the library is added.

Usage Examples

Rename Drive files in one batch

The snippet below shows the minimal code to rename two files with a single Drive API v3 batch request.

function renameFilesWithBatch() {
  const req = {
    batchPath: BatchRequest.getBatchPath("drive"), // Fetch latest batch path
    requests: [
      {
        method: "PATCH",
        endpoint: "https://www.googleapis.com/drive/v3/files/FILE_ID_1?fields=name",
        requestBody: { name: "sample1" }, // New file name
      },
      {
        method: "PATCH",
        endpoint: "https://www.googleapis.com/drive/v3/files/FILE_ID_2?fields=name",
        requestBody: { name: "sample2" },
      },
    ],
  };
  const res = BatchRequest.Do(req); // Execute batch
  Logger.log(res); // Raw HTTP response
}

Explanation: BatchRequest.getBatchPath("drive") obtains the correct batch/drive/v3 path, and Do() sends both PATCH requests in a single quota-consuming call.