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

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

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

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

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

    เปิด Apps Script API

  5. สร้างโปรเจ็กต์ Apps Script และกำหนดโปรเจ็กต์ Apps Script ให้กับโปรเจ็กต์ที่อยู่ในระบบคลาวด์

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

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

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

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

Code.gsappsscript.json
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 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"
}