Erweiterter Drive-Label-Dienst
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Mit dem erweiterten Dienst „Google Drive-Labels“ können Sie Labels für Ihre Drive-Dateien und -Ordner erstellen und verwalten. Mit diesem erweiterten Dienst können Sie alle Funktionen der Drive Labels API in Apps Script verwenden.
Wenn Sie Drive-Labels anwenden oder entfernen möchten, verwenden Sie den Advanced Drive Service.
Referenz
Weitere Informationen zu diesem Dienst finden Sie in der Dokumentation zur Google Drive Labels API. Wie alle erweiterten Dienste in Apps Script verwendet der Dienst „Drive Labels API“ dieselben Objekte, Methoden und Parameter wie die öffentliche API.
Informationen zum Melden von Problemen und zum Finden von anderem Support finden Sie im Supportleitfaden zur Google Drive Labels API.
Beispielcode
Im folgenden Beispielcode wird Version 2 der API verwendet.
Labels auflisten
Das folgende Codebeispiel zeigt, wie Sie eine Liste der Labels abrufen, die für den Nutzer verfügbar sind, der die Anfrage stellt.
Label erhalten
Im folgenden Codebeispiel wird gezeigt, wie Sie ein einzelnes Label anhand seines Ressourcennamens (dem Stringwert des Labels) abrufen. Rufen Sie die Liste der Labels über die API ab oder verwenden Sie den Drive-Label-Manager, um den Labelnamen zu ermitteln. Weitere Informationen zum Label-Manager finden Sie unter Google Drive-Labels verwalten.
Labels für ein Drive-Element auflisten
Das folgende Codebeispiel zeigt, wie Sie ein Drive-Element abrufen und alle auf dieses Element angewendeten Labels auflisten.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
Zuletzt aktualisiert: 2025-08-31 (UTC).
[null,null,["Zuletzt aktualisiert: 2025-08-31 (UTC)."],[[["\u003cp\u003eUtilize the Google Drive Labels advanced service in Apps Script to create and manage labels for your Google Drive files and folders, leveraging the Drive Labels API.\u003c/p\u003e\n"],["\u003cp\u003eTo use this advanced service, ensure you enable the Advanced Drive Service in your Apps Script project settings before implementation.\u003c/p\u003e\n"],["\u003cp\u003eAccess comprehensive documentation and support resources for the Google Drive Labels API, which uses the same structure as the public API, in the provided references.\u003c/p\u003e\n"],["\u003cp\u003eExplore the provided sample code snippets to learn how to list available labels, retrieve specific labels by name, and list labels applied to Drive items using Apps Script.\u003c/p\u003e\n"]]],[],null,["# Advanced Drive Labels Service\n\nCreate and manage labels for your Drive files and folders with the Google Drive\nLabels advanced service. With this advanced service, you can use all the\nfeatures of the\n[Drive Labels API](/drive/labels/guides/overview)\nin Apps Script.\n\nTo apply or remove Drive labels, use the\n[Advanced Drive Service](/apps-script/advanced/drive).\n| **Note:** This is an advanced service that you must [turn on before use](/apps-script/guides/services/advanced).\n\nReference\n---------\n\nFor more information about this service, see the documentation for the\n[Google Drive Labels API](/drive/labels). Like all advanced\nservices in Apps Script, the Drive Labels API service uses the same objects,\nmethods, and parameters as the public API.\n\nTo report issues and find other support, see the Google Drive Labels API\n[support guide](/drive/labels/support).\n\nSample code\n-----------\n\nThe sample code below uses\n[version 2](/drive/labels/reference/rest) of the\nAPI.\n\n### List labels\n\nThe following code sample shows how to get a list of labels available to the\nuser making the request. \nadvanced/driveLabels.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/driveLabels.gs) \n\n```javascript\n/**\n * List labels available to the user.\n */\nfunction listLabels() {\n let pageToken = null;\n let labels = [];\n do {\n try {\n const response = DriveLabels.Labels.list({\n publishedOnly: true,\n pageToken: pageToken\n });\n pageToken = response.nextPageToken;\n labels = labels.concat(response.labels);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to list labels with error %s', err.message);\n }\n } while (pageToken != null);\n\n console.log('Found %d labels', labels.length);\n}\n```\n\n### Get a label\n\nThe following code sample shows how to get a single label by its\n[resource name](/drive/labels/reference/rest/v2/labels/get)\n(which is the string value of the label). To find the label name, get the list\nof labels through the API or use the Drive labels manager. For more information\non the labels manager, go to\n[Manage Drive labels](https://support.google.com/a/answer/9292382). \nadvanced/driveLabels.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/driveLabels.gs) \n\n```javascript\n/**\n * Get a label by name.\n * @param {string} labelName The label name.\n */\nfunction getLabel(labelName) {\n try {\n const label = DriveLabels.Labels.get(labelName, {view: 'LABEL_VIEW_FULL'});\n const title = label.properties.title;\n const fieldsLength = label.fields.length;\n console.log(`Fetched label with title: '${title}' and ${fieldsLength} fields.`);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to get label with error %s', err.message);\n }\n}\n```\n\n### List labels for a Drive item\n\nThe following code sample shows how to get a Drive item and list all labels\napplied to that item. \nadvanced/driveLabels.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/driveLabels.gs) \n\n```javascript\n/**\n * List Labels on a Drive Item\n * Fetches a Drive Item and prints all applied values along with their to their\n * human-readable names.\n *\n * @param {string} fileId The Drive File ID\n */\nfunction listLabelsOnDriveItem(fileId) {\n try {\n const appliedLabels = Drive.Files.listLabels(fileId);\n\n console.log('%d label(s) are applied to this file', appliedLabels.labels.length);\n\n appliedLabels.labels.forEach((appliedLabel) =\u003e {\n // Resource name of the label at the applied revision.\n const labelName = 'labels/' + appliedLabel.id + '@' + appliedLabel.revisionId;\n\n console.log('Fetching Label: %s', labelName);\n const label = DriveLabels.Labels.get(labelName, {view: 'LABEL_VIEW_FULL'});\n\n console.log('Label Title: %s', label.properties.title);\n\n Object.keys(appliedLabel.fields).forEach((fieldId) =\u003e {\n const fieldValue = appliedLabel.fields[fieldId];\n const field = label.fields.find((f) =\u003e f.id == fieldId);\n\n console.log(`Field ID: ${field.id}, Display Name: ${field.properties.displayName}`);\n switch (fieldValue.valueType) {\n case 'text':\n console.log('Text: %s', fieldValue.text[0]);\n break;\n case 'integer':\n console.log('Integer: %d', fieldValue.integer[0]);\n break;\n case 'dateString':\n console.log('Date: %s', fieldValue.dateString[0]);\n break;\n case 'user':\n const user = fieldValue.user.map((user) =\u003e {\n return `${user.emailAddress}: ${user.displayName}`;\n }).join(', ');\n console.log(`User: ${user}`);\n break;\n case 'selection':\n const choices = fieldValue.selection.map((choiceId) =\u003e {\n return field.selectionOptions.choices.find((choice) =\u003e choice.id === choiceId);\n });\n const selection = choices.map((choice) =\u003e {\n return `${choice.id}: ${choice.properties.displayName}`;\n }).join(', ');\n console.log(`Selection: ${selection}`);\n break;\n default:\n console.log('Unknown: %s', fieldValue.valueType);\n console.log(fieldValue.value);\n }\n });\n });\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed with error %s', err.message);\n }\n}\n```"]]