دليل سريع للبدء في Google Apps Script

كيفية إنشاء نص برمجي في "برمجة تطبيقات Google" يرسل طلبات إلى Google Drive Activity API

توضّح أدلة البدء السريع كيفية إعداد تطبيق وتشغيله لاستدعاء Google Workspace API. يستخدم دليل البدء السريع هذا طريقة مصادقة مبسطة مناسبة لبيئة الاختبار. بالنسبة إلى بيئة التشغيل الفعلي، ننصحك بالتعرّف على المصادقة والتفويض قبل اختيار بيانات اعتماد الوصول المناسبة لتطبيقك.

في "برمجة تطبيقات Google"، تستخدم أدلة البدء السريع في Google Workspace خدمات Google المتقدّمة لاستدعاء Google Workspace APIs والتعامل مع بعض تفاصيل عملية المصادقة والتفويض.

الأهداف

  • ضبط البيئة
  • إنشاء النص البرمجي وضبطه
  • تشغيل النص البرمجي

المتطلبات الأساسية

  • حساب Google
  • الوصول إلى Google Drive

إنشاء النص البرمجي

  1. أنشئ نصًا برمجيًا جديدًا في أداة تعديل النصوص البرمجية في برمجة التطبيقات من خلال الانتقال إلى script.google.com/create.
  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. انقر على "حفظ" .
  2. انقر على مشروع بلا عنوان، ثم اكتب دليل البدء السريع وانقر على إعادة التسمية.

ضبط النص البرمجي

تفعيل Google Drive Activity API

افتح مشروع "برمجة تطبيقات Google".

  1. انقر على المحرِّر .
  2. بجانب الخدمات، انقر على "إضافة خدمة" .
  3. اختَر Drive Activity API وانقر على إضافة.

تشغيل النموذج

في محرِّر "برمجة تطبيقات Google"، انقر على تشغيل.

في المرة الأولى التي تشغّل فيها النموذج، سيُطلب منك منح إذن الوصول:

  1. انقر على مراجعة الأذونات.
  2. اختر حسابًا.
  3. انقر على سماح.

يظهر سجلّ تنفيذ النص البرمجي في أسفل النافذة.

الخطوات التالية