Advanced Drive Labels Service

  • The Google Drive Labels advanced service in Apps Script allows you to create and manage labels for Drive files and folders.

  • This service uses the Drive Labels API, and you must turn on this advanced service before use.

  • You can use the Advanced Drive Service to apply or remove Drive labels.

  • The documentation for the Google Drive Labels API serves as a reference for this service.

  • Sample code is provided to demonstrate listing labels, getting a specific label, and listing labels applied to a Drive item.

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 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 Google 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 Google 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.
 */
functionlistLabels(){
letpageToken=null;
letlabels=[];
do{
try{
constresponse=DriveLabels.Labels.list({
publishedOnly:true,
pageToken:pageToken
});
pageToken=response.nextPageToken;
labels=labels.concat(response.labels);
}catch(err){
// TODO (developer) - Handle exception
console.log('Failed to list labels with error %s',err.message);
}
}while(pageToken!=null);
console.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.
 * @param {string} labelName The label name.
 */
functiongetLabel(labelName){
try{
constlabel=DriveLabels.Labels.get(labelName,{view:'LABEL_VIEW_FULL'});
consttitle=label.properties.title;
constfieldsLength=label.fields.length;
console.log(`Fetched label with title: '${title}' and ${fieldsLength} fields.`);
}catch(err){
// TODO (developer) - Handle exception
console.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.
 *
 * @param {string} fileId The Drive File ID
 */
functionlistLabelsOnDriveItem(fileId){
try{
constappliedLabels=Drive.Files.listLabels(fileId);
console.log('%d label(s) are applied to this file',appliedLabels.labels.length);
appliedLabels.labels.forEach((appliedLabel)=>{
// Resource name of the label at the applied revision.
constlabelName='labels/'+appliedLabel.id+'@'+appliedLabel.revisionId;
console.log('Fetching Label: %s',labelName);
constlabel=DriveLabels.Labels.get(labelName,{view:'LABEL_VIEW_FULL'});
console.log('Label Title: %s',label.properties.title);
Object.keys(appliedLabel.fields).forEach((fieldId)=>{
constfieldValue=appliedLabel.fields[fieldId];
constfield=label.fields.find((f)=>f.id==fieldId);
console.log(`Field ID: ${field.id}, Display Name: ${field.properties.displayName}`);
switch(fieldValue.valueType){
case'text':
console.log('Text: %s',fieldValue.text[0]);
break;
case'integer':
console.log('Integer: %d',fieldValue.integer[0]);
break;
case'dateString':
console.log('Date: %s',fieldValue.dateString[0]);
break;
case'user':
constuser=fieldValue.user.map((user)=>{
return`${user.emailAddress}: ${user.displayName}`;
}).join(', ');
console.log(`User: ${user}`);
break;
case'selection':
constchoices=fieldValue.selection.map((choiceId)=>{
returnfield.selectionOptions.choices.find((choice)=>choice.id===choiceId);
});
constselection=choices.map((choice)=>{
return`${choice.id}: ${choice.properties.displayName}`;
}).join(', ');
console.log(`Selection: ${selection}`);
break;
default:
console.log('Unknown: %s',fieldValue.valueType);
console.log(fieldValue.value);
}
});
});
}catch(err){
// TODO (developer) - Handle exception
console.log('Failed with error %s',err.message);
}
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月13日 UTC.