Удалить метку из файла

На этой странице описано, как удалить метку с отдельного файла в Google Диск.

To remove the file label metadata from a file, use the files.modifyLabels method. The request body contains an instance of ModifyLabelsRequest to modify the set of labels on a file. The request might contain several modifications that are applied atomically. That is, if any modifications aren't valid, then the entire update is unsuccessful and none of the (potentially dependent) changes are applied.

Объект ModifyLabelsRequest содержит экземпляр LabelModification , который представляет собой изменение метки в файле. Он также может содержать экземпляр FieldModification , который представляет собой изменение поля метки. Чтобы удалить метку из файла, установите FieldModification.removeLabel в True .

В случае успеха тело ответа содержит метки, добавленные или обновленные в результате запроса. Они находятся в объекте modifiedLabels типа Label .

Пример

The following code sample shows how to use the labelId to remove all fields associated with the label using the fileId . For example, if a label contains both text and user fields, removing a label deletes both the text and user fields associated with the label. Whereas, unsetting the text field removes it from the label but leaves the user field untouched. For more information, see Unset a label field on a file .

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',
      requestBody: labelModificationRequest,
    });
    return updateResponse;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

Замените следующее:

  • LABEL_ID : labelId метки, которую нужно изменить. Чтобы найти метки в файле, используйте метод files.listLabels .
  • FILE_ID : fileId файла, для которого изменяются метки.