I am trying to pull the list of guests for a calendar event and figure out each of the guests status (YES, NO, MAYBE) but ran into a problem with a user group. It just gives me the group name and a status of INVITED, but I need each guest in the user group and their invite status. Is this possible?
var calendarName="calendar name";
var calDate=new Date("04/02/2013");
function testCalendarEvents(){
var calendars = CalendarApp.getCalendarsByName(calendarName);
var events = calendars[0].getEventsForDay(calDate);
for(var c=0; c<events.length; c++){
Logger.log("Event "+c+": "+events[c].getTitle());
var guestList=events[c].getGuestList();
for(var d=0; guestList!=null && d<guestList.length; d++){
Logger.log("Guest "+d+": "+guestList[d].getEmail()+", Status: "+guestList[d].getGuestStatus());
}
}
}
This is not possible for DomainGroups presently, as there is no apps-script API support for drilling in to the status of group members. According to the documentation, you should be able to view those users and their status through the Calendar UI, assuming that you have appropriate permissions to view group members.
From the Enterprise Calendar FAQ:
Can I invite a mailing list from our corporate directory to a meeting?
Yes, you can invite any mailing list (group) in our corporate
directory to a meeting. Each member of the group will receive an email
invitation. Note, however, that the email invitation won't show all
the members of the group. Also, the group members will appear in the
invitation on each attendee's calendar only if you have permission to
view the group's member list.
If you are a Domain Administrator, you can use the GroupsManager service to retrieve the memberslist of a group. Unfortunately, within the context of a Calendar Event, that's about the only useful thing you can do. You cannot get the invitation status of individual users. I've modified your script to retrieve the member list:
function testCalendarEvents(){
var calendarName="david_bingham#mitel.com";
var calDate=new Date("04/01/2013");
var calendars = CalendarApp.getCalendarsByName(calendarName);
var events = calendars[0].getEventsForDay(calDate);
try {
var domainAdmin = false; // assume we aren't an admin in a domain
GroupsManager.getGroup("test");
domainAdmin = true; // we passed the test, so we ARE
}
catch (error) {
// We didn't pass the test... why not?
// Get "Service error: : Invalid request URI" if outside of an enterprise account
// Get "You do not have permission to perform that action." if not admin.
Logger.log("Not Domain Admin: "+error.message);
}
for(var c=0; c<events.length; c++){
Logger.log("Event "+c+": "+events[c].getTitle());
var guestList=events[c].getGuestList();
for(var d=0; guestList!=null && d<guestList.length; d++){
if (!domainAdmin) {
Logger.log("Guest "+d+": "+guestList[d].getEmail()+", Status: "+guestList[d].getGuestStatus());
}
else {
// Check if this guest is a group
var group = GroupsManager.getGroup(guestList[d].getEmail());
if (group) {
// getAllMembers() returns an array of email addresses
var groupMembers = group.getAllMembers();
for (var e in groupMembers) {
Logger.log("Guest "+d+"/"+e+": "+groupMembers[e]+
", Status: "+"UNKNOWN(group "+guestList[d].getEmail()+")");
}
}
else {
Logger.log("Guest "+d+": "+guestList[d].getEmail()+", Status: "+guestList[d].getGuestStatus());
}
}
}
}
}
Related
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.
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
2. Choose Admin SDK API
3. Click version drop-down, select reports_v1 and Add
SAMPLE SHEET
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:
Here's the Execution logs result for reference:
I need to find USER_ID of all the users in our current organization. We have Google Workspace - Basic plan and have 120+ users.
Please refer here for what USER_ID I'm talking about
https://developers.google.com/hangouts/chat/reference/message-formats/basic#messages_that_mention_specific_users
What is the easiest way to fetch the USER_ID? The solution can be something like where Google Sheet is updated with the new user and its USER_ID.
Answer
It is not possible to get this USER_ID of all the users in a specific domain. This is what the documentation says about this id:
To determine the USER_ID for a user, examine the sender field of the incoming message from the user.
Also, if you were to use the space.members.list method to get a membership (or a user) resource, they do not contain any id.
Work around
There is a sample code in the documentation that shows how to get all the users in a Google Workspace domain. I have modified it to get the user ids and finally create a Sheet with all the information.
Code
function listAllUsers() {
var pageToken;
var page;
var userList = []
do {
page = AdminDirectory.Users.list({
domain: 'example.com',
maxResults: 100,
pageToken: pageToken
});
var users = page.users;
if (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
userList.push([user.id, user.name.fullName, user.primaryEmail])
Logger.log('u:%s, %s (%s)', user.id, user.name.fullName, user.primaryEmail);
}
} else {
Logger.log('No users found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
var nusers = userList.length
var ss = SpreadsheetApp.create('usersList')
ss.getActiveSheet().getRange(1,1,nusers,3).setValues(userList)
console.log(ss.getUrl())
}
References
Admin SDK Directory Service: List all users
Chat API: Method spaces.members.list
Chat API: Resource Membership
Chat API: Resource User
I have users in our Gsuite domain who use several email aliases. I need to setup different email signatures depending on the sender email alias. Is there a way to do this without having to pay for external software?
Thanks folks!
Since you have tagged apps script you can create a script where you can use G Suite Admin SDK and Gmail API to retrieve all the users and then set up a signature for each alias of the user.
This is an example on Apps Script for users: list:
/**
* Lists users in a G Suite domain.
*/
function listUsers() {
var optionalArgs = {
customer: 'my_customer',
maxResults: 10,
orderBy: 'email'
};
var response = AdminDirectory.Users.list(optionalArgs);
var users = response.users;
if (users && users.length > 0) {
Logger.log('Users:');
for (i = 0; i < users.length; i++) {
var user = users[i];
Logger.log('%s (%s)', user.primaryEmail, user.name.fullName);
}
} else {
Logger.log('No users found.');
}
}
Using Users: list (from Admin SDK) you will get all the users on your domain, and from that you can use path or update (from Users.settings.sendAs) to create a signature.
Is it possible to get all domain email addresses (#example.com) to a variable? (the domain is of course connected with Gmail). I was searching and have found just ContactsApp class, which can give contacts from address books, but I need all company's email addresses.
To get user information like this for your entire domain, you need to use the Admin SDK Directory API. You will need to query Google Apps for your domain's users.
var optionalArgs = {"customer":"my_customer"},
response,
users = []
;
response = AdminDirectory.Users.list(optionalArgs);
users = response.users;
This will get you your first page of users, so you may need to create a loop to fetch all of them. You will need to understand the User resource, which is returned for each of your users. Both their primary domain email and their aliases can be found in this resource:
var email,
aliases
;
email = users[0].emails[0]; // if email.primary is true, email.address is the primary domain email for this user
aliases = users[0].aliases; // list of user's alias email addresses
More information can be found in the documentation
This is the function to get all the users email address list in appscript .
function listAllUsers1() {
var pageToken;
var page;
do {
page = AdminDirectory.Users.list({
domain: 'abc.com',
orderBy: 'givenName',
maxResults: 100,
pageToken: pageToken,
});
var users = page.users;
Logger.log(users);
if (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
Logger.log(user);
Logger.log('%s (%s)', user.name.fullName, user.primaryEmail);
}
} else {
Logger.log('No users found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
}
I know this is two-in-a-row, but they're different issues, so I thought it best to split them up. I'm getting problems with accepting invitations on behalf of students. I'm a super admin within the domain, and it works through the API explorer, but not through Google Apps Script's Advanced Service for Classroom. And before you ask, yes, I did enable Classroom in both the Advanced Services menu and Developer's Console.
Here's the block of code:
function invitations() {
var classList = Classroom.Courses.list().courses;
for (i in classList) {
var classId = classList[i].id;
var teacher = Classroom.Courses.Teachers.get(classId, classList[i].ownerId).profile.name.fullName;
if (teacher == 'FULL_NAME_GOES_HERE') { // I had entered my full name here as a string. If testing, you can replace with your own
var title = Classroom.Courses.get(classId).name;
Logger.log('Teacher: %s, Title: %ss',teacher,title);
var list = Classroom.Invitations.list({'courseId':classId}).invitations;
if (list) {
Logger.log('list');
for (k in list) {
var invitation = list[k];
var student = invitation.userId;
var id = invitation.id;
var response = Classroom.Invitations.accept({id:id});
Logger.log('ID: %s',id);
}
}
}
}
}
The id being passed to Classroom.Invitations.accept returns an error "Requested entity was not found." but when I input the same ID into the API explorer, it returns 200 - OK. Is this a problem with the Authorization scope? I have authorization to manage Classroom rosters, which I believe this falls under.