ยกเลิกการตั้งค่าช่องป้ายกำกับในไฟล์

หน้านี้จะอธิบายวิธียกเลิกการตั้งค่าป้ายกำกับ Field ในไฟล์ Google ไดรฟ์ไฟล์เดียว

หากต้องการนำข้อมูลเมตาออกจากไฟล์โดยการเลิกตั้งค่าป้ายกำกับไฟล์ ให้ใช้เมธอด 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 ให้เรียกข้อมูลป้ายกำกับโดยใช้ API ป้ายกำกับของ Google ไดรฟ์
  • LABEL_ID: labelId ของป้ายกำกับที่จะแก้ไข
  • FILE_ID: fileId ของไฟล์ที่มีการแก้ไขป้ายกำกับ