Guida rapida di Google Apps Script

Le guide rapide spiegano come configurare ed eseguire un'app che chiama un'API Google Workspace.

Le guide rapide di Google Workspace utilizzano le librerie client API per gestire alcuni dettagli del flusso di autenticazione e autorizzazione. Ti consigliamo di utilizzare le librerie client per le tue app. Questa guida rapida utilizza un approccio di autenticazione semplificato appropriato per un ambiente di test. Per un ambiente di produzione, ti consigliamo di informarti su autenticazione e autorizzazione prima di scegliere le credenziali di accesso appropriate per la tua app.

Crea uno Google Apps Script che invia richieste all'API Google Drive Activity.

Obiettivi

  • Crea lo script.
  • Attivare l'API Google Drive Activity.
  • Esegui l'esempio.

Prerequisiti

  • Un Account Google
  • Accesso a Google Drive

Crea lo script

  1. Per creare un nuovo script, vai alla pagina script.google.com/create.
  2. Sostituisci i contenuti dell'editor di script con il seguente codice:

drive/activity-v2/quickstart.gs.
/**
 * Lists 10 activity for a Drive user.
 * @see https://developers.google.com/drive/activity/v2/reference/rest/v2/activity/query
 */
function listDriveActivity() {
  const request = {
    pageSize: 10
    // Use other parameter here if needed.
  };
  try {
    // Activity.query method is used Query past activity in Google Drive.
    const response = DriveActivity.Activity.query(request);
    const activities = response.activities;
    if (!activities || activities.length === 0) {
      console.log('No activity.');
      return;
    }
    console.log('Recent activity:');
    for (const activity of activities) {
      // get time information of activity.
      const time = getTimeInfo(activity);
      // get the action details/information
      const action = getActionInfo(activity.primaryActionDetail);
      // get the actor's details of activity
      const actors = activity.actors.map(getActorInfo);
      // get target information of activity.
      const targets = activity.targets.map(getTargetInfo);
      // print the time,actor,action and targets of drive activity.
      console.log('%s: %s, %s, %s', time, actors, action, targets);
    }
  } catch (err) {
    // TODO (developer) - Handle error from drive activity API
    console.log('Failed with an error %s', err.message);
  }
}

/**
 * @param {object} object
 * @return {string}  Returns the name of a set property in an object, or else "unknown".
 */
function getOneOf(object) {
  for (const key in object) {
    return key;
  }
  return 'unknown';
}

/**
 * @param {object} activity Activity object.
 * @return {string} Returns a time associated with an activity.
 */
function getTimeInfo(activity) {
  if ('timestamp' in activity) {
    return activity.timestamp;
  }
  if ('timeRange' in activity) {
    return activity.timeRange.endTime;
  }
  return 'unknown';
}

/**
 * @param {object} actionDetail The primary action details of the activity.
 * @return {string} Returns the type of action.
 */
function getActionInfo(actionDetail) {
  return getOneOf(actionDetail);
}

/**
 * @param {object} user The User object.
 * @return {string}  Returns user information, or the type of user if not a known user.
 */
function getUserInfo(user) {
  if ('knownUser' in user) {
    const knownUser = user.knownUser;
    const isMe = knownUser.isCurrentUser || false;
    return isMe ? 'people/me' : knownUser.personName;
  }
  return getOneOf(user);
}

/**
 * @param {object} actor The Actor object.
 * @return {string} Returns actor information, or the type of actor if not a user.
 */
function getActorInfo(actor) {
  if ('user' in actor) {
    return getUserInfo(actor.user);
  }
  return getOneOf(actor);
}

/**
 * @param {object} target The Target object.
 * @return {string} Returns the type of a target and an associated title.
 */
function getTargetInfo(target) {
  if ('driveItem' in target) {
    const title = target.driveItem.title || 'unknown';
    return 'driveItem:"' + title + '"';
  }
  if ('drive' in target) {
    const title = target.drive.title || 'unknown';
    return 'drive:"' + title + '"';
  }
  if ('fileComment' in target) {
    const parent = target.fileComment.parent || {};
    const title = parent.title || 'unknown';
    return 'fileComment:"' + title + '"';
  }
  return getOneOf(target) + ':unknown';
}

  1. Fai clic su Salva .
  2. Fai clic su Progetto senza titolo, digita Guida rapida e fai clic su Rinomina.

Attiva l'API Google Drive Activity

  1. Apri il progetto Apps Script.
  2. Fai clic su Editor .
  3. Accanto a Servizi, fai clic su Aggiungi un servizio .
  4. Seleziona l'API Drive Activity e fai clic su Aggiungi.

Esegui l'esempio

Nell'editor di Apps Script, fai clic su Esegui.

La prima volta che esegui l'esempio, ti viene chiesto di autorizzare l'accesso:

  1. Fai clic su Rivedi autorizzazioni.
  2. Scegli un account.
  3. Fai clic su Consenti.

Il log di esecuzione dello script viene visualizzato nella parte inferiore della finestra.

Passaggi successivi