Dịch vụ nhãn nâng cao cho Drive

Tạo và quản lý nhãn cho các tệp và thư mục trên Drive bằng dịch vụ nâng cao của Nhãn Google Drive. Với dịch vụ nâng cao này, bạn có thể sử dụng tất cả các tính năng của API Nhãn Drive trong Apps Script.

Để áp dụng hoặc xoá nhãn Drive, hãy sử dụng Dịch vụ Drive nâng cao.

Tài liệu tham khảo

Để biết thêm thông tin về dịch vụ này, hãy xem tài liệu về API Nhãn Google Drive. Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ API Nhãn Drive sử dụng cùng đối tượng, phương thức và tham số như API công khai.

Để báo cáo vấn đề và tìm thông tin hỗ trợ khác, hãy xem hướng dẫn hỗ trợ về API Nhãn Google Drive.

Mã mẫu

Mã mẫu bên dưới sử dụng phiên bản 2 của API.

Danh sách nhãn

Mã mẫu sau đây cho biết cách nhận danh sách nhãn mà người dùng gửi yêu cầu.

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);
}

Tải một nhãn

Mã mẫu sau đây cho biết cách lấy một nhãn theo tên tài nguyên (là giá trị chuỗi của nhãn). Để tìm tên nhãn, hãy lấy danh sách nhãn thông qua API hoặc sử dụng trình quản lý nhãn của Drive. Để biết thêm thông tin về trình quản lý nhãn, hãy chuyển đến phần Quản lý nhãn Drive.

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);
  }
}

Liệt kê nhãn cho một mục trên Drive

Mã mẫu sau đây cho biết cách tải một mục trên Drive và liệt kê tất cả nhãn được áp dụng cho mục đó.

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.items.length);

    appliedLabels.items.forEach((appliedLabel) => {
      // 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);

      Object.keys(appliedLabel.fields).forEach((fieldId) => {
        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);
  }
}