Advanced Drive Labels Service

Create and manage labels for your Drive files and folders with the Google Drive Labels advanced service. With this advanced service, you can use all the features of the Drive Labels web API in Apps Script.

To apply or remove Drive labels, use the Advanced Drive Service.

Reference

For more information about this service, see the documentation for the Drive Labels API. Like all advanced services in Apps Script, the Drive Labels API service uses the same objects, methods, and parameters as the public API.

To report issues and find other support, see the Drive Labels API support guide.

Sample code

The sample code below uses version 2 of the API.

List labels

The following code sample shows how to get a list of labels available to the user making the request.

advanced/driveLabels.gs
/**
 * List labels available to the user.
 */
function listLabels() {
  let pageToken = null;
  let labels = [];
  do {
    try {
      const response = DriveLabels.Labels.list({publishedOnly: true, pageToken: pageToken})
      pageToken = response.nextPageToken;
      labels = labels.concat(response.labels);
    } catch (err) {
      // TODO (developer) - Handle exception
      Logger.log('Failed to list labels with error %s', err.message);
    }
  } while (pageToken != null);

  Logger.log('Found %d labels', labels.length);
}

Get a label

The following code sample shows how to get a single label by its resource name (which is the string value of the label). To find the label name, get the list of labels through the API or use the Drive labels manager. For more information on the labels manager, go to Manage Drive labels.

advanced/driveLabels.gs
/**
 * Get a label by name.
 */
function getLabel(labelName) {
  try {
    const label = DriveLabels.Labels.get(labelName, {view: "LABEL_VIEW_FULL"});
    Logger.log("Fetched label with title: '%s' and %d fields.", label.properties.title, label.fields.length);
  } catch (err) {
    // TODO (developer) - Handle exception
    Logger.log('Failed to get label with error %s', err.message);
  }
}

List labels for a Drive item

The following code sample shows how to get a Drive item and list all labels applied to that item.

advanced/driveLabels.gs
/*
 * List Labels on a Drive Item
 * Fetches a Drive Item and prints all applied values along with their to their human-readable names.
 * 
 * - fileId: Drive File ID
 */
function listLabelsOnDriveItem(fileId) {
  try {
    const appliedLabels = Drive.Files.listLabels(fileId);

    Logger.log("%d label(s) are applied to this file", appliedLabels.items.length);

    appliedLabels.items.forEach((appliedLabel) => {
      // Resource name of the label at the applied revision.
      const labelName = "labels/" + appliedLabel.id + "@" + appliedLabel.revisionId;

      Logger.log("Fetching Label: %s", labelName);
      const label = DriveLabels.Labels.get(labelName, {view: "LABEL_VIEW_FULL"});

      Logger.log("Label Title: %s", label.properties.title);

      Object.keys(appliedLabel.fields).forEach(fieldId => {
        const fieldValue = appliedLabel.fields[fieldId];
        const field = label.fields.find((f) => f.id == fieldId);

        Logger.log("Field ID: %s, Display Name: %s", field.id, field.properties.displayName);
        switch(fieldValue.valueType) {
          case 'text':
            Logger.log("Text: %s", fieldValue.text[0]);
            break;
          case 'integer':
            Logger.log("Integer: %d", fieldValue.integer[0]);
            break;
          case 'dateString':
            Logger.log("Date: %s", fieldValue.dateString[0]);
            break;
          case 'user':
            Logger.log("User: %s", fieldValue.user.map((user) => user.emailAddress + ": " + user.displayName).join(", "));
            break;
          case 'selection':
          const choices = fieldValue.selection.map((choiceId) => field.selectionOptions.choices.find((choice) => choice.id == choiceId));
          Logger.log("Selection: %s", choices.map((choice) => choice.id + ": " + choice.properties.displayName).join(", "));
            break;
          default:
            Logger.log("Unknown: %s", fieldValue.valueType);
            Logger.log(fieldValue.value);
        }
      });
    });
  } catch (err) {
      // TODO (developer) - Handle exception
    Logger.log('Failed with error %s', err.message);   
  }
}