取消設定檔案的標籤欄位

本頁說明如何為單一 Google 雲端硬碟檔案取消設定 Field 標籤。

如要取消設定檔案標籤來移除檔案的中繼資料,請使用 files.modifyLabels 方法。要求主體包含 ModifyLabelsRequest 例項,用於修改檔案中的標籤組合。要求可能包含多項以不可分割的形式套用的修改。也就是說,如果任何修改都無效,整個更新就會失敗,且不會套用任何 (可能相依) 的變更。

ModifyLabelsRequest 包含 LabelModification 的執行個體,這是對檔案標籤的修改。也可能包含 FieldModification 例項,這是對標籤欄位的修改。如要取消設定欄位的值,請將 FieldModification.unsetValues 設為 True

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

範例

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

Java

LabelFieldModification fieldModification =
  new LabelFieldModification().setFieldId("FIELD_ID").setUnsetValues(true);

ModifyLabelsRequest modifyLabelsRequest =
  new ModifyLabelsRequest()
      .setLabelModifications(
          ImmutableList.of(
              new LabelModification()
                .setLabelId("LABEL_ID")
                .setFieldModifications(ImmutableList.of(fieldModification))));

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

Python

field_modification = {'fieldId':'FIELD_ID','unsetValues':True}
label_modification = {'labelId':'LABEL_ID', 'fieldModifications':[field_modification]}

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

Node.js

/**
* Unset a label with a field on a Drive file
* @return{obj} updated label data
**/
async function unsetLabelField() {
  // 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 fieldModification = {
    'fieldId': 'FIELD_ID',
    'unsetValues': True,
  };
  const labelModification = {
    'labelId': 'LABEL_ID',
    'fieldModifications': [fieldModification],
  };
  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;
  }
}

更改下列內容:

  • FIELD_ID:要修改欄位的 fieldId。如要找出 fieldId,請使用 Google Drive Labels API 擷取標籤。
  • LABEL_ID:要修改的標籤的 labelId
  • FILE_ID:要修改標籤的檔案 fileId