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