進階雲端硬碟標籤服務

透過 Google 雲端硬碟標籤進階服務,建立及管理雲端硬碟檔案和資料夾的標籤。透過這項進階服務,您可以使用 Apps Script 中 Drive Labels API 的所有功能。

如要套用或移除雲端硬碟標籤,請使用進階雲端硬碟服務

參考資料

如要進一步瞭解這項服務,請參閱 Google Drive Labels API 的說明文件。如同 Apps Script 的所有進階服務,Drive Labels API 服務便會使用與公用 API 相同的物件、方法和參數。

如要回報問題及尋找其他支援,請參閱 Google Drive Labels API 支援指南

程式碼範例

下方程式碼範例使用第 2 版的 API。

列出標籤

以下程式碼範例說明如何取得提出要求的使用者可用的標籤清單。

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