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

เพื่อจัดการไฟล์และโฟลเดอร์ ซึ่งรวมถึงพร็อพเพอร์ตี้ที่กำหนดเองและเวอร์ชันก่อนหน้า

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

นี่เป็นบริการขั้นสูงที่ต้องเปิดใช้ก่อน ใช้งาน

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

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

หากต้องการรายงานปัญหาและดูการสนับสนุนอื่นๆ โปรดดูคู่มือการสนับสนุน 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);
  }
}

สร้างโฟลเดอร์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างโฟลเดอร์ในไดรฟ์

/**
 * Creates a new folder.
 */
function createFolder() {
  var folderMetadata = {
    'name': 'New Folder',
    'mimeType': 'application/vnd.google-apps.folder'
  };
  var folder = Drive.Files.create(folderMetadata);
  Logger.log('Folder ID: ' + folder.id);
}

ค้นหาไฟล์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีค้นหาไฟล์โดยใช้สตริงคำค้นหา

/**
 * Searches for files with a specific name.
 */
function searchFiles() {
  var query = 'name contains "Project Plan" and trashed = false';
  var files = Drive.Files.list({
    'q': query,
    'fields': 'files(id, name, mimeType)'
  });
  if (files.files && files.files.length > 0) {
    for (var i = 0; i < files.files.length; i++) {
      var file = files.files[i];
      Logger.log('%s (ID: %s)', file.name, file.id);
    }
  } else {
    Logger.log('No files found.');
  }
}

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

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

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

เพิ่มผู้ใช้ลงในไฟล์

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

/**
 * Adds a user to a file as an editor without sending an email notification.
 */
function addEditor() {
  var fileId = '1234567890abcdefghijklmnopqrstuvwxyz';
  var userEmail = 'bob@example.com';
  var request = {
    'role': 'writer',
    'type': 'user',
    'emailAddress': userEmail
  };
  Drive.Permissions.create(request, fileId, {
    'sendNotificationEmail': false
  });
}