本頁說明如何移除單一 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。