บริการไดรฟ์ขั้นสูง

บริการขั้นสูงของไดรฟ์ช่วยให้คุณใช้ Google Drive API ใน Apps Script ได้ API นี้ช่วยให้สคริปต์สร้าง ค้นหา และ แก้ไขไฟล์และโฟลเดอร์ใน Google ไดรฟ์ได้ เช่นเดียวกับบริการไดรฟ์ ในตัวของ Apps Script ในกรณีส่วนใหญ่ บริการในตัวจะใช้งานง่ายกว่า แต่บริการขั้นสูงนี้มีฟีเจอร์เพิ่มเติมบางอย่าง เช่น การเข้าถึงพร็อพเพอร์ตี้ไฟล์ที่กำหนดเอง รวมถึงการแก้ไขไฟล์และโฟลเดอร์

ข้อมูลอ้างอิง

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

หากต้องการรายงานปัญหาและรับการสนับสนุนอื่นๆ โปรดดูคู่มือการสนับสนุน Drive API

โค้ดตัวอย่าง

ตัวอย่างโค้ดในส่วนนี้ใช้ API เวอร์ชัน 3

อัปโหลดไฟล์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีบันทึกไฟล์ลงในไดรฟ์ของผู้ใช้

advanced/drive.gs
/**
 * Uploads a new file to the user's Drive.
 */
function uploadFile() {
  try {
    // Makes a request to fetch a URL.
    const image = UrlFetchApp.fetch("http://goo.gl/nd7zjB").getBlob();
    let file = {
      name: "google_logo.png",
      mimeType: "image/png",
    };
    // Create a file in the user's Drive.
    file = Drive.Files.create(file, image, { fields: "id,size" });
    console.log("ID: %s, File size (bytes): %s", file.id, file.size);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log("Failed to upload file with error %s", err.message);
  }
}

แสดงรายการโฟลเดอร์

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

advanced/drive.gs
/**
 * Lists the top-level folders in the user's Drive.
 */
function listRootFolders() {
  const query =
    '"root" in parents and trashed = false and ' +
    'mimeType = "application/vnd.google-apps.folder"';
  let folders;
  let pageToken = null;
  do {
    try {
      folders = Drive.Files.list({
        q: query,
        pageSize: 100,
        pageToken: pageToken,
      });
      if (!folders.files || folders.files.length === 0) {
        console.log("All folders found.");
        return;
      }
      for (let i = 0; i < folders.files.length; i++) {
        const folder = folders.files[i];
        console.log("%s (ID: %s)", folder.name, folder.id);
      }
      pageToken = folders.nextPageToken;
    } catch (err) {
      // TODO (developer) - Handle exception
      console.log("Failed with error %s", err.message);
    }
  } while (pageToken);
}

แสดงรายการการแก้ไข

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

advanced/drive.gs
/**
 * Lists the revisions of a given file.
 * @param {string} fileId The ID of the file to list revisions for.
 */
function listRevisions(fileId) {
  let revisions;
  let pageToken = null;
  do {
    try {
      revisions = Drive.Revisions.list(fileId, {
        fields: "revisions(modifiedTime,size),nextPageToken",
      });
      if (!revisions.revisions || revisions.revisions.length === 0) {
        console.log("All revisions found.");
        return;
      }
      for (let i = 0; i < revisions.revisions.length; i++) {
        const revision = revisions.revisions[i];
        const date = new Date(revision.modifiedTime);
        console.log(
          "Date: %s, File size (bytes): %s",
          date.toLocaleString(),
          revision.size,
        );
      }
      pageToken = revisions.nextPageToken;
    } catch (err) {
      // TODO (developer) - Handle exception
      console.log("Failed with error %s", err.message);
    }
  } while (pageToken);
}

เพิ่มคุณสมบัติไฟล์

ตัวอย่างโค้ดต่อไปนี้ใช้ฟิลด์ appProperties เพื่อเพิ่มพร็อพเพอร์ตี้ที่กำหนดเองลงในไฟล์ มีเพียงสคริปต์เท่านั้นที่มองเห็นพร็อพเพอร์ตี้ที่กำหนดเอง หากต้องการเพิ่ม พร็อพเพอร์ตี้ที่กำหนดเองลงในไฟล์ซึ่งแอปอื่นๆ มองเห็นได้ด้วย ให้ใช้ฟิลด์ properties แทน ดูข้อมูลเพิ่มเติมได้ที่เพิ่มพร็อพเพอร์ตี้ไฟล์ที่กำหนดเอง

advanced/drive.gs
/**
 * Adds a custom app property to a file. Unlike Apps Script's DocumentProperties,
 * Drive's custom file properties can be accessed outside of Apps Script and
 * by other applications; however, appProperties are only visible to the script.
 * @param {string} fileId The ID of the file to add the app property to.
 */
function addAppProperty(fileId) {
  try {
    let file = {
      appProperties: {
        department: "Sales",
      },
    };
    // Updates a file to add an app property.
    file = Drive.Files.update(file, fileId, null, {
      fields: "id,appProperties",
    });
    console.log(
      "ID: %s, appProperties: %s",
      file.id,
      JSON.stringify(file.appProperties, null, 2),
    );
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log("Failed with error %s", err.message);
  }
}