0

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 Answers1

0

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

SputnikDrunk2
  • 3,398
  • 1
  • 5
  • 17
  • Wow awesome, it almost works. Only problem is, it is listing only today's events. – Mohammed Zaid May 21 '21 at 09:58
  • If I remove the max result argument then I'm getting around 35 results which is only for last 2 days. – Mohammed Zaid May 21 '21 at 10:48
  • Since this issue is now specific than to what was posted about "How to pull deleted, archived, suspended users data to Google sheets from Admin SDK", please post a new question specific for the issue. – SputnikDrunk2 May 21 '21 at 20:50
  • Done, have posted a new question, link below for your reference: https://stackoverflow.com/questions/67654227/having-issues-with-the-appscript-pulling-data-from-google-workspace-using-report – Mohammed Zaid May 22 '21 at 21:15