Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions advanced/driveLabels.gs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function listLabels() {
} while (pageToken != null);

Logger.log('Found %d labels', labels.length);
console.log(labels[0].name)
}
// [END apps_script_drive_labels_list_labels]

Expand All @@ -50,4 +49,61 @@ function getLabel(labelName) {
Logger.log('Failed to get label with error %s', err.message);
}
}
// [END apps_script_drive_labels_get_label]
// [END apps_script_drive_labels_get_label]

// [START apps_script_drive_labels_list_labels_on_drive_item]
/*
* 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);
}
}
// [END apps_script_drive_labels_list_labels_on_drive_item]