Google Apps Script 快速入門

建立向 Google Drive Activity API 發出要求的 Google Apps Script 指令碼

快速入門導覽課程會說明如何設定及執行應用程式,來呼叫 Google Workspace API。本快速入門導覽課程會使用簡化的驗證方法,適用於測試環境。在正式環境中,建議您先瞭解驗證和授權,再選擇適合應用程式的存取憑證

在 Apps Script,Google Workspace 快速入門導覽課程會使用進階 Google 服務呼叫 Google Workspace API,並處理驗證和授權流程的部分詳細資料。

目標

  • 設定環境。
  • 建立及設定指令碼。
  • 執行指令碼。

必要條件

  • Google 帳戶
  • Google 雲端硬碟存取權

建立指令碼

  1. 前往 script.google.com/create,在 Apps Script 編輯器中建立新指令碼。
  2. 將指令碼編輯器的內容替換成下列程式碼:

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. 按一下「Save」(儲存) 圖示
  2. 按一下「Untitled project」(未命名的專案),輸入「Quickstart」,然後按一下「Rename」(重新命名)

設定指令碼

啟用 Google Drive Activity API

開啟 Apps Script 專案。

  1. 按一下「Editor」(編輯器) 圖示
  2. 按一下「Services」(服務) 旁的「Add」(新增服務) 圖示
  3. 選取「Drive Activity API」,然後按一下「Add」(新增)

執行範例

在 Apps Script 編輯器中,按一下「Run」(執行)

首次執行範例時,系統會提示您授予存取權:

  1. 按一下「Review permissions」(查看權限)
  2. 選擇帳戶。
  3. 按一下「Allow」(允許)

指令碼的執行記錄會顯示在視窗底部。

後續步驟