Layanan Drive Lanjutan

Dengan layanan Drive lanjutan, Anda dapat menggunakan Google Drive API di Apps Script. Mirip dengan layanan Drive bawaan Apps Script, API ini memungkinkan skrip untuk membuat, menemukan, dan mengubah file dan 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 selengkapnya 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 seperti API publik. Untuk informasi selengkapnya, lihat Cara tanda tangan metode ditentukan.

Untuk melaporkan masalah dan menemukan dukungan lainnya, lihat Panduan dukungan Drive API.

Kode contoh

Contoh kode di bagian ini menggunakan API versi 3.

Upload file

Contoh kode 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 = {
      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);
  }
}

Cantumkan folder

Contoh kode berikut menunjukkan cara mencantumkan folder level 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,
        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);
}

Mencantumkan revisi

Contoh kode berikut menunjukkan cara mencantumkan revisi untuk file tertentu. Perhatikan bahwa beberapa file dapat memiliki beberapa revisi dan Anda harus menggunakan token halaman untuk mengakses daftar lengkap hasil.

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

Menambahkan properti file

Contoh kode berikut menggunakan kolom appProperties untuk menambahkan properti kustom ke file. Properti kustom hanya dapat dilihat oleh skrip. Untuk menambahkan properti kustom ke file yang juga terlihat oleh aplikasi lain, gunakan kolom properties. Untuk mengetahui informasi selengkapnya, lihat Menambahkan properti file kustom.

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