הסרת תווית מקובץ

בדף זה מוסבר איך להסיר תווית בקובץ אחד ב-Google Drive.

כדי להסיר מטא-נתונים של תוויות קובץ מקובץ, משתמשים ב-method files.modifyLabels. גוף הבקשה מכיל מופע של ModifyLabelsRequest שמאפשר לשנות את קבוצת התוויות בקובץ. הבקשה עשויה לכלול מספר שינויים מיושמים באופן אטומי. כלומר, אם שינויים כלשהם לא תקינים, העדכון כולו לא יכשל, ואף אחד מהשינויים (שעשויים להיות תלויים בהם) לא ייושם.

השדה ModifyLabelsRequest מכיל מופע של LabelModification, שהוא שינוי בתווית בקובץ. הוא יכול גם להכיל מופע של FieldModification, שהוא שינוי בשדה של תווית. כדי להסיר את התווית מהקובץ, צריך להגדיר את FieldModification.removeLabel לערך True.

אם הבדיקה הצליחה, גוף התגובה יכלול את התוויות שנוספו או עודכנו על ידי הבקשה. הן קיימות באובייקט modifiedLabels מסוג Label.

דוגמה

דוגמת הקוד הבאה ממחישה איך להשתמש ב-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 של התווית שצריך לשנות. כדי לאתר את התוויות בקובץ, משתמשים ב-method files.listLabels.
  • FILE_ID: ה-fileId של הקובץ שעבורו שונו התוויות.