Memigrasikan skrip identik secara massal dari Rhino ke V8

Halaman ini menjelaskan cara memigrasikan skrip yang identik ke V8 menggunakan Apps Script dan Apps Script API.

Anda harus memigrasikan semua skrip yang menggunakan runtime Rhino sebelum Rhino ditolak, yang akan dilakukan mulai 31 Januari 2026. Jika Anda memiliki beberapa skrip identik yang berjalan di Rhino, Anda dapat memigrasikannya ke V8 secara bersamaan menggunakan Apps Script API.

Menyiapkan lingkungan Anda

  1. Dari setelan dasbor Apps Script, aktifkan Apps Script API.
    1. Buka setelan dasbor Apps Script.
    2. Jika API dinonaktifkan, klik Google Apps Script API, lalu aktifkan tombol Google Apps Script API.
  2. Buat project Google Cloud standar atau gunakan kembali project yang sudah ada.
  3. Di project Cloud Anda, konfigurasi layar izin OAuth.
  4. Di project Cloud Anda, aktifkan Apps Script API.

    Aktifkan Apps Script API

  5. Buat project Apps Script dan tetapkan project Apps Script ke project Cloud Anda.

    1. Buat project Apps Script mandiri dari dasbor Apps Script atau dengan membuka script.new.
    2. Klik Setelan Project Ikon untuk setelan project.
    3. Di bagian Google Cloud Platform (GCP) Project, klik Change project.
    4. Masukkan nomor project project Cloud Anda.
    5. Klik Set project.

Migrasikan skrip

Contoh kode berikut menunjukkan cara menggunakan Apps Script API untuk memigrasikan skrip yang identik dari Rhino ke V8 dengan mengganti file di setiap project Apps Script dengan sekumpulan file yang kompatibel dengan V8.

Pastikan Anda memiliki akses editor ke project skrip yang ingin dimigrasikan.

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

Untuk menggunakan Apps Script API di project Apps Script, Anda harus menambahkan cakupan OAuth berikut ke file manifes:

  • "https://www.googleapis.com/auth/script.projects"
  • "https://www.googleapis.com/auth/script.external_request"

Untuk menampilkan file manifes di editor, klik Project Settings Ikon untuk setelan project dan centang kotak Show "appsscript.json" manifest file in editor. Berikut adalah contoh file manifes dengan cakupan OAuth yang sesuai:

{
  "timeZone": "America/Denver",
  "dependencies": {
  },
  "oauthScopes": [
  "https://www.googleapis.com/auth/script.projects",
  "https://www.googleapis.com/auth/script.external_request"
],
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}