大量遷移相同的程式碼從 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 專案,並將該專案指派給 Cloud 專案。

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

遷移指令碼

下列程式碼範例說明如何使用 Apps Script API,將 Rhino 中的相同指令碼遷移至 V8,方法是將每個 Apps Script 專案中的檔案,替換為一組與 V8 相容的檔案。

請確認您至少擁有要遷移的指令碼專案編輯者存取權。

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