Google Workspace

Google Workspace: Export list of all groups using Google App Script

Google Workspace doesn’t have an option to export list of groups. This can be quite frustrating if you have lot of groups. Using Google Apps Script we can export all groups for a particular domain to a Google Sheet.

Created a Google Sheet and add below code to Google App Script. Ensure you add Admin API library and account has admin access.

function getGroups() {
var groups = [];
var options = {
domain: "mkerala.com", // Google Apps domain name
customer: "my_customer",
maxResults: 100,
projection: "basic", // Fetch basic details of users
viewType: "domain_public",
orderBy: "email" // Sort results by users
}
do {
var response = AdminDirectory.Groups.list(options);
response.groups.forEach(function(group) {
groups.push([group.email]);
});
// For domains with many users, the results are paged
if (response.nextPageToken) {
options.pageToken = response.nextPageToken;
}
} while (response.nextPageToken);
// Insert data in a spreadsheet
var thespreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = thespreadsheet.getActiveSheet();;
sheet.getRange(2,1,groups.length, groups[0].length).setValues(groups);
}

4 thoughts on “Google Workspace: Export list of all groups using Google App Script

  • Martin Kramarčík

    TypeError: Cannot read properties of null (reading ‘getActiveSheet’)
    getGroups @ Kód.gs:23

    Reply
    • mkerala

      The error message suggests that you have created a standalone script that is not bound to a spreadsheet. You need a Google Sheet attached to the script.

      Reply
  • Buenas tardes.
    He intentado utilizar el codigo pero me da este error:
    GoogleJsonResponseException: API call to directory.groups.list failed with error: Not Authorized to access this resource/api
    getGroups @ Código.gs:12
    Me podríais ayudar.

    Un saludo

    Reply
    • Add admindirectory api in Google Apps script library.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *