با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این صفحه نحوه تنظیم یک برچسب Field در یک فایل Google Drive توضیح می دهد.
برای افزودن متادیتا به یک فایل با تنظیم برچسب فایل، از روش files.modifyLabels استفاده کنید. بدنه درخواست شامل نمونهای از ModifyLabelsRequest برای اصلاح مجموعه برچسبها در یک فایل است. درخواست ممکن است شامل تغییرات متعددی باشد که به صورت اتمی اعمال می شوند. به این معنا که اگر هر گونه تغییری معتبر نباشد، کل بهروزرسانی ناموفق است و هیچ یک از تغییرات (بالقوه وابسته) اعمال نمیشود.
ModifyLabelsRequest شامل نمونه ای از LabelModification است که اصلاحی برای برچسب روی یک فایل است. همچنین ممکن است شامل یک نمونه از FieldModification باشد که اصلاحی در فیلد یک برچسب است.
در صورت موفقیت آمیز بودن، بدنه پاسخ حاوی برچسب هایی است که توسط درخواست اضافه یا به روز شده است. اینها در یک شیء modifiedLabels از نوع Label وجود دارند.
مثال
نمونه کد زیر نحوه استفاده از fieldId یک فیلد متنی را برای تعیین مقدار این Field در یک فایل نشان می دهد. هنگامی که یک Field برچسب در ابتدا روی یک فایل تنظیم می شود، برچسب را به فایل اعمال می کند. سپس میتوانید یک فیلد را تنظیم کنید یا همه فیلدهای مرتبط با برچسب را حذف کنید. برای اطلاعات بیشتر، به لغو تنظیم فیلد برچسب روی فایل و حذف برچسب از فایل مراجعه کنید.
/*** Set a label with a text field on a Drive file* @return{obj} updated label data**/asyncfunctionsetLabelTextField(){// Get credentials and build service// TODO (developer) - Use appropriate auth mechanism for your appconst{GoogleAuth}=require('google-auth-library');const{google}=require('googleapis');constauth=newGoogleAuth({scopes:'https://www.googleapis.com/auth/drive'});constservice=google.drive({version:'v3',auth});constfieldModification={'fieldId':'FIELD_ID','setTextValues':['VALUE'],};constlabelModification={'labelId':'LABEL_ID','fieldModifications':[fieldModification],};constlabelModificationRequest={'labelModifications':[labelModification],};try{constupdateResponse=awaitservice.files.modifyLabels({fileId:'FILE_ID',resource:labelModificationRequest,});returnupdateResponse;}catch(err){// TODO (developer) - Handle errorthrowerr;}}
موارد زیر را جایگزین کنید:
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 بازیابی کنید.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Set a label field on a file\n\nThis page describes how to set a label\n[`Field`](/workspace/drive/labels/reference/rest/v2/labels#field) on a single\nGoogle Drive file.\n\nTo add metadata to a file by setting a file label, use the\n[`files.modifyLabels`](/workspace/drive/api/v2/reference/files/modifyLabels) method. The\n[request body](/workspace/drive/api/reference/rest/v2/files/modifyLabels#request-body)\ncontains an instance of\n[`ModifyLabelsRequest`](/workspace/drive/api/reference/rest/v2/files/modifyLabels#modifylabelsrequest)\nto modify the set of labels on a file. The request might contain several\nmodifications that are applied atomically. That is, if any modifications aren't\nvalid, then the entire update is unsuccessful and none of the (potentially\ndependent) changes are applied.\n\nThe `ModifyLabelsRequest` contains an instance of\n[`LabelModification`](/workspace/drive/api/reference/rest/v2/files/modifyLabels#labelmodification)\nwhich is a modification to a label on a file. It might also contain an instance\nof\n[`FieldModification`](/workspace/drive/api/reference/rest/v2/files/modifyLabels#fieldmodification)\nwhich is a modification to a label's field.\n\nIf successful, the [response\nbody](/workspace/drive/api/reference/rest/v2/files/modifyLabels#response-body) contains\nthe labels added or updated by the request. These exist within a\n`modifiedLabels` object of type [`Label`](/workspace/drive/api/reference/rest/v2/Label).\n\nExample\n-------\n\nThe following code sample shows how to use the `fieldId` of a text field to set\na value for this [`Field`](/workspace/drive/labels/reference/rest/v2/labels#field) on a\nfile. When a label `Field` is initially set on a file, it applies the label to\nthe file. You can then unset a single field or remove all fields associated with\nthe label. For more information, see [Unset a label field on a\nfile](/workspace/drive/api/guides/unset-label) and [Remove a label from a\nfile](/workspace/drive/api/guides/remove-label). \n\n### Java\n\n LabelFieldModification fieldModification =\n new LabelFieldModification().setFieldId(\"\u003cvar translate=\"no\"\u003eFIELD_ID\u003c/var\u003e\").setSetTextValues(ImmutableList.of(\"\u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e\"));\n\n ModifyLabelsRequest modifyLabelsRequest =\n new ModifyLabelsRequest()\n .setLabelModifications(\n ImmutableList.of(\n new LabelModification()\n .setLabelId(\"\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e\")\n .setFieldModifications(ImmutableList.of(fieldModification))));\n\n ModifyLabelsResponse modifyLabelsResponse = driveService.files().modifyLabels(\"\u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e\", modifyLabelsRequest).execute();\n\n### Python\n\n field_modification = {'fieldId':'\u003cvar translate=\"no\"\u003eFIELD_ID\u003c/var\u003e','setTextValues':['\u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e']}\n label_modification = {'labelId':'\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e', 'fieldModifications':[field_modification]}\n\n modified_labels = drive_service.files().modifyLabels(fileId=\"\u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e\", body = {'labelModifications' : [label_modification]}).execute()\n\n### Node.js\n\n /**\n * Set a label with a text field on a Drive file\n * @return{obj} updated label data\n **/\n async function setLabelTextField() {\n // Get credentials and build service\n // TODO (developer) - Use appropriate auth mechanism for your app\n\n const {GoogleAuth} = require('google-auth-library');\n const {google} = require('googleapis');\n\n const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});\n const service = google.drive({version: 'v3', auth});\n const fieldModification = {\n 'fieldId': '\u003cvar translate=\"no\"\u003eFIELD_ID\u003c/var\u003e',\n 'setTextValues': ['\u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e'],\n };\n const labelModification = {\n 'labelId': '\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e',\n 'fieldModifications': [fieldModification],\n };\n const labelModificationRequest = {\n 'labelModifications': [labelModification],\n };\n try {\n const updateResponse = await service.files.modifyLabels({\n fileId: '\u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e',\n resource: labelModificationRequest,\n });\n return updateResponse;\n } catch (err) {\n // TODO (developer) - Handle error\n throw err;\n }\n }\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eFIELD_ID\u003c/var\u003e: The `fieldId` of the field to modify. To locate the `fieldId`, retrieve the label using the [Google Drive Labels API](/workspace/drive/labels/guides/search-label).\n- \u003cvar translate=\"no\"\u003eVALUE\u003c/var\u003e: The new `value` for this field.\n- \u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e: The `labelId` of the label to modify.\n- \u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e: The `fileId` of the file for which the labels are modified.\n\nNotes\n-----\n\n- To set a label with no fields, apply `labelModifications` with no `fieldModifications` present.\n- To set values for selection field options, use the [`Choice`](/workspace/drive/labels/reference/rest/v2/labels#choice) id of the value that you can get by fetching the label schema in the [Drive Labels API](/workspace/drive/labels/guides/overview).\n- Only a `Field` that supports lists of values can have multiple values set, otherwise you'll receive a `400: Bad Request` error response.\n- Set the proper value type for the selected `Field` (such as integer, text, user, etc.), otherwise you'll receive a `400: Bad Request` error response. You can retrieve the field data type using the [Drive Labels API](/workspace/drive/labels/reference/rest/v2/labels#field)."]]