Dịch vụ Drive nâng cao

Dịch vụ Drive nâng cao cho phép bạn sử dụng API Google Drive trong Apps Script. Tương tự như Drive tích hợp sẵn của Apps Script API này, API này cho phép các tập lệnh tạo, tìm cũng như sửa đổi tệp và thư mục trong Google Drive. Trong hầu hết các trường hợp, hàm tích hợp sẵn dịch vụ này dễ sử dụng hơn nhưng dịch vụ nâng cao này cung cấp thêm các tính năng này, bao gồm quyền truy cập vào thuộc tính tệp tuỳ chỉnh cũng như các bản sửa đổi tệp và thư mục.

Tài liệu tham khảo

Để biết thông tin chi tiết về dịch vụ này, hãy xem tài liệu tham khảo tài liệu dành cho API Google Drive. Thích tất cả các dịch vụ nâng cao trong Apps Script, Dịch vụ Drive sử dụng các đối tượng, phương thức và tham số giống với API công khai. Để biết thêm thông tin, hãy xem bài viết Cách chữ ký phương thức xác định.

Để báo cáo sự cố và tìm sự hỗ trợ khác, hãy xem phần hỗ trợ API Drive hướng dẫn.

Mã mẫu

Các mã mẫu trong phần này sử dụng phiên bản 3 API.

Tải tệp lên

Mã mẫu sau đây cho biết cách lưu một tệp vào Drive.

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

Liệt kê thư mục

Mã mẫu sau đây cho biết cách liệt kê các thư mục cấp cao nhất trong Drive. Lưu ý việc sử dụng mã thông báo trang để truy cập vào danh sách đầy đủ kết quả.

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

Liệt kê bản sửa đổi

Mã mẫu sau đây cho biết cách liệt kê các bản sửa đổi cho một tệp nhất định. Ghi chú một số tệp có thể có một vài bản sửa đổi và bạn nên sử dụng mã thông báo trang để để xem danh sách kết quả đầy đủ.

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

Thêm thuộc tính tệp

Mã mẫu sau đây sử dụng trường appProperties để thêm một thuộc tính tuỳ chỉnh vào một tệp. Thuộc tính tuỳ chỉnh chỉ hiển thị với tập lệnh. Để thêm một thuộc tính tùy chỉnh vào tệp cũng hiển thị với các ứng dụng khác, hãy sử dụng Thay vào đó, trường properties. Để biết thêm thông tin, hãy xem bài viết Thêm tệp tuỳ chỉnh thuộc tính.

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