Question

I have a spreadsheet with 6 columns of email addresses that update automatically based on user input and on age. Each column corresponds to a distinct Google Group.

I would like to auto-populate the Google Group membership based on the dynamic values in each of the spreadsheet columns. For example -- either on edit or on a daily timer -- a Google Script would read the list of email addresses in Column A and update the corresponding Google Group.

Ideally, the script would call the current Google Group membership, compare it to the list of email addresses in Column A, delete from the Group any addresses that no longer appear in Column A, and add to the Group any addresses that appear in Column A but are not currently in the Group.

This may be asking for too much. So alternatively, I can imagine the script deleting all members from the Group, then adding all the email addresses in Column A to the Group. While this would accomplish the same objective (as far as I can see), I wonder whether there would be some history lost or some similar side effect to erasing all members on a routine basis. Thus, I prefer the comparison approach first described.

In the forum, I can find this discussion, with a reply from Serge insas: Add emailadress to Spreadsheet, auto add the email to a Google group

When I run this code in my script editor, replacing 'testgroup@domain.com' with the email address for my Google Group, I get the following error:

Service error: : Invalid request URI

with a reference to line 10, which is

var group = GroupsManager.getGroup('testgroup@domain.com');

Getting past that error is 1 problem. Once I get past that error, I need to learn how to either compare Group membership to the spreadsheet list and/or delete all and reimport all.

Any help on these 2 problems is greatly appreciated!


Edit 1 (after Mogsdad's confirmation that I cannot use GAS API to update Google Group membership)

Okay, so plan B for me is not a hack-around, because I'm too novice to feel comfortable going there. Plan B is instead to write a script that will compare the spreadsheet user list to the Google Group user list and tell me which users need to be manually added to / removed from the Google Group user list. I've adapted the code you provided in the post you linked me to.

    function compareGroupMembers() {  
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Groups');
      var rows = sheet.getDataRange();
      var values = rows.getValues();
      var sheetUsers = [];
    Logger.log(values)

    // Get user list from sheet
      for (var i = 1; i < values.length; i++) {
        var row = values[i];
        sheetUsers.push(row[0]); // build array of users
      }
      Logger.log(sheetUsers)

    // Get user list from group
      var group = GroupsApp.getGroupByEmail("mygroup@googlegroups.com");
      var groupUsers = group.getUsers().join(',').split(',');
      Logger.log(groupUsers)

    // Compare user lists and define users to Add and Remove
    //*****This is where I need direction. This code is just a placeholder.*****
      var usersToAdd = sheetUsers.uniqueFrom(groupUsers);
      var usersToRemove = groupUsers.uniqueFrom(sheetUsers);
      var addErrors = 0; // Counter for exceptions

    //Then I would like usersToAdd and usersToRemove to be listed in a separate sheet
    //Because I have six groups, I'm hoping the new sheet can be organized by
    //Col A is Google Group email
    //Col B is user email
    //Col C is "add" or "delete"

    };

Is it possible to create an output that tells me which members to manually add (i.e. those that are on the sheet list but not on the group list) and those to manually delete (i.e. those who are on the group list but are not on the sheet list)?

Additionally, can I have the same script do the same thing for six different groups? (in my sheet the group email address is in row 1, and the member email addresses are directly under it, for all 6 columns) If it is possible, then I would want the output to specify which group the user must be manually added to or deleted from.

Thanks for your help!

Was it helpful?

Solution

You should be able to adapt the code from How to add permission to Google site using spreadsheet?, replacing Site.getViewers, Site.addViewer() and Site.removeViewer() with DomainGroup.getAllMembers(), DomainGroup.addMember() and DomainGroup.removeMember() respectively, and adjusting other details appropriately.

WRT your error... make sure you haven't confused group types. See Domain groups versus Google groups.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top