I want to create New Google Group Using App Script

2,504 views
Skip to first unread message

Mr. Ahmed

unread,
Feb 2, 2022, 6:18:34 PM2/2/22
to Google Apps Script Community
Hello.
I want to create New Google Group (Without any Member) Using App Script.
What's the solution, let me know how to do that.

Braja Patnaik

unread,
Feb 3, 2022, 6:14:23 AM2/3/22
to Google Apps Script Community
This could be possible if you have a google workspace account. Pls check the below link.

Nabil Hassan

unread,
Apr 17, 2022, 6:07:44 PM4/17/22
to Google Apps Script Community
Hi All,

Can someone share the working code for this in which I can replace the variable values and get the work done?

I've spent days to figure this out and now reached a point to give up.

Any help will be much appreciated.

Regards,

Clark Lind

unread,
Apr 18, 2022, 8:51:14 AM4/18/22
to Google Apps Script Community
Normal 'Gmail' users do not have access to do this. You have to have a Workspace account/domain.

Nabil Hassan

unread,
Apr 19, 2022, 4:43:49 AM4/19/22
to Google Apps Script Community
Hi,

Yes, I'm doing it from my Company's domain.

Here is my code:

function domainGroupChecker()
{
var ss = SpreadsheetApp.openById(sheetID);
var sheet = ss.getSheetByName(sheetName);
var lastRow = sheet.getRange('AB3').getNextDataCell(SpreadsheetApp.Direction.DOWN).getLastRow();
console.log("Total Domain Groups to be Checked: "+lastRow);
for(i=3; i<=lastRow; i++)
{
var groupEmail = sheet.getRange(i,28).getValue();
console.log("Got "+groupEmail+" at row number "+i);
try
{
var group = GroupsApp.getGroupByEmail(groupEmail);
console.log("Group "+groupEmail+" Found!");
sheet.getRange(i,29).setValue('Group Found');
}
catch(e)
{
//Code to create the group
sheet.getRange(i,29).setValue("Group Created");
}
}
}

I just need to write that part where if the group is not found then it automatically creates the group.

Any help is appreciated

Regards,

Clark Lind

unread,
Apr 19, 2022, 8:25:07 AM4/19/22
to Google Apps Script Community
Ok good. I'm not an Admin, but you should be able to use the Admin SDK/Directory API (groups.insert).  
Make sure the browser you are using is logged in as the Admin account so your credentials are already in the browser.
Click that link and go to the page
On the right side should be a form for trying the API.  Try entering an email for a group that needs to be created, and uncheck the API credential since you want to be sure it works with OAuth 2. Then
press execute.
adminSDK2.jpg
If you get a green 200 response below the execute button, then the group was created successfully.

Assuming the above worked --
If you don't have the Admin SDK service in your project yet,
in your Apps script project, left-side menu, go to Services -> Add a service. 
Select the Admin SDK:  
adminSDK.jpg

Once the service has been added, 
the actual code should be fairly easy.  You may have to play around with this since I can't test it, but something like the below should work. 
This isn't very efficient, and may be able to be refactored to do a single batch request. But play with this and see if any of this works for you:

catch(e)
    {
    //Code to create the group
    var emailObj = {
        "email": groupEmail
        }
     groups.insert( emailObj )
    sheet.getRange(i,29).setValue("Group Created");
    }

Or:

catch(e)
    {
    //Code to create the group
     groups.insert( {"email": groupEmail})
    sheet.getRange(i,29).setValue("Group Created");
    }




Clark Lind

unread,
Apr 19, 2022, 8:38:22 AM4/19/22
to Google Apps Script Community
Use this format instead:

catch(e)
    {
    //Code to create the group
    var emailObj = {
           "email":  "email": groupEmail,
            "name": "My New Group Name",
           "description": "This is the new group."

Nabil Hassan

unread,
Apr 19, 2022, 10:04:44 AM4/19/22
to Google Apps Script Community
April 19 2022.pngSo I tried the first step.

I got a 200 in green.

then I modified the code to insert what you sent and this is what I'm getting (attached the screenshot) 

This is the code:

function domainGroupChecker()
{
var ss = SpreadsheetApp.openById(sheetID);
var sheet = ss.getSheetByName(sheetName);
var lastRow = sheet.getRange('AB3').getNextDataCell(SpreadsheetApp.Direction.DOWN).getLastRow();
console.log("Total Domain Groups to be Checked: "+lastRow);
for(i=3; i<=lastRow; i++)
{
var groupEmail = sheet.getRange(i,28).getValue();
console.log("Got "+groupEmail+" at row number "+i);
try
{
var group = GroupsApp.getGroupByEmail(groupEmail);
console.log("Group "+groupEmail+" Found!");
sheet.getRange(i,29).setValue('Group Found');
}
catch(e)
{
//Code to create the group
console.log(group);
var emailObj =
{
"email": groupEmail,
//"name": "My New Group Name",
"description": "Welcome to the group"
}
group.insert({emailObj});
sheet.getRange(i,29).setValue("Group Created");
}
}
}

Nabil Hassan

unread,
Apr 19, 2022, 10:08:18 AM4/19/22
to Google Apps Script Community
April 19 2022_1.png

Nabil Hassan

unread,
Apr 19, 2022, 10:34:30 AM4/19/22
to Google Apps Script Community
I found a working code on StackOverFlow, here is the link.

I tried to replicate the same thing with my code and here's how it looks.. and I'm stuck with how to get the key

function domainGroupChecker()
{
var ss = SpreadsheetApp.openById(sheetID);
var sheet = ss.getSheetByName(sheetName);
var lastRow = sheet.getRange('AB3').getNextDataCell(SpreadsheetApp.Direction.DOWN).getLastRow();
console.log("Total Domain Groups to be Checked: "+lastRow);
for(i=3; i<=lastRow; i++)
{
var groupEmail = sheet.getRange(i,28).getValue();
console.log("Got "+groupEmail+" at row number "+i);
try
{
var group = GroupsApp.getGroupByEmail(groupEmail);
console.log("Group "+groupEmail+" Found!");
sheet.getRange(i,29).setValue('Group Found');
}
catch(e)
{
//Code to create the group
var groupName = sheet.getRange(i,40);
var requestBody = {email: groupEmail, name: groupName, description: "Welcome to the Group!"};
var fetchArgs = googleOAuth_("Groups", scope);
fetchArgs.method = "POST";
fetchArgs.contentType = "application/json";
fetchArgs.payload = JSON.stringify(requestBody);
fetchArgs.muteHttpExceptions = true;
UrlFetchApp.fetch(url, fetchArgs);
sheet.getRange(i,29).setValue("Group Created");
}
}
}


Clark Lind

unread,
Apr 19, 2022, 11:33:35 PM4/19/22
to Google Apps Script Community
I think I left off something..    try  instead of groups.insert,  try   AdminDirectory.groups.insert(  {emailObj}  )   (group(s) is with an "s"). 

Laurie Nason

unread,
Apr 20, 2022, 8:46:43 AM4/20/22
to google-apps-sc...@googlegroups.com
var emailObj = {
"name": "Lauries Group Name",
"description": "This is Laurie's new group."
}
var newGroup = AdminDirectory.Groups.insert( emailObj );

Yep - just tried that, thanks - works great. Created the group - now to work out all the other options I want to include and indeed, if I can even set some of them via appsscript.

Laurie

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/1fc394bc-f288-453b-9270-c9a8723bc016n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages