How to pull deleted, archived, suspended users data to Google sheets from Admin SDK >> Reports API using Appscript

535 Views Asked by At

How to import deleted, archived, suspended USERS data (user email, deleted date, org unit path, archive date, suspended date) to Google sheets from Admin SDK >> Reports API using Appscript. Thanks.

1

There are 1 best solutions below

4
On

Welcome to Stackoverflow. Please be mindful to always include any research/sample scripts you've done on your end when posting questions as much as possible. Please see the guidelines about How do I ask a good question? as this is an integral part in this community.

RECOMMENDATION:

You can use this sample script below as reference. This script is based to a sample from Google Apps Script Quickstart

function listUsers() {
  
    var sheet = SpreadsheetApp.getActive().getActiveSheet();
    var values = [];
    var userKey = 'all';
    var applicationName = 'admin';
    var optionalArgs = {
      maxResults: 100
    };

    var response = AdminReports.Activities.list(userKey, applicationName, optionalArgs);
    var activities = response.items;
    if (activities && activities.length > 0) {
      Logger.log('REPORTS:');
      for (i = 0; i < activities.length; i++) {
        var activity = activities[i];

        //ONLY GET DATA IF ACTION IS EITHER "SUSPEND_USER", "DELETE_USER", or "ARCHIVE_USER"
        if(activity.events[0].name == "SUSPEND_USER" || activity.events[0].name == "DELETE_USER" || activity.events[0].name == "ARCHIVE_USER"){
            Logger.log('%s: %s (%s)', activity.id.time, activity.events[0].parameters[0].value,
            activity.events[0].name);
            //RETRIEVES THE TIMESTAMP, USER'S EMAIL, & THE EVENT NAME THAT WAS PERFORMED TO THE USER
            values = [[activity.id.time, activity.events[0].parameters[0].value,activity.events[0].name]]; 

            //SET THE DATA TO SHEET
            var lrow = sheet.getLastRow()+1;
            sheet.getRange("A"+lrow+":C"+lrow).setValues(values); 
        }

      }
    } else {
      Logger.log('No reports found.');
    }
}

NOTE:

Before you run the script, you need to add the AdminReports API on your Apps Script editor:

1. Click Services plus icon

enter image description here

2. Choose Admin SDK API

enter image description here

3. Click version drop-down, select reports_v1 and Add

enter image description here

SAMPLE SHEET

enter image description here

RESULT

After running the sample script, the timestamp when the action was made, user email address and the action made (Suspended/Deleted/Archived) will be added to the sheet:

enter image description here

Here's the Execution logs result for reference:

enter image description here