移除檔案的標籤

本頁說明如何移除單一 Google 雲端硬碟檔案的標籤。

如要移除檔案的檔案標籤中繼資料,請使用 files.modifyLabels 方法。要求主體包含 ModifyLabelsRequest 執行個體,用於修改檔案的標籤組合。要求可能包含某些會自動套用的修改內容。也就是說,如果任何修改無效,則整個更新都會失敗,且不會套用任何 (可能相依) 的變更。

ModifyLabelsRequest 包含 LabelModification 的執行個體,可用來修改檔案標籤。也可能包含 FieldModification 的執行個體,這是為了修改標籤欄位。如要從檔案中移除標籤,請將 FieldModification.removeLabel 設為 True

如果成功,回應主體會包含要求新增或更新的標籤。這些函式存在於 Label 類型的 modifiedLabels 物件中。

範例

以下程式碼範例說明如何使用 labelId 移除使用 fileId 與該標籤相關聯的所有欄位。舉例來說,如果標籤同時包含文字和使用者欄位,則移除標籤「同時」會刪除與標籤相關聯的文字和使用者欄位。取消設定文字欄位會將欄位從標籤中移除,但使用者欄位則維持不變。詳情請參閱取消設定檔案中的標籤欄位

Java

ModifyLabelsRequest modifyLabelsRequest =
  new ModifyLabelsRequest()
      .setLabelModifications(
          ImmutableList.of(
              new LabelModification()
                .setLabelId("LABEL_ID")
                .setRemoveLabel(true)));

ModifyLabelsResponse modifyLabelsResponse = driveService.files().modifyLabels("FILE_ID", modifyLabelsRequest).execute();

Python

label_modification = {'labelId':'LABEL_ID', 'removeLabel': True]}

modified_labels = drive_service.files().modifyLabels(fileId="FILE_ID", body = {'labelModifications' : [label_modification]}).execute();

Node.js

/**
* Remove a label on a Drive file
* @return{obj} updated label data
**/
async function removeLabel() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
  const service = google.drive({version: 'v3', auth});
  const labelModification = {
    'labelId': 'LABEL_ID',
    'removeLabel': True,
  };
  const labelModificationRequest = {
    'labelModifications': [labelModification],
  };
  try {
    const updateResponse = await service.files.modifyLabels({
      fileId: 'FILE_ID',
      resource: labelModificationRequest,
    });
    return updateResponse;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }

更改下列內容:

  • LABEL_ID:要修改的標籤的 labelId。如要找出檔案的標籤,請使用 files.listLabels 方法。
  • FILE_ID:經過修改的檔案的 fileId