大量遷移相同的程式碼從 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. 在雲端專案中設定 OAuth 同意畫面
  4. 在 Cloud 專案中啟用 Apps Script API

    開啟 Apps Script API

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

    1. 從 Apps Script 資訊主頁建立獨立的 Apps Script 專案,或前往 script.new
    2. 按一下「專案設定」專案設定圖示
    3. 在「Google Cloud 專案」部分,按一下「變更專案」
    4. 輸入 Cloud 專案的專案編號。
    5. 點選「設定專案」

遷移指令碼

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