取消設定檔案的標籤欄位

本頁面說明如何設定單一 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