大量遷移相同的程式碼從 Rhino 到 V8

本頁面說明如何使用 Apps Script 和 Apps Script API,將相同的腳本遷移至 V8。

您必須在 2026 年 1 月 31 日當天或之後,遷移所有使用 Rhino 執行階段的指令碼,否則 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. 在雲端專案中設定 OAuth 同意畫面
  4. 在 Cloud 專案中啟用 Apps Script API

    啟用 Apps Script API

  5. 建立 Apps Script 專案,並將 Apps Script 專案指派給 Cloud 專案。

    1. 您可以透過 Apps Script 資訊主頁或 script.new 建立獨立的 Apps Script 專案。
    2. 按一下「Project Settings」圖示 專案設定圖示
    3. 在「Google Cloud Platform (GCP) 專案」部分,按一下「變更專案」
    4. 輸入 Cloud 專案的專案編號。
    5. 按一下「設定專案」

遷移指令碼

以下程式碼範例說明如何使用 Apps Script API,將相同的指令碼從 Rhino 遷移至 V8,方法是將每個 Apps Script 專案中的檔案替換為一組與 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"

如要在編輯器中顯示資訊清單檔案,請按一下「Project Settings」專案設定圖示,然後勾選「Show "appsscript.json" manifest file in editor」方塊。以下是含有適當 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"
}