Layanan Drive Lanjutan

Layanan Drive lanjutan memungkinkan Anda menggunakan Google Drive API di Apps Script. Mirip dengan layanan Drive bawaan Apps Script, API ini memungkinkan skrip untuk membuat, menemukan, dan mengubah file serta folder di Google Drive. Pada umumnya, layanan bawaan lebih mudah digunakan, tetapi layanan lanjutan ini menyediakan beberapa fitur tambahan, termasuk akses ke properti file kustom serta revisi untuk file dan folder.

Referensi

Untuk informasi mendetail tentang layanan ini, lihat dokumentasi referensi untuk Google Drive API. Seperti semua layanan lanjutan di Apps Script, layanan Drive lanjutan menggunakan objek, metode, dan parameter yang sama dengan API publik. Untuk mengetahui informasi selengkapnya, lihat Cara menentukan tanda tangan metode.

Untuk melaporkan masalah dan menemukan dukungan lain, lihat Panduan dukungan Drive v2.

Kode contoh

Kode contoh di bawah menggunakan API versi 2.

Mengupload file

Contoh berikut menunjukkan cara menyimpan file ke Drive pengguna.

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 = {
      title: 'google_logo.png',
      mimeType: 'image/png'
    };
    // Insert new files to user's Drive
    file = Drive.Files.insert(file, image);
    console.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed to upload file with error %s', err.message);
  }
}

Mencantumkan folder

Contoh berikut menunjukkan cara menampilkan daftar folder tingkat teratas di Drive pengguna. Perhatikan penggunaan token halaman untuk mengakses daftar lengkap hasil.

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,
        maxResults: 100,
        pageToken: pageToken
      });
      if (!folders.items || folders.items.length === 0) {
        console.log('No folders found.');
        return;
      }
      for (let i = 0; i < folders.items.length; i++) {
        const folder = folders.items[i];
        console.log('%s (ID: %s)', folder.title, folder.id);
      }
      pageToken = folders.nextPageToken;
    } catch (err) {
      // TODO (developer) - Handle exception
      console.log('Failed with error %s', err.message);
    }
  } while (pageToken);
}

Mencantumkan revisi

Contoh berikut menunjukkan cara mencantumkan revisi untuk file tertentu. Perlu diperhatikan bahwa beberapa properti revisi hanya tersedia untuk jenis file tertentu. Misalnya, Google Workspace file aplikasi berisi link untuk mengekspor file ke format yang berbeda.

advanced/drive.gs
/**
 * Lists the revisions of a given file. Note that some properties of revisions
 * are only available for certain file types. For example, Google Workspace
 * application files do not consume space in Google Drive and thus list a file
 * size of 0.
 * @param {string} fileId The ID of the file to list revisions for.
 */
function listRevisions(fileId) {
  try {
    const revisions = Drive.Revisions.list(fileId);
    if (!revisions.items || revisions.items.length === 0) {
      console.log('No revisions found.');
      return;
    }
    for (let i = 0; i < revisions.items.length; i++) {
      const revision = revisions.items[i];
      const date = new Date(revision.modifiedDate);
      console.log('Date: %s, File size (bytes): %s', date.toLocaleString(),
          revision.fileSize);
    }
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', err.message);
  }
}

Menambahkan properti khusus

Contoh berikut menunjukkan cara menambahkan properti kustom ke file. Tidak seperti properti dokumen Apps Script, properti file kustom Drive dapat diakses di luar Apps Script dan oleh aplikasi lain (jika visibilitas ditetapkan ke PUBLIC).

advanced/drive.gs
/**
 * Adds a custom 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 (if the visibility is set to PUBLIC).
 * @param {string} fileId The ID of the file to add the property to.
 */
function addCustomProperty(fileId) {
  try {
    const property = {
      key: 'department',
      value: 'Sales',
      visibility: 'PUBLIC'
    };
    // Adds a property to a file
    Drive.Properties.insert(property, fileId);
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', err.message);
  }
}