คู่มือเริ่มใช้งาน Google Apps Script ฉบับย่อ

การเริ่มต้นอย่างรวดเร็วอธิบายวิธีตั้งค่าและเรียกใช้แอปที่เรียกใช้ Google Workspace API

การเริ่มต้น Google Workspace อย่างรวดเร็วจะใช้ไลบรารีของไคลเอ็นต์ API ในการจัดการรายละเอียดบางอย่างของขั้นตอนการตรวจสอบสิทธิ์และการให้สิทธิ์ เราขอแนะนำให้คุณใช้ไลบรารีของไคลเอ็นต์สำหรับแอปของคุณเอง การเริ่มต้นอย่างรวดเร็วนี้ใช้วิธีการตรวจสอบสิทธิ์แบบง่ายที่เหมาะสำหรับสภาพแวดล้อมการทดสอบ สำหรับสภาพแวดล้อมของเวอร์ชันที่ใช้งานจริง เราขอแนะนำให้เรียนรู้เกี่ยวกับการตรวจสอบสิทธิ์และการให้สิทธิ์ก่อนที่จะเลือกข้อมูลเข้าสู่ระบบที่เหมาะสำหรับแอปของคุณ

สร้าง Google Apps Script ที่ส่งคำขอไปยัง Google Drive Activity API

วัตถุประสงค์

  • เขียนสคริปต์
  • เปิดใช้ API กิจกรรม Google ไดรฟ์
  • เรียกใช้ตัวอย่าง

ข้อกำหนดเบื้องต้น

  • บัญชี Google
  • การเข้าถึง Google ไดรฟ์

สร้างสคริปต์

  1. สร้างสคริปต์ใหม่โดยไปที่ script.google.com/create
  2. แทนที่เนื้อหาของโปรแกรมแก้ไขสคริปต์ด้วยโค้ดต่อไปนี้

ไดรฟ์/กิจกรรม-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. คลิกบันทึก
  2. คลิกโปรเจ็กต์ที่ไม่มีชื่อ พิมพ์ Quickstart แล้วคลิกเปลี่ยนชื่อ

เปิดใช้ API กิจกรรม Google ไดรฟ์

  1. เปิดโปรเจ็กต์ Apps Script
  2. คลิกตัดต่อวิดีโอ
  3. ถัดจากบริการ ให้คลิกเพิ่มบริการ
  4. เลือก Drive Activity API แล้วคลิก Add

เรียกใช้ตัวอย่าง

คลิกเรียกใช้ในเครื่องมือแก้ไข Apps Script

ครั้งแรกที่เรียกใช้ตัวอย่าง ระบบจะเตือนให้คุณอนุญาตการเข้าถึง

  1. คลิกตรวจสอบสิทธิ์
  2. เลือกบัญชี
  3. คลิกอนุญาต

บันทึกการดำเนินการของสคริปต์จะปรากฏที่ด้านล่างของหน้าต่าง

ขั้นตอนถัดไป