進階雲端硬碟標籤服務
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用 Google 雲端硬碟標籤進階服務,為雲端硬碟檔案和資料夾建立及管理標籤。透過這項進階服務,您可以在 Apps Script 中使用 Drive Labels API 的所有功能。
如要套用或移除雲端硬碟標籤,請使用進階雲端硬碟服務。
參考資料
如要進一步瞭解這項服務,請參閱 Google 雲端硬碟標籤 API 說明文件。與 Apps Script 中的所有進階服務一樣,Drive Labels API 服務使用的物件、方法和參數都與公開 API 相同。
如要回報問題及尋求其他支援,請參閱 Google Drive Labels API 支援指南。
程式碼範例
下方的範例程式碼使用 API 的第 2 版。
列出標籤
下列程式碼範例說明如何取得可供提出要求的使用者使用的標籤清單。
取得標籤
下列程式碼範例說明如何依資源名稱 (標籤的字串值) 取得單一標籤。如要找出標籤名稱,請透過 API 取得標籤清單,或使用 Google 雲端硬碟標籤管理工具。如要進一步瞭解標籤管理員,請參閱「管理雲端硬碟標籤」。
列出雲端硬碟項目的標籤
下列程式碼範例顯示如何取得 Google 雲端硬碟項目,並列出套用至該項目的所有標籤。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間: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```"]]