Advanced Drive Service

The advanced Drive service allows you to use the Google Drive web API in Apps Script. Much like Apps Script's built-in Drive service, this API allows scripts to create, find, and modify files and folders in Google Drive. In most cases, the built-in service is easier to use, but this advanced service provides a few extra features, including access to custom file properties as well as revisions for files and folders.

Reference

For detailed information on this service, see the reference documentation for the Google Drive web API. Like all advanced services in Apps Script, the advanced Drive service uses the same objects, methods, and parameters as the public API.

Sample code

The sample code below uses version 2 of the API.

Open code in new window

Uploading files

The following example demonstrates how to save a file to a user's Drive.

function uploadFile() {
  var image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
  var file = {
    title: 'google_logo.png',
    mimeType: 'image/png'
  };
  file = Drive.Files.insert(file, image);
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}

Listing folders

The following example demonstrates how to list the top-level folders in the user's Drive. Notice the use of page tokens to access the full list of results.

function listRootFolders() {
  var query = '"root" in parents and trashed = false and ' +
      'mimeType = "application/vnd.google-apps.folder"';
  var folders, pageToken;
  do {
    folders = Drive.Files.list({
      q: query,
      maxResults: 100,
      pageToken: pageToken
    });
    if (folders.items && folders.items.length > 0) {
      for (var i = 0; i < folders.items.length; i++) {
        var folder = folders.items[i];
        Logger.log('%s (ID: %s)', folder.title, folder.id);
      }
    } else {
      Logger.log('No folders found.');
    }
    pageToken = folders.nextPageToken;
  } while (pageToken);
}

Listing revisions

The following example demonstrates how to list the revisions for a given file. Note that some properties of revisions are only available for certain file types. For example, G Suite application files do not consume space in Google Drive and thus return a file size of 0.

function listRevisions(fileId) {
  var revisions = Drive.Revisions.list(fileId);
  if (revisions.items && revisions.items.length > 0) {
    for (var i = 0; i < revisions.items.length; i++) {
      var revision = revisions.items[i];
      var date = new Date(revision.modifiedDate);
      Logger.log('Date: %s, File size (bytes): %s', date.toLocaleString(),
          revision.fileSize);
    }
  } else {
    Logger.log('No revisions found.');
  }
}

Adding custom properties

The following example demonstrates how to add a custom property to a file. Unlike Apps Script's document properties, Drive's custom file properties can be accessed outside of Apps Script and by other applications (if the visibility is set to PUBLIC).

function addCustomProperty(fileId) {
  var property = {
    key: 'department',
    value: 'Sales',
    visibility: 'PUBLIC'
  };
  Drive.Properties.insert(property, fileId);
}

Send feedback about...

Apps Script
Apps Script