בדף הזה מוסבר איך לבטל הגדרה של תווית Field
בקובץ יחיד ב-Google Drive.
כדי להסיר מטא-נתונים מקובץ על ידי ביטול ההגדרה של תווית קובץ, משתמשים בשיטה files.modifyLabels
. גוף הבקשה מכיל מופע של ModifyLabelsRequest
כדי לשנות את קבוצת התוויות בקובץ. יכול להיות שהבקשה תכלול כמה שינויים שיוחלו באופן אטומי. כלומר, אם שינוי כלשהו לא תקין, העדכון כולו ייכשל ואף אחד מהשינויים (שעשויים להיות תלויים זה בזה) לא יוחל.
התג ModifyLabelsRequest
מכיל מופע של LabelModification
, שהוא שינוי בתווית של קובץ. יכול להיות שהוא יכיל גם מופע של FieldModification
, שהוא שינוי בשדה של תווית. כדי לבטל את ההגדרה של הערכים בשדה, מגדירים את FieldModification.unsetValues
ל-True
.
אם הפעולה מצליחה, גוף התשובה מכיל את התוויות שנוספו או עודכנו על ידי הבקשה. הם קיימים באובייקט modifiedLabels
מסוג Label
.
דוגמה
בדוגמת הקוד הבאה מוצג אופן השימוש ב-fieldId
וב-labelId
כדי לבטל את ההגדרה של ערכי השדות ב-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
של הקובץ שהתוויות שלו ישונו.