高级云端硬盘标签服务
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 Google 云端硬盘标签高级服务,您可以为云端硬盘文件和文件夹创建标签并管理这些标签。借助此高级服务,您可以在 Apps 脚本中使用 Drive Labels API 的所有功能。
如需应用或移除云端硬盘标签,请使用高级云端硬盘服务。
参考
如需详细了解此服务,请参阅 Google Drive Labels API 的文档。与 Apps 脚本中的所有高级服务一样,Drive Labels API 服务使用的对象、方法和参数均与公共 API 相同。
如需报告问题并查找其他支持资源,请参阅 Google Drive Labels API 支持指南。
示例代码
以下示例代码使用 API 的版本 2。
列出标签
以下代码示例展示了如何获取发出请求的用户可用的标签列表。
获取标签
以下代码示例展示了如何通过标签的资源名称(即标签的字符串值)获取单个标签。如需查找标签名称,请通过 API 获取标签列表,或使用 Google 云端硬盘标签管理器。如需详细了解标签管理器,请参阅管理云端硬盘标签。
列出云端硬盘内容的标签
以下代码示例展示了如何获取某个 Google 云端硬盘项目并列出应用于该项目的所有标签。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\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```"]]