הגדרה של שדה תווית בקובץ

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

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

השדה ModifyLabelsRequest מכיל מופע של LabelModification שינוי בתווית בקובץ. היא יכולה גם להכיל מופע של FieldModification, שהוא שינוי בשדה של תווית.

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

דוגמה

דוגמת הקוד הבאה ממחישה איך להשתמש ב-fieldId בשדה טקסט כדי להגדיר ערך בשביל Field בקובץ. כשהתווית Field מוגדרת בקובץ בהתחלה, היא חלה על הקובץ. אחר כך אפשר לבטל את ההגדרה של שדה יחיד או להסיר את כל השדות שמשויכים לתווית. מידע נוסף זמין במאמרים ביטול הגדרה של שדה תווית בקובץ והסרת תווית מקובץ.

Java

LabelFieldModification fieldModification =
new LabelFieldModification().setFieldId("FIELD_ID").setSetTextValues(ImmutableList.of("VALUE"));

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','setTextValues':['VALUE']}
label_modification = {'labelId':'LABEL_ID', 'fieldModifications':[field_modification]}

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

Node.js

/**
* Set a label with a text field on a Drive file
* @return{obj} updated label data
**/
async function setLabelTextField() {
  // 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',
    'setTextValues': ['VALUE'],
  };
  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.
  • VALUE: הערך החדש של value לשדה הזה.
  • LABEL_ID: ה-labelId של התווית שרוצים לשנות.
  • FILE_ID: ה-fileId של הקובץ שאת התוויות שלו שונו.

הערות

  • כדי להגדיר תווית ללא שדות, צריך להחיל את הערך labelModifications בלי fieldModifications.
  • כדי להגדיר ערכים לאפשרויות של שדה הבחירה, משתמשים במזהה Choice של הערך שמקבלים על ידי אחזור סכימת התוויות מ-Drive Labels API.
  • אפשר להגדיר כמה ערכים רק בשדה Field שתומך ברשימות ערכים, אחרת תתקבל הודעת השגיאה 400: Bad Request.
  • צריך להגדיר את הסוג של הערך המתאים ל-Field שנבחר (למשל מספר שלם, טקסט, משתמש וכו'), אחרת תקבלו תשובה עם השגיאה 400: Bad Request. אפשר לאחזר את סוג הנתונים של השדות באמצעות Drive Labels API.