Hướng dẫn bắt đầu nhanh về Google Apps Script

Hướng dẫn bắt đầu nhanh giải thích cách thiết lập và chạy một ứng dụng gọi API Google Workspace.

Tính năng bắt đầu nhanh của Google Workspace sử dụng thư viện ứng dụng API để xử lý một số chi tiết trong quy trình xác thực và uỷ quyền. Bạn nên sử dụng các thư viện ứng dụng cho ứng dụng của mình. Bước bắt đầu nhanh này sử dụng phương pháp xác thực đơn giản, phù hợp với môi trường kiểm thử. Đối với môi trường phát hành chính thức, bạn nên tìm hiểu về xác thực và uỷ quyền trước khi chọn thông tin xác thực truy cập phù hợp với ứng dụng của mình.

Tạo một Google Apps Script để gửi yêu cầu đến Google Drive Activity API.

Mục tiêu

  • Tạo tập lệnh.
  • Bật Google Drive Activity API.
  • Chạy mẫu.

Điều kiện tiên quyết

  • Tài khoản Google
  • Quyền truy cập vào Google Drive

Tạo tập lệnh

  1. Tạo tập lệnh mới bằng cách truy cập vào script.google.com/create.
  2. Thay thế nội dung của trình chỉnh sửa tập lệnh bằng mã sau:

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. Nhấp vào biểu tượng Lưu .
  2. Nhấp vào Dự án không có tiêu đề, nhập Bắt đầu nhanh rồi nhấp vào Đổi tên.

Bật Google Drive Activity API

  1. Mở dự án Apps Script.
  2. Nhấp vào biểu tượng Trình chỉnh sửa .
  3. Bên cạnh phần Dịch vụ, hãy nhấp vào Thêm dịch vụ .
  4. Chọn API Hoạt động trên Drive rồi nhấp vào Thêm.

Chạy mẫu

Trong trình chỉnh sửa Apps Script, hãy nhấp vào Run (Chạy).

Lần đầu tiên bạn chạy mẫu, mẫu sẽ nhắc bạn cấp quyền truy cập:

  1. Nhấp vào Xem xét các quyền.
  2. Chọn một tài khoản.
  3. Nhấp vào Cho phép.

Nhật ký thực thi của tập lệnh sẽ xuất hiện ở cuối cửa sổ.

Các bước tiếp theo