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

このページでは、Google 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 が無効になっている場合は、[Apps Script API] をクリックし、 [Apps Script API] トグルをオンにします。
  2. 標準の Google Cloud プロジェクトを作成する か、既存のプロジェクトを再利用します。
  3. Cloud プロジェクトで、OAuth 同意 画面を構成します。
  4. Cloud プロジェクトで、Apps Script API を有効にします

    Apps Script API を有効にする

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

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

スクリプトを移行する

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

Apps Script API の projects.UpdateContent メソッドを使用する場合は、変更しないファイルも含め、スクリプト プロジェクト内のすべてのファイルを含めます。ファイルを含めないと、ファイルは削除され、復元できなくなります。

移行するスクリプト プロジェクトに対する編集者のアクセス権があることを確認してください。

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