Memigrasikan skrip identik secara massal dari Rhino ke V8

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

Anda harus memigrasikan skrip apa pun yang menggunakan runtime Rhino sebelum Rhino dinonaktifkan, yang akan terjadi pada atau setelah 31 Januari 2026. Jika memiliki beberapa skrip yang identik dan berjalan di Rhino, Anda dapat memigrasikannya ke V8 sekaligus 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, aktifkan Apps Script API.

    Mengaktifkan 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.

Memigrasikan 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 kumpulan file yang kompatibel dengan V8.

Pastikan Anda memiliki setidaknya akses editor ke project skrip yang ingin Anda migrasikan.

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!`);
    }
  });
}

Untuk menggunakan Apps Script API dalam 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 mengekspos 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"
}