ย้ายข้อมูลสคริปต์ที่เหมือนกันจำนวนมากจาก Rhino ไปยัง V8

หน้านี้อธิบายวิธีย้ายข้อมูลสคริปต์ที่เหมือนกันไปยัง V8 โดยใช้ Google Apps Script และ Apps Script API

โดยรันไทม์ Rhino ได้ปิดบริการในวันที่ 31 มกราคม 2026 หรือหลังจากนั้น ย้ายข้อมูล สคริปต์ที่ใช้รันไทม์ Rhino ก่อนวันที่ดังกล่าว หากมีสคริปต์ที่เหมือนกันหลายรายการที่ทำงานใน Rhino ให้ย้ายข้อมูลไปยัง V8 พร้อมกันทั้งหมดโดยใช้ Apps Script API

ตั้งค่าสภาพแวดล้อม

  1. เปิด Apps Script API จากการตั้งค่าแดชบอร์ดของ Apps Script
    1. ไปที่แดชบอร์ด Apps Script การตั้งค่า
    2. หากปิด API อยู่ ให้คลิก Apps Script API แล้วเปิดปุ่มสลับ Apps Script API
  2. สร้างโปรเจ็กต์ Google Cloud มาตรฐาน หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ
  3. กำหนดค่าหน้าจอขอความยินยอม OAuth ในโปรเจ็กต์ Cloud
  4. เปิดใช้ Apps Script API ในโปรเจ็กต์ Cloud

    เปิดใช้ Apps Script API

  5. สร้างโปรเจ็กต์ Apps Script แล้วกำหนดให้กับโปรเจ็กต์ Cloud

    1. สร้างโปรเจ็กต์ Apps Script แบบสแตนด์อโลนจากแดชบอร์ด Apps Script หรือไปที่ script.new
    2. คลิกการตั้งค่าโปรเจ็กต์ ไอคอนสำหรับการตั้งค่าโปรเจ็กต์
    3. ในส่วนโปรเจ็กต์ Google Cloud ให้คลิกเปลี่ยนโปรเจ็กต์
    4. ป้อนหมายเลขโปรเจ็กต์ของโปรเจ็กต์ที่อยู่ในระบบคลาวด์
    5. คลิกตั้งค่าโปรเจ็กต์

ย้ายข้อมูลสคริปต์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีใช้ Apps Script API เพื่อ ย้ายข้อมูลสคริปต์ที่เหมือนกันจาก Rhino ไปยัง V8 โดยการแทนที่ไฟล์ในแต่ละ โปรเจ็กต์ Apps Script ด้วยชุดไฟล์ที่เข้ากันได้กับ V8

เมื่อใช้เมธอด projects.UpdateContent ของ Apps Script API ให้รวมไฟล์ทั้งหมดในโปรเจ็กต์สคริปต์ แม้แต่ไฟล์ที่คุณไม่ต้องการเปลี่ยนแปลง หากไม่รวมไฟล์ ระบบจะลบไฟล์และกู้คืนไม่ได้

ตรวจสอบว่าคุณมีสิทธิ์เข้าถึงโปรเจ็กต์สคริปต์ที่คุณวางแผนจะ ย้ายข้อมูลในระดับผู้แก้ไขเป็นอย่างน้อย

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 API ในโปรเจ็กต์ Apps Script ให้เพิ่มขอบเขต OAuth ต่อไปนี้ลงในไฟล์ Manifest

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

หากต้องการแสดงไฟล์ Manifest ในเครื่องมือแก้ไข ให้คลิกการตั้งค่าโปรเจ็กต์ ไอคอนสำหรับการตั้งค่าโปรเจ็กต์ แล้วเลือกช่องแสดงไฟล์ Manifest "appsscript.json" ในเครื่องมือแก้ไข ต่อไปนี้คือตัวอย่างไฟล์ Manifest ที่มีขอบเขต 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"
}