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

หน้านี้อธิบายวิธียกเลิกการตั้งค่าป้ายกำกับ 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',
      requestBody: 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 ของไฟล์ที่จะแก้ไขป้ายกำกับ