将相同的脚本从 Rhino 批量迁移到 V8

本页介绍了如何使用 Apps 脚本和 Apps 脚本 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 脚本项目,并将该 Apps 脚本项目分配给您的 Cloud 项目。

    1. 通过 Apps 脚本信息中心或访问 script.new 创建独立的 Apps 脚本项目。
    2. 点击项目设置 项目设置的图标
    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(在编辑器中显示“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"
}