Google Apps Script でGoogle Driveの更新履歴を取得する

Google Apps ScriptでGoogle Driveの更新履歴を取得するコードを作成しました
使い方は以下関数testのように、getFilesAfterSpecifiedDateを呼んで、確認対象のフォルダIDをいつから検索するのかだけ指定すれば実行出来ます

# 利用スコープについて
本スクリプトのappscript.jsonは以下の通りです

```json:appscript.json
{
  "timeZone": "Asia/Tokyo",
  "dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "DriveActivity",
        "version": "v2",
        "serviceId": "driveactivity"
      },
      {
        "userSymbol": "People",
        "version": "v1",
        "serviceId": "peopleapi"
      }
    ]
  },
  "oauthScopes": [
    "https://www.googleapis.com/auth/contacts",
    "https://www.googleapis.com/auth/contacts.readonly",
    "https://www.googleapis.com/auth/directory.readonly",
    "https://www.googleapis.com/auth/user.addresses.read",
    "https://www.googleapis.com/auth/user.birthday.read",
    "https://www.googleapis.com/auth/user.emails.read",
    "https://www.googleapis.com/auth/user.gender.read",
    "https://www.googleapis.com/auth/user.organization.read",
    "https://www.googleapis.com/auth/user.phonenumbers.read",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/userinfo.profile",
    "https://www.googleapis.com/auth/drive.activity.readonly",
    "https://www.googleapis.com/auth/contacts.readonly"
  ],
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}
```

# 追加するサービスについて
Google Apps scriptのエディタで以下2つのサービスを追加します

- DriveActivity v2
- People v1

# 実際のコードについて
以下に掲載します

```javascript:app.gs
function test() {

  // 検索条件指定 getFilesAfterSpecifiedDate("対象フォルダID","検索開始日時")
  const results = DriveActivityService.getFilesAfterSpecifiedDate(
    "",
    new Date("2021/01/01")
  )

  // 検索結果確認
  results.forEach(result => {
    console.log(result);
  })
}

/**
 * アクティビティ変換後のファイル形式
 * @typedef {Object} FileModel
 * @property {string} fileId
 * @property {Date} lastUpdate
 * @property {string} fileType
 * @property {string} title
 * @property {string} url
 * @property {string} actor
 * @property {string} actorAddress
 */

class DriveActivityService {

  /**
   * 最終更新日が指定日より後のファイル一覧を出力する
   * @param {string} folderId
   * @param {Date} specifiedDate
   * @returns {FileModel[]}
   */
  static getFilesAfterSpecifiedDate(folderId, specifiedDate) {
    const { activities } = DriveActivity.Activity.query({
      ancestorName: `items/${folderId}`,
      filter: `time > ${specifiedDate.getTime()}`,
    })
    return activities.map(e => this._getFileModelFromActivity(e));
  }

  /**
   * ActivityよりModelを生成する
   * @param {Driveactivity_v2.Driveactivity.V2.Schema.DriveActivity} activity
   * @returns {FileModel}
   */
  static _getFileModelFromActivity(activity) {
    const item = activity.targets[0].driveItem;
    const knownUser = activity.actors[0].user.knownUser.personName;
    const fileId = item.name.replace("items/", "");
    const { url, fileType } = this._getMIMETypeAndUrlFromENUM(item.mimeType, fileId);

    const user = this._getUserNameFromPeopleId(knownUser);
    return {
      fileId: fileId,
      fileType: fileType,
      title: item.title,
      url: url,
      actor: user.actor,
      actorAddress: user.actorAddress,
      lastUpdate: new Date(activity.timestamp),
    }
  }

  /**
   * PeopleIDを元にユーザー名を返す
   * @param {string} peopleId
   * @returns {{actor:string,actorAddress:string}}
   */
  static _getUserNameFromPeopleId(peopleId) {

    const people = People.People.getBatchGet({
      resourceNames: [peopleId],
      personFields: "names,emailAddresses"
    });
    if (people.responses[0].person.names === undefined) {
      return { actor: "情報なし", actorAddress: "情報なし" };
    } else {
      return {
        actor: people.responses[0].person.names[0].displayName,
        actorAddress: people.responses[0].person.emailAddresses[0].value,
      }
    }
  }

  /**
   * MIMETypeをファイル形式に変換とURLを生成する
   * @param {string} rowEnum
   * @param {string} fileId
   * @returns {{url:string,fileType:string}}
   */
  static _getMIMETypeAndUrlFromENUM(rowEnum, fileId) {
    if (rowEnum === MimeType.GOOGLE_DOCS) {
      return { url: `https://docs.google.com/document/d/${fileId}`, fileType: "Google Document", };
    } else if (rowEnum === MimeType.GOOGLE_FORMS) {
      return { url: `https://docs.google.com/forms/d/${fileId}`, fileType: "Google Form" };
    } else if (rowEnum === MimeType.GOOGLE_SHEETS) {
      return { url: `https://docs.google.com/spreadsheets/d/${fileId}`, fileType: "Google Spreadsheet" };
    } else if (rowEnum === MimeType.GOOGLE_SITES) {
      return { url: `https://sites.google.com/d/${fileId}`, fileType: "Google Site" };
    } else if (rowEnum === MimeType.GOOGLE_SLIDES) {
      return { url: `https://docs.google.com/presentation/d/${fileId}`, fileType: "Google Slide" };
    } else if (rowEnum === MimeType.FOLDER) {
      return { url: `https://drive.google.com/drive/folders/${fileId}`, fileType: "Folder" };
    } else if (rowEnum === MimeType.SHORTCUT) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "SHORTCUT" };
    } else if (rowEnum === MimeType.BMP) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "BMP" };
    } else if (rowEnum === MimeType.GIF) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "GIF" };
    } else if (rowEnum === MimeType.JPEG) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "JPEG" };
    } else if (rowEnum === MimeType.PNG) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "PNG" };
    } else if (rowEnum === MimeType.SVG) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "SVG" };
    } else if (rowEnum === MimeType.PDF) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "PDF" };
    } else if (rowEnum === MimeType.CSV) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "CSV" };
    } else if (rowEnum === MimeType.PLAIN_TEXT) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Text" };
    } else if (rowEnum === MimeType.RTF) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Ritch Text" };
    } else if (rowEnum === MimeType.MICROSOFT_EXCEL) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Microsoft Excel" };
    } else if (rowEnum === MimeType.MICROSOFT_EXCEL_LEGACY) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Microsoft Excel" };
    } else if (rowEnum === MimeType.MICROSOFT_POWERPOINT) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Microsoft Powerpoint" };
    } else if (rowEnum === MimeType.MICROSOFT_POWERPOINT_LEGACY) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Microsoft Powerpoint" };
    } else if (rowEnum === MimeType.MICROSOFT_WORD) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Microsoft Word" };
    } else if (rowEnum === MimeType.MICROSOFT_WORD_LEGACY) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Microsoft Word" };
    } else if (rowEnum === MimeType.ZIP) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Zip" };
    } else if (rowEnum === MimeType.GOOGLE_APPS_SCRIPT) {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "Google Apps Script" };
    } else {
      return { url: `https://drive.google.com/file/d/${fileId}`, fileType: "その他ファイル" };
    }
  }
}
```

# 解決していない問題
MimeType.ZIPだけ何故かうまく判定してくれないので、そのうちファイル形式ZIPが判定出来るようになったら更新します

以上