บริการป้ายกำกับไดรฟ์ขั้นสูง

สร้างและจัดการป้ายกำกับสำหรับไฟล์และโฟลเดอร์ในไดรฟ์ด้วยบริการขั้นสูงของป้ายกำกับใน Google ไดรฟ์ บริการขั้นสูงนี้ช่วยให้คุณใช้ฟีเจอร์ทั้งหมดของ Drive Labels API ใน Apps Script ได้

หากต้องการใช้หรือนำป้ายกำกับไดรฟ์ออก ให้ใช้ บริการไดรฟ์ขั้นสูง

ข้อมูลอ้างอิง

ดูข้อมูลเพิ่มเติมเกี่ยวกับบริการนี้ได้ในเอกสารประกอบสำหรับ Google Drive Labels API เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script บริการ Drive Labels API จะใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ

หากต้องการรายงานปัญหาและรับการสนับสนุนอื่นๆ โปรดดูคู่มือการสนับสนุนของ Google Drive Labels API

โค้ดตัวอย่าง

โค้ดตัวอย่างด้านล่างใช้ API เวอร์ชัน 2

ป้ายกำกับรายการ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับรายการป้ายกำกับที่พร้อมใช้งานสำหรับ ผู้ใช้ที่ส่งคำขอ

advanced/driveLabels.gs
/**
 * List labels available to the user.
 */
function listLabels() {
  let pageToken = null;
  let labels = [];
  do {
    try {
      const response = DriveLabels.Labels.list({
        publishedOnly: true,
        pageToken: pageToken,
      });
      pageToken = response.nextPageToken;
      labels = labels.concat(response.labels);
    } catch (err) {
      // TODO (developer) - Handle exception
      console.log("Failed to list labels with error %s", err.message);
    }
  } while (pageToken != null);

  console.log("Found %d labels", labels.length);
}

รับป้ายกำกับ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับป้ายกำกับรายการเดียวโดยใช้ ชื่อทรัพยากร (ซึ่งเป็นค่าสตริงของป้ายกำกับ) หากต้องการค้นหาชื่อป้ายกำกับ ให้รับรายการ ป้ายกำกับผ่าน API หรือใช้เครื่องมือจัดการป้ายกำกับของไดรฟ์ ดูข้อมูลเพิ่มเติม เกี่ยวกับเครื่องมือจัดการป้ายกำกับได้ที่ จัดการป้ายกำกับไดรฟ์

advanced/driveLabels.gs
/**
 * Get a label by name.
 * @param {string} labelName The label name.
 */
function getLabel(labelName) {
  try {
    const label = DriveLabels.Labels.get(labelName, {
      view: "LABEL_VIEW_FULL",
    });
    const title = label.properties.title;
    const fieldsLength = label.fields.length;
    console.log(
      `Fetched label with title: '${title}' and ${fieldsLength} fields.`,
    );
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log("Failed to get label with error %s", err.message);
  }
}

แสดงป้ายกำกับสำหรับรายการในไดรฟ์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับรายการในไดรฟ์และแสดงป้ายกำกับทั้งหมด ที่ใช้กับรายการนั้น

advanced/driveLabels.gs
/**
 * List Labels on a Drive Item
 * Fetches a Drive Item and prints all applied values along with their to their
 * human-readable names.
 *
 * @param {string} fileId The Drive File ID
 */
function listLabelsOnDriveItem(fileId) {
  try {
    const appliedLabels = Drive.Files.listLabels(fileId);

    console.log(
      "%d label(s) are applied to this file",
      appliedLabels.labels.length,
    );

    for (const appliedLabel of appliedLabels.labels) {
      // Resource name of the label at the applied revision.
      const labelName = `labels/${appliedLabel.id}@${appliedLabel.revisionId}`;

      console.log("Fetching Label: %s", labelName);
      const label = DriveLabels.Labels.get(labelName, {
        view: "LABEL_VIEW_FULL",
      });

      console.log("Label Title: %s", label.properties.title);

      for (const fieldId of Object.keys(appliedLabel.fields)) {
        const fieldValue = appliedLabel.fields[fieldId];
        const field = label.fields.find((f) => f.id === fieldId);

        console.log(
          `Field ID: ${field.id}, Display Name: ${field.properties.displayName}`,
        );
        switch (fieldValue.valueType) {
          case "text":
            console.log("Text: %s", fieldValue.text[0]);
            break;
          case "integer":
            console.log("Integer: %d", fieldValue.integer[0]);
            break;
          case "dateString":
            console.log("Date: %s", fieldValue.dateString[0]);
            break;
          case "user": {
            const user = fieldValue.user
              .map((user) => {
                return `${user.emailAddress}: ${user.displayName}`;
              })
              .join(", ");
            console.log(`User: ${user}`);
            break;
          }
          case "selection": {
            const choices = fieldValue.selection.map((choiceId) => {
              return field.selectionOptions.choices.find(
                (choice) => choice.id === choiceId,
              );
            });
            const selection = choices
              .map((choice) => {
                return `${choice.id}: ${choice.properties.displayName}`;
              })
              .join(", ");
            console.log(`Selection: ${selection}`);
            break;
          }
          default:
            console.log("Unknown: %s", fieldValue.valueType);
            console.log(fieldValue.value);
        }
      }
    }
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log("Failed with error %s", err.message);
  }
}