I wrote a google apps script which fetches the G Suite (google apps) users from the AdminDirectory API
As on output, i get the domain name in front of every user (used replace to extract domain name from each user email id).
what i want to do-:
1. Count the number of users on each domain in one column, so end result should look like this-:
Column - I (exaxple)
domainA.com = 120 users
domainB.com = 28 users
etc....
Any help is appreciated.
I think there is probably a way to shorten this code, but something like this will work. You will have to figure out how to read the correct columns in and output to the correct columns, but hopefully, this will help out:
var emails = [
'someone#testA.com',
'someone#testB.com',
'someone#testA.com',
'someoneelse#testA.com'
];
function countDomains() {
var domainCounts = {};
var domains = emails.map(function(domain) {return domain.split('#')[1];});
domains.forEach(function(each) {
if(domainCounts.hasOwnProperty(each)) {
domainCounts[each]++;
} else {
domainCounts[each] = 1;
}
});
Logger.log(domainCounts);
}
Related
I'm working with a google sheet that contains a column of unique project numbers followed by a column with the equipment description used in that project. I have set up my code to send an automatic email with this information to a list of recipients. What I want to do that I don't know how to is what code can I use that will read the project number column and when it finds two project numbers exactly the same, it will group the equipment together into a list that will be sent in that automatic email. I know the list can be a variable but I just don't know how to make it look for spa project numbers to join the equipment together without changing anything in the google sheet. Thank y'all!
I'm agree with Cooper. To build an object is a most obvious way to get unique values (ID, etc) from any list:
var data = [
[1,'a'],
[2,'b'],
[3,'c'],
[1,'d']
]
var obj = {};
data.forEach(x => {
try { obj[x[0]].push(x[1]) } // try to add the value to an existed array
catch(e) { obj[x[0]] = [x[1]] } // or make an array if there is no existed one
});
console.log(obj); // out: [ {1:[a,d]}, {2:[b]}, {3:[c]} ]
var id = 1
console.log('All the values from ID '+ id + ': ' + obj[id].join(', ')); // out: a, d
I have a C# Console application that uses EWS (Exchange Web Services) to impersonate a user and I create or update his current Contacts list.
In order to determine if I have to create or update his list, I first need to search his existing Contacts for a particular domain name like so:
private static IEnumerable<Contact> GetExistingContacts(ExchangeService service)
{
var domainToFilterOn = "#contoso.com";
SearchFilter sfSearch = new SearchFilter.ContainsSubstring(ContactSchema.EmailAddress1, domainToFilterOn);
FindItemsResults<Item> contacts = service.FindItems(WellKnownFolderName.Contacts, sfSearch, new ItemView(int.MaxValue));
var results = contacts.Cast<Contact>().ToList();
return results;
}
The problem I’m facing is that the method GetExistingContacts() yields 0 results for the given domain name which is wrong since I know I have a bunch of Contacts holding the #contoso.com domain name inside the EmailAddre1 field.
After a little bit of digging and testing, I finally figure out why the method wasn’t returning any results and the reason was because the email addresses are stored in the Exchange distinguished name instead of the SMTP format.
To further my investigation, I created a few new Contacts with fake/non-existing #contoso.com domain name like: test#contoso.com, gazou#contoso.com, etc.
To my surprise, the GetExistingContacts() method started to return these fake Contacts.
The conclusion is that whenever I create new Contacts that have resolvable email addresses, then these Contacts are stored using the Exchange distinguished name but when I create new Contacts that have non-resolvable email addresses, then these Contacts are stored as SMTP (which are returned by my GetExistingContacts() method).
How do I start fixing this?
Is my search method wrong? Is there another way to search inside the EmailAddress1 field?
Meanwhile, I managed to find a workaround using the .Load() method of the Contact object but this workaround seems ugly and costly in terms of execution time.
I basically get all Contacts, loop and call the Load() method, then add them to a List() and make a Linq query to filter the results. If my user has 800 Contacts, that takes a long time to Load() everyone of them.
Here’s the example:
private static IEnumerable<Contact> GetCurrentContacts(ExchangeService service)
{
var contacts = new List<Contact>();
var data = service.FindItems(WellKnownFolderName.Contacts, new ItemView(int.MaxValue));
foreach (var item in data.Items)
{
if (item is Contact)
{
item.Load();
contacts.Add(item as Contact);
}
}
var result = contacts.Where(x => x.EmailAddresses[EmailAddressKey.EmailAddress1].Address.Contains("#contoso.com")).ToList();
return result;
}
Needless to say, I don't think that is the correct approach although it works.
Another alternative I’ve tried was to force the RoutingType to SMTP thinking it would create the new Contact in the SMTP format as opposed to the Exchange distinguished name but unfortunately, the email address still gets stored in the Exchange distinguished name disregarding the fact that I forced the RoutingType like so:
var email = new EmailAddress();
email.Address = "goodemail#contoso.com";
email.RoutingType = "SMTP";
Contact contact = new Contact(service);
contact.EmailAddresses[EmailAddressKey.EmailAddress1] = email;
...
contact.Save();
If anyone can help me shed some light on this, that would be great!
Thanks in advance
If the SMTP address your trying to use for a Contact is visible (or resolvable) in the Global address list then the X500 address of Directory entry will be used to track the Contact to the Directory entry (this is by design). If you want to return the SMTP address instead of the X500 address when you retrieve the contacts all you need to do is make use you do a GetItem or Load in the Managed API on the contact or contacts in question if you have multiple contact use LoadPropertiesFromItems https://blogs.msdn.microsoft.com/exchangedev/2010/03/16/loading-properties-for-multiple-items-with-one-call-to-exchange-web-services/
You can override this behavior by setting the extended properties on the contacts directly see https://social.technet.microsoft.com/Forums/exchange/en-US/2b375c56-bee1-4d88-b638-f95649ef964a/use-ews-create-a-contact-which-has-a-same-email-address-in-gal-it-will-show-up-with-x500-formatting?forum=exchangesvrdevelopment but I would recommended you stay with the default behavior you can make you code more efficient using batch Loads.
Thank you #Glen Scales, the LoadPropertiesForItems() helped.
This is the final result if anyone cares:
private static IEnumerable<Contact> GetExistingContacts(ExchangeService service)
{
var contacts = new List<Contact>();
var filterContactsOnDomain = "#contoso.com";
var data = service.FindItems(WellKnownFolderName.Contacts, new ItemView(int.MaxValue));
if (data.TotalCount > 0)
{
service.LoadPropertiesForItems(data, new PropertySet(ContactSchema.EmailAddress1));
foreach (var item in data.Items)
{
if (item is Contact)
{
contacts.Add(item as Contact);
}
}
}
var result = contacts.Where(x => x.EmailAddresses[EmailAddressKey.EmailAddress1].Address.ToLower().Contains(filterContactsOnDomain.ToLower())).ToList();
return result;
}
The only thing I dislike is having to get all Contacts (800 of them), load the EmailAddress1 field for all 800 of them, loop and add those 800 Contacts to a list and then, filter on that list...
I guess it would've been nice to have the ability to search (or filter) directly on an X500 email address format thus not having to fetch all 800 Contacts.
Oh well...
Thanks again #Glen Scales
Good Evening! I'm Aaron Ayres, an 8th grade mathematics teacher at Noblesville West Middle School in Noblesville, Indiana. Recently for a school-wide incentive my school is using Google Drive to share a "pass" that grants nominated students access to rewards and other treats during the month they are nominated. We typically have about 80 students who are nominated on a monthly basis for their hard work - each educator in the building nominates one student each month based on multiple criteria.
Instead of having to manually go through my folder and un-share the individual passes at the end of the month, I have been researching for a more efficient method of allowing the passes to expire. I came across Amit Argawal's Auto-expire script (http://www.labnol.org/internet/auto-expire-google-drive-links/27509/) and I think it could potentially work but it only expires one file at a time. Is there a way I can modify the script (or create a new script) to reference multiple, sharable links in the script editor so the sharing access to each individual students' pass expires at the end of the month? In other words, is there a way for the script to expire all 80+ passes at the same date and time? I'm a novice to Google Scripts so I'm wondering if I am not formatting my list or URL references correctly so the script recognizes multiple files, or perhaps I could revise the script to recognize an array of links instead of a single variable url link.
Thanks in advance for the assistance!
I hope you have the full script at your hand.Please give the URLs in a list, separated with commas. Are the passwords different for each student? Otherwise you could have just added the URL of the folder without changing the original script at all.
However, Please try changing these portions of the script:
// Enter the full list of URLs(separated with comma) of the public Google Drive file or folder
var URL_LS = ["https://drive.google.com/open?id=0B_NmiOlCM-VTcFp6U1hydTM2MzVCY3AzMUpuTEtVWUhXRXNz", "https://drive.google.com/file/d/0B_NmiOlCM-VTNXFqOU1jTG5JdHJVQ3ZGUGNqZklJNWludllr/view?usp=sharing"] ;
var URL_LN = URL_LS.length;
// Enter the expiry date in YYYY-MM-DD HH:MM format
var EXPIRY_TIME = "2016-02-18 09:46";
And again,replace the autoExpire() as follows:
function autoExpire() {
var id, asset, i, email, users;
try {
for (var j = 0; j < URL_LN; j++) {
var URL = URL_LS[j];
var id = URL.match(/[-\w]{25,}/g);
if (id) {
id = id[id.length-1];
asset = DriveApp.getFileById(id) ? DriveApp.getFileById(id) : DriveApp.getFolderById(id);
if (asset) {
asset.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.NONE);
asset.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.NONE);
users = asset.getEditors();
for (i in users) {
email = users[i].getEmail();
if (email !== "") {
asset.removeEditor(email);
}
}
users = asset.getViewers();
for (i in users) {
email = users[i].getEmail();
if (email !== "") {
asset.removeViewer(email);
}
}
}
}
}
} catch (e) {
Logger.log(e.toString());
}
}
I have a google spreadsheet which will be used for collaboration and I was able to create a UI interface which will fetch and update data from a table to make it easier for users.
For the work I need to restrict users to only be able to update rows if they are assigned to them and I am able to do it. But along with that, I also need to allow administrator to assign or update rows which are not assigned to them.
I have a table with all employee's email address and an admin flag for those who are admin.
Name____________Email________________Is Admin
Employee 1_______emp1#domain.com_____Admin
Employee 2_______emp2#domain.com_____
Employee 3_______emp3#domain.com_____
Employee 4_______emp4#domain.com_____Admin
Employee 5_______emp5#domain.com_____
Employee 6_______emp6#domain.com_____
How can I write a something in my script that will allow me to if the user who is triggering a function has admin right or not.
I user Session.getActiveUser().getEmail() to pull out the users email address. I am creating the tool to be used within google apps domain.
Code that checks if the user is the owner of the row, this part works fine but what I want to do is if a user is admin they basically will skip this check.
if (s.getRange("E4").getValue() != usr()){
msg("Once a Task is assigned to an employee, only assigned employee can make any changes to the Task. Thank you");
return;
}
s = sheet
usr() = function calling to check active user email
If I can do something like countifs where I can check count based on the email address and Admin criteria and if its >= 1 proceed if its 0 show error that you can not make changes.
Please let me know.
Really appreciate the help.
Thank you.
Zooooon
Are you looking for something like this?
function myFunction() {
var s = SpreadsheetApp.getActiveSheet();
var usr = Session.getActiveUser().getEmail();
var emails = s.getRange("E2:F").getValues();
for (var i=0;i<emails.length;i++) {
if (emails[i][0] === usr){
if (emails[i][1] === "Admin") {
//Do something when Admin
return;
} else {
//Do something when not admin
return;
}
} else {
//Do something when email not in the list
}
}
}
I am assuming your data is in the range "D:F"
Hi im using Advanced Services in Google Apps Script.
Im trying to add a number to the users profile.
var userValue = 'test#company.com';
var phoneValue = 017236233;
var users = AdminDirectory.Users.get(userValue);
for (var i = 0; i < users.length; i++) {
AdminDirectory.Users.update(users[i].phones[].primary, phoneValue);
}
The last part is the one i am not certain on. It fails with "Syntax error. (line 22, file "Code")"
Looking at the autocomplete on the update method you have to give it a User resource and a userKey (the user primary e-mail).
So this line of code should be:
AdminDirectory.Users.update(userResource, userPrimaryEmail);
Since you just want to add a phone your User resource can only contain this:
var userResource = {
phones:[{
value: phoneValue
}]
}
However be aware that this would update the whole list of phones and overwrite older values.
Also, note that the get method your are using doesn't return a list of User resources but a single User resource. You could use that same resource, update it and send it back.
So what you're looking would be:
var userPrimaryEmail = 'test#company.com';
var phoneValue = 017236233;
var user = AdminDirectory.Users.get(userPrimaryEmail);
// If user has no phones add a 'phones' empty list to the user resource
if (! user.phones){
user.phones = [];
}
user.phones.push(
{
value: phoneValue,
type: "mobile" // Could be 'home' or 'work' of whatever is allowed
}
)
AdminDirectory.Users.update(user, userPrimaryEmail);
A syntax error means that the code you wrote isn't valid. More information on syntax errors is available on the Apps Script troubleshooting guide.