고급 Drive 라벨 서비스

Google Drive 라벨 고급 서비스를 사용하여 Drive 파일 및 폴더의 라벨을 만들고 관리합니다. 이 고급 서비스를 사용하면 Apps Script에서 Drive Labels API의 모든 기능을 사용할 수 있습니다.

Drive 라벨을 적용하거나 삭제하려면 고급 드라이브 서비스를 사용하세요.

참조

이 서비스에 대한 자세한 내용은 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를 통해 라벨 목록을 가져오거나 Drive 라벨 관리자를 사용합니다. 라벨 관리자에 대한 자세한 내용은 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);
  }
}

Drive 항목의 라벨 나열

다음 코드 샘플은 Drive 항목을 가져오고 해당 항목에 적용된 모든 라벨을 나열하는 방법을 보여줍니다.

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