Servizio avanzato per le etichette di Drive

Crea e gestisci le etichette per i tuoi file e le tue cartelle di Drive con il servizio avanzato Etichette di Google Drive. Con questo servizio avanzato, puoi utilizzare tutte le funzionalità dell'API Drive Labels in Apps Script.

Per applicare o rimuovere le etichette di Drive, utilizza il servizio Drive avanzato.

Riferimento

Per saperne di più su questo servizio, consulta la documentazione dell'API Google Drive Labels. Come tutti i servizi avanzati in Apps Script, il servizio API Drive Labels utilizza gli stessi oggetti, metodi e parametri dell'API pubblica.

Per segnalare problemi e trovare altro supporto, consulta la guida all'assistenza dell'API Google Drive Labels.

Codice di esempio

Il codice campione riportato di seguito utilizza la versione 2 dell'API.

Etichette di elenchi

Il seguente esempio di codice mostra come ottenere un elenco delle etichette disponibili per l'utente che effettua la richiesta.

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

Ottenere un'etichetta

Il seguente esempio di codice mostra come ottenere una singola etichetta in base al relativo nome risorsa (che è il valore stringa dell'etichetta). Per trovare il nome dell'etichetta, recupera l'elenco delle etichette tramite l'API o utilizza il gestore delle etichette di Drive. Per saperne di più sul gestore delle etichette, vai a Gestire le etichette di 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);
  }
}

Elencare le etichette per un elemento di Drive

Il seguente esempio di codice mostra come ottenere un elemento di Drive ed elencare tutte le etichette applicate a quell'elemento.

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