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

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

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

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

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

    เปิดใช้ Apps Script API

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

    1. สร้างโปรเจ็กต์ Apps Script แบบสแตนด์อโลนจากแดชบอร์ด Apps Script หรือไปที่ script.new
    2. คลิกการตั้งค่าโปรเจ็กต์ ไอคอนสำหรับการตั้งค่าโปรเจ็กต์
    3. ในส่วนโปรเจ็กต์ Google Cloud Platform (GCP) ให้คลิกเปลี่ยนโปรเจ็กต์
    4. ป้อนหมายเลขโปรเจ็กต์ของโปรเจ็กต์ที่อยู่ในระบบคลาวด์
    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 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"
}