Dịch vụ nhãn nâng cao cho Drive
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tạo và quản lý nhãn cho tệp và thư mục trên Drive bằng dịch vụ nâng cao Nhãn của Google Drive. Với dịch vụ nâng cao này, bạn có thể sử dụng tất cả các tính năng của Drive Labels API trong Apps Script.
Để áp dụng hoặc xoá nhãn Drive, hãy sử dụng Dịch vụ Drive nâng cao.
Tài liệu tham khảo
Để biết thêm thông tin về dịch vụ này, hãy xem tài liệu về Google Drive Labels API. Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ Drive Labels API sử dụng cùng các đối tượng, phương thức và tham số như API công khai.
Để báo cáo vấn đề và tìm thông tin hỗ trợ khác, hãy xem hướng dẫn hỗ trợ về API Nhãn của Google Drive.
Mã mẫu
Mã mẫu bên dưới sử dụng phiên bản 2 của API.
Liệt kê nhãn
Mẫu mã sau đây cho biết cách lấy danh sách nhãn mà người dùng đưa ra yêu cầu có thể sử dụng.
Nhận nhãn
Mẫu mã sau đây cho thấy cách lấy một nhãn duy nhất theo tên tài nguyên (là giá trị chuỗi của nhãn). Để tìm tên nhãn, hãy lấy danh sách nhãn thông qua API hoặc sử dụng trình quản lý nhãn của Drive. Để biết thêm thông tin về trình quản lý nhãn, hãy xem bài viết Quản lý nhãn trong Drive.
Liệt kê nhãn cho một mục trên Drive
Mã mẫu sau đây cho biết cách lấy một mục trên Drive và liệt kê tất cả nhãn được áp dụng cho mục đó.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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```"]]