创建和发布标签
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页面介绍了如何创建和发布标签。
创建标签
如需创建标签,请对 labels
集合使用 create
方法。标签可以选择性地包含一个或多个 Fields
。
您还必须指定:
此示例会创建一个具有指定标题的标准管理员标签,以及一个包含 2 个 Choices
的 SelectionOptions
Field
。
Python
label_body = {
'label_type': 'ADMIN',
'properties': {
'title': 'TITLE'
},
'fields': [{
'properties': {
'display_name': 'DISPLAY_NAME'
},
'selection_options': {
'list_options': {},
'choices': [{
'properties': {
'display_name': 'CHOICE_1'
}
}, {
'properties': {
'display_name': 'CHOICE_2'
}
}]
}
}]
}
response = service.labels().create(
body=label_body, useAdminAccess=True).execute()
Node.js
var label = {
'label_type': 'ADMIN',
'properties': {
'title': 'TITLE'
},
'fields': [{
'properties': {
'display_name': 'DISPLAY_NAME'
},
'selection_options': {
'list_options': {},
'choices': [{
'properties': {
'display_name': 'CHOICE_1'
}
}, {
'properties': {
'display_name': 'CHOICE_2'
}
}]
}
}]
};
service.labels.create({
'resource': label,
'use_admin_access': true
}, (err, res) => {
if (err) return console.error('The API returned an error: ' + err);
console.log(res);
});
标签是在 UNPUBLISHED_DRAFT
State
中创建的。它必须是 PUBLISHED
,用于文件,并且对用户可见。如需了解详情,请参阅标签生命周期。
标签数量限制
创建和应用标签时,存在以下限制:
- 标准标签 - 150
- 每个标签的字段数 - 10
- 每个文件的用户应用标签数量上限 - 5
使用标签中的字段
发布标签
借助 Labels API,您可以在草稿标签完成后发布该标签。
如需发布标签,请使用 publish
方法并指定:
此示例使用 ID
来识别并发布正确的标签。
Python
service.labels().publish(
name='labels/ID',
body={
'use_admin_access': True
}).execute()
Node.js
service.labels.publish({
'resource': {
'use_admin_access': true
},
'name': 'labels/ID'
}, (err, res) => {
if (err) return console.error('The API returned an error: ' + err);
console.log(res);
});
标签发布后,该标签会移至 PUBLISHED
State
,并且标签的修订版本 ID 会递增。然后,用户就可以看到该标签,并将其应用于文件。如需了解详情,请参阅标签生命周期。
发布标签时的限制
标签一经发布,便无法返回到原始草稿状态。如需了解详情,请参阅标签生命周期。
发布标签会创建新发布的修订版本。所有之前的草稿修订版本都会被删除。系统会保留之前发布的修订版本,但会根据需要自动删除这些版本。
发布后,您将无法再进行某些更改。一般来说,这意味着任何会使与标签相关的现有元数据失效或触发新限制的更改都会被拒绝。例如,标签发布后,不允许对标签进行以下更改:
- 标签会被直接删除。(必须先停用,然后才能删除。)
Field.FieldType
已更改。
- 对字段验证选项的更新会拒绝之前接受的内容。
- 减少了最大条目数。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Create & publish a label\n\nThis page describes how to create and publish a label.\n\nCreate a label\n--------------\n\nTo create a label, use the\n[`create`](/workspace/drive/labels/reference/rest/v2/labels/create) method on the\n[`labels`](/workspace/drive/labels/reference/rest/v2/labels) collection. Labels can\noptionally contain one or more\n[`Fields`](/workspace/drive/labels/reference/rest/v2/labels#field).\n\nYou also must specify:\n\n- A [`LabelType`](/workspace/drive/labels/reference/rest/v2/labels#labeltype).\n\n- A label `title` through the\n [`property`](/workspace/drive/labels/reference/rest/v2/labels#properties) method.\n\n- `useAdminAccess` is `true` to use the user's administrator credentials. The\n server verifies that the user is an admin with the \"Manage Labels\" privilege\n before allowing access.\n\nThis example creates a standard, admin label with a specified title and one\n[`SelectionOptions`](/workspace/drive/labels/reference/rest/v2/labels#selectionoptions)\n`Field` with 2 [`Choices`](/workspace/drive/labels/reference/rest/v2/labels#choice). \n\n### Python\n\n label_body = {\n 'label_type': 'ADMIN',\n 'properties': {\n 'title': '\u003cvar translate=\"no\"\u003eTITLE\u003c/var\u003e'\n },\n 'fields': [{\n 'properties': {\n 'display_name': '\u003cvar translate=\"no\"\u003eDISPLAY_NAME\u003c/var\u003e'\n },\n 'selection_options': {\n 'list_options': {},\n 'choices': [{\n 'properties': {\n 'display_name': '\u003cvar translate=\"no\"\u003eCHOICE_1\u003c/var\u003e'\n }\n }, {\n 'properties': {\n 'display_name': '\u003cvar translate=\"no\"\u003eCHOICE_2\u003c/var\u003e'\n }\n }]\n }\n }]\n }\n response = service.labels().create(\n body=label_body, useAdminAccess=True).execute()\n\n### Node.js\n\n var label = {\n 'label_type': 'ADMIN',\n 'properties': {\n 'title': '\u003cvar translate=\"no\"\u003eTITLE\u003c/var\u003e'\n },\n 'fields': [{\n 'properties': {\n 'display_name': '\u003cvar translate=\"no\"\u003eDISPLAY_NAME\u003c/var\u003e'\n },\n 'selection_options': {\n 'list_options': {},\n 'choices': [{\n 'properties': {\n 'display_name': '\u003cvar translate=\"no\"\u003eCHOICE_1\u003c/var\u003e'\n }\n }, {\n 'properties': {\n 'display_name': '\u003cvar translate=\"no\"\u003eCHOICE_2\u003c/var\u003e'\n }\n }]\n }\n }]\n };\n\n service.labels.create({\n 'resource': label,\n 'use_admin_access': true\n }, (err, res) =\u003e {\n if (err) return console.error('The API returned an error: ' + err);\n console.log(res);\n });\n\nThe label is created in the `UNPUBLISHED_DRAFT`\n[`State`](/workspace/drive/labels/reference/rest/v2/labels#state). It must be `PUBLISHED`\nfor use on files and to be visible to users. For more information, see [Label\nlifecycle](/workspace/drive/labels/guides/label-lifecycle).\n\n### Label limits\n\nThe following limits apply when creating and applying labels:\n\n- Standard labels---150\n- Fields per label---10\n- User-applied labels per file---5\n\n### Working with fields in labels\n\n\u003cbr /\u003e\n\n| `Field Type` | What users can do | Format considerations and limits |\n|-----------------------------------------------------------------------------------------|----------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [`SelectionOptions`](/workspace/drive/labels/reference/rest/v2/labels#selectionoptions) | Select one or more options from a list | Maximum number of options supported is 200 Maximum number of selections for a file is 10 as configured in [`ListOptions`](/workspace/drive/labels/reference/rest/v2/labels#listoptions) |\n| [`IntegerOptions`](/workspace/drive/labels/reference/rest/v2/labels#integeroptions) | Enter a numerical value | Only whole numbers are supported |\n| [`DateOptions`](/workspace/drive/labels/reference/rest/v2/labels#dateoptions) | Select a calendar date | (Optional) [`Date format`](/workspace/drive/labels/reference/rest/v2/labels#dateformat) can be set to \"Long: Month DD, YYYY\", or \"Short: MM/DD/YY\" |\n| [`TextOptions`](/workspace/drive/labels/reference/rest/v2/labels#textoptions) | Enter text in a text box | Maximum character count supported is 100 |\n| [`UserOptions`](/workspace/drive/labels/reference/rest/v2/labels#UserOptions) | Select one or more users from Google Workspace contacts. | Maximum number of user entries for a file is 10 as configured in [`ListOptions`](/workspace/drive/labels/reference/rest/v2/labels#listoptions) |\n\n\u003cbr /\u003e\n\nPublish a label\n---------------\n\nThe Labels API lets you publish a draft label once it's complete.\n\nTo publish a label, use the\n[`publish`](/workspace/drive/labels/reference/rest/v2/labels/publish) method and specify:\n\n- A [Label resource](/workspace/drive/labels/reference/rest/v2/labels#resource:-label)\n that represents every label. It contains a resource `Name` and `ID`, which\n is a globally unique identifier for the label.\n\n- `useAdminAccess` is `true` to use the user's administrator credentials. The\n server verifies that the user is an admin with the \"Manage Labels\" privilege\n before allowing access.\n\nThis example uses the `ID` to identify and publish the correct label. \n\n### Python\n\n service.labels().publish(\n name='labels/\u003cvar translate=\"no\"\u003eID\u003c/var\u003e',\n body={\n 'use_admin_access': True\n }).execute()\n\n### Node.js\n\n service.labels.publish({\n 'resource': {\n 'use_admin_access': true\n },\n 'name': 'labels/\u003cvar translate=\"no\"\u003eID\u003c/var\u003e'\n }, (err, res) =\u003e {\n if (err) return console.error('The API returned an error: ' + err);\n console.log(res);\n });\n\nOnce the label is published, the label moves to the `PUBLISHED`\n[`State`](/workspace/drive/labels/reference/rest/v2/labels#state) and the label's revision\nID is incremented. The label is then visible to users and can be applied to\nfiles. For more information, see [Label\nlifecycle](/workspace/drive/labels/guides/label-lifecycle).\n\n### Constraints when publishing labels\n\n- Once published, a label cannot return to its original draft state. For more\n information, see [Label lifecycle](/workspace/drive/labels/guides/label-lifecycle).\n\n- Publishing a label creates a newly published revision. All previous draft\n revisions are deleted. Previously published revisions are kept but are\n subject to automated deletion as needed.\n\n- Once published, some changes are no longer permitted. Generally, this means\n any change that invalidates or triggers new restrictions on existing\n metadata related to the label is rejected. For example, the following\n changes to a label aren't permitted after the label is published:\n\n - The label is directly deleted. (It must be disabled first, then deleted.)\n - The `Field.FieldType` is changed.\n - Updates to the Field validation options reject something previously accepted.\n - A reduction in maximum entries."]]