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

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

Rhino ランタイムを使用しているスクリプトは、Rhino のサポート終了(2026 年 1 月 31 日以降)までに移行する必要があります。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 に移行する方法を示しています。

移行するスクリプト プロジェクトに対して、少なくとも編集者権限があることを確認します。

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!`);
    }
  });
}

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"
}