同じスクリプトを Rhino から V8 に一括で移行する

このページでは、Apps Script と Apps Script API を使用して、同一のスクリプトを V8 に移行する方法について説明します。

Rhino ランタイムを使用するスクリプトは、2026 年 1 月 31 日以降に Rhino が利用できなくなる前に移行する必要があります。Rhino で実行されている同じスクリプトが複数ある場合は、Apps Script API を使用して、それらをまとめて V8 に移行できます。

環境の設定

  1. Apps Script ダッシュボードの設定で、Apps Script API を有効にします。
    1. Apps Script ダッシュボードの設定に移動します。
    2. API がオフになっている場合は、[Google Apps Script API] をクリックし、Google Apps Script API の切り替えをオンにします。
  2. 標準の Google Cloud プロジェクトを作成するか、既存のプロジェクトを再利用します。
  3. Cloud プロジェクトで、OAuth 同意画面を構成します。
  4. Cloud プロジェクトで Apps Script API を有効にします

    Apps Script API を有効にする

  5. Apps Script プロジェクトを作成し、Apps Script プロジェクトを Cloud プロジェクトに割り当てます。

    1. Apps Script ダッシュボードから、または script.new に移動して、スタンドアロンの Apps Script プロジェクトを作成します。
    2. [プロジェクトの設定] プロジェクト設定のアイコン をクリックします。
    3. [Google Cloud Platform(GCP)プロジェクト] セクションで、[プロジェクトを変更] をクリックします。
    4. Cloud プロジェクトのプロジェクト番号を入力します。
    5. [プロジェクトを設定] をクリックします。

スクリプトを移行する

次のコードサンプルは、Apps Script API を使用して、各 Apps Script プロジェクトのファイルを V8 互換のファイルセットに置き換えることで、同一のスクリプトを Rhino から V8 に移行する方法を示しています。

移行する予定のスクリプト プロジェクトに対する編集者以上のアクセス権があることを確認します。

コード.gs

function updateRhinoScripts() {
  // An array of script IDs of script projects to migrate.
  // TODO(developer): Replace with your script IDs.
  const scriptIds = ['abcdef12345678', 'abcdef12345678'];
  // An array of file objects to replace the existing files in each script project.
  // Remember to include all files for the script, excluded files are deleted.
  // TODO(developer): Replace with your script files.
  const filesToUpdate = {
    "files": [
      {
        "name": "Code",
        "type": "SERVER_JS",
        "source": "// New updates\nfunction myFunction() {\n  console.log('Hello, world!');\n}"
      },
      {
        "name": "appsscript",
        "type": "JSON",
        "source": JSON.stringify({
          "timeZone": "America/New_York",
          "dependencies": {},
          "exceptionLogging": "STACKDRIVER",
          "runtimeVersion": "V8"
        })
      }
    ]
  };
  updateMultipleAppsScripts(scriptIds, filesToUpdate);
}

function updateMultipleAppsScripts(scriptIds, filesToUpdate) {
  // 'scriptIds' should be an array of script IDs
  // 'filesToUpdate' should be an array of objects, each with:
  // name: The filename (For example, "Code", "Utilities")
  // source: The source code for that file.
  scriptIds.forEach(function (scriptId) {
    // Makes the API request.
    const response = UrlFetchApp.fetch(
      `https://script.googleapis.com/v1/projects/${scriptId}/content`,
      {
        method: "PUT",
        headers: {
          Authorization: `Bearer ${ScriptApp.getOAuthToken()}`
        },
        contentType: "application/json",
        payload: JSON.stringify(filesToUpdate),
        muteHttpExceptions: true
      }
    );
    if (response.getResponseCode() !== 200) {
      console.log(`Error updating script ${scriptId}: ${response.getContentText()}`);
    } else {
      console.log(`Script ${scriptId} updated successfully!`);
    }
  });
}

appsscript.json

Apps Script プロジェクトで Apps Script API を使用するには、マニフェスト ファイルに次の OAuth スコープを追加する必要があります。

  • "https://www.googleapis.com/auth/script.projects"
  • "https://www.googleapis.com/auth/script.external_request"

マニフェスト ファイルをエディタに表示するには、[プロジェクトの設定] プロジェクト設定のアイコン [「appsscript.json」マニフェスト ファイルをエディタで表示する] チェックボックスをオンにします。適切な OAuth スコープを含むマニフェスト ファイルの例を次に示します。

{
  "timeZone": "America/Denver",
  "dependencies": {
  },
  "oauthScopes": [
  "https://www.googleapis.com/auth/script.projects",
  "https://www.googleapis.com/auth/script.external_request"
],
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}