How to check the identity of the person who added a Google Doc comment - google-drive-api

I want to create a add-on that processes all the comments added by the user in a Google Docs document. I can use Drive.Comments.list to get the list of all comments in the doc and Session.getEffectiveUser to get the identify of the user running the script.
I tried filtering by email address. However, the results of Drive.Comments.list do not include author.emailAddress. Here is an example to show that displayName is included. Here is an example that shows that emailAddress is not included. The documentation for emailAddress does say This may not be present in certain contexts if the user has not made their email address visible to the requester. However, emailAddress is not present even for comments added by the user running the script. And so filtering by email address does not work.
I then tried filtering by display name. However, Session.getEffectiveUser does not provide a way to get the display name. Is there any other way to get the display name of the user running the script?

Try about.get This returns the users email address.
{
"kind": "drive#about",
"user": {
"kind": "drive#user",
"displayName": "Linda Lawton",
"photoLink": "https://lh3.googleusercontent.com/a-/AOh14GhroCYJp2P9xeYeYk1npchBPK-zbtTxzNQo0WAHI20=s64",
"me": true,
"permissionId": ddd88225573437243",
"emailAddress": "xxxxx#gmail.com"
},

Related

Does Gsuite support the creation of custom fields that can be used to discover information when used as search strings?

I want to bulk-load some emails in the Gsuite account using a csv file. However there's this field called "building id" which is NOT REQUIRED but is handy for my requirements; I just realized that out of the box, Gsuite cannot locate a record by the mentioned field as the search term. My question is, is there a workaround to this problem? Better yet, does Gsuite support the creation of custom fields that can be used to discover information when used as search strings? Anyone who knows a solution to this problem?
When using the Directory API to list the members, using the query parameter to get the members with a specific buildingId is not an option. The available queries that can be made are listed here.
However, you can create your own custom field using the below Directory API schemas.insert request:
POST https://admin.googleapis.com/admin/directory/v1/customer/{customerId}/schemas
With the following request body:
{
"schemaId": string,
"schemaName": string,
"fields": [
{
"displayName": "",
"fieldName": "",
"fieldType": ""
}
],
"displayName": string,
}
Afterwards, when you want to retrieve the users with a particular value, you will have to make the below request:
GET https://admin.googleapis.com/admin/directory/v1/users
With the following fields:
customFieldMask > set to the name of the schema;
projection > set to custom;
query=schemaName.FIELD='VALUE' > this will query the users who have the field from the custom field with the value 'VALUE'.
Reference
Directory API Users:list;
Directory API Schemas.insert.

Google Contacts script slow

My goal is really simple, which is to add contacts to Google Contacts App from data range and label them as "Coworkers' and "My Contacts" so i can use them on my phone. The below is the working codes I just came up with.
function myFunction() {
//deletecontacts();
var gn=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Leader Directory (Read only)');
var systemcon=ContactsApp.getContactGroup('System Group: My Contacts');
var systemmy=ContactsApp.getContactGroup('System Group: Coworkers');
var valuerng=gn.getRange("A2:H138").getValues();
for (var i=0;i<=gn.getLastRow()-2;i++){
var newcontact=ContactsApp.createContact(valuerng[i][0],'' , valuerng[i][1]);
newcontact.addPhone('MOBILE_PHONE',valuerng[i][2]);
newcontact.addAddress('Chapel', valuerng[i][6]);
newcontact.addAddress('Mailing', valuerng[i][4]);
newcontact.addCompany(valuerng[i][7],'');
systemmy.addContact(newcontact);
systemcon.addContact(newcontact)
}
}
Can anyone modify them so they'd run faster? If I just upload the entire csv to Contacts App, it'd only take 3 seconds but my codes take something like 3 minutes. Another question is why the codes to add newly created contacts can only run at the end or more often than not,
systemmy.addContact(newcontact);
systemcon.addContact(newcontact)
would return null at random contacts and quit the function if they were executed right after a new contact is created?
The reason your script is slow is because each instruction in your for loop is actually making an update request, resulting in four update requests and one post request (when creating the contact).
Unfortunately, there is no way to reduce this to just one update request since all the methods you have used - addPhone(),addAddress(),addCompany() - expect a Contact but return a different type, therefore, making it impossible calling them one after the other.
Since this is not a viable option for you, you might want to take a look at the People API.
You can send this POST request:
POST https://people.googleapis.com/v1/people:createContact
With the following request body:
{
"names": [],
"emailAddresses": [],
"phoneNumbers": [],
"addresses": [],
"organizations": []
}
Reference
Contacts Service Apps Script;
People API Create Contact;
People API REST Resource: people.

Accessing list of groups along with user's role

I want to get a list of groups where i have member access and also i want to know which role I have in those Google groups.
for example, assume a user is a member of 70 groups. Through admin console I can check which role is assigned to that particular user, But its a lengthy process.
Can I list Google groups,where a user has membership access(atleast) along with the role(member,manager or owner) assigned to the user in those groups.
I would like to do it using Google App Script.
Thanks in advance
You can use the Group:list API where a userKey can be specified. It returns all the groups the user is a member of. Use the following HTTP request and include the authorization described in Authorize requests .
There's a JSON request were you can get the role of the group member, a group member's role can be (OWNER, MANAGER, MEMBER).
Here's a successful response returns an HTTP 200 status code and the member's information:
{
"kind": "directory#member",
"id": "group member's unique ID",
"email": "liz#example.com",
"role": "MEMBER",
"type": "GROUP"
}
For more information regarding Group members, click here: https://developers.google.com/admin-sdk/directory/v1/guides/manage-group-members#create_member

Insert all domain members into Group

I'm currently writing an Add-on to manage Google Groups, and it uses the Admin SDK Directory API to loop through and retrieve all group members and make changes etc.
One curious issue I've found is that when 'All members of the domain' have been added to the group, no member email is supplied. For example, if I retrieve all members of the group, each member will be returned in the format:
{
"kind": "admin#directory#member",
"etag": "\"ABCDEFGHIJKLMNOPQRSTUV123456789\"",
"id": "123455678910",
"email": "email#myDomain.com",
"role": "MEMBER",
"type": "USER"
},
However, if you've added all users within the domain to a group, when you retrieve this 'member', it's returned in the format:
{
"kind": "admin#directory#member",
"etag": "\"ABCDEFGHIJKLMNOPQRSTUV123456789\"",
"id": "123455678910",
"role": "MEMBER",
"type": "CUSTOMER"
},
This is fine for retrieval, as I can identify that it's 'All users in the domain' by the 'type' always being 'Customer', then I just give it an arbitrary email address to display in my interface (I'm using '*#domain.com' just because).
However, when I'm updating the group members list using the 'Insert' method, it requires an email Address (It refers to 'memberkey', but I understand that this must be an email address).
It won't accept dummy addresses such as *#domain.com (It returns an error that this particular address exists, so I gues it's in use in the background), and if I use an existing address and try to overwrite the 'type' from 'User' to 'Customer' in an attempt to convert an existing member to the 'All users' value, this doesn't work (I suspect the 'type' field does not allow 'Customer' as a writable field, only a readable one).
My question: There does not seem to be a method to 'Add all users in the domain' to a group neatly, without looping through the domain and literally adding all members one by one. Does this method exist and I've just missed it? Or is there a neater way to add all members to the group without looping through all the members on the domain and adding them to the group one by one?
No API method has existed for this even back into the provisioning API days. Nothing in Group Settings is different for a group of this kind, it seems to be an unsupported Member Resource. Trying to modify existing members to become type: 'CUSTOMER' also fail.
You can, however, set a single group in the Admin Panel UI to be your "All Members" and then use the address of that group as a proxy member. i.e. it is a propagated all member feature. This is a workaround as opposed to a direct answer, but as I state above the real answer is "No".

MailChimp Interested Value in "Group" always false after "subscribing"

I send over proper json formatted code, according to v2 api docs:
lists/subscribe.json
"GROUPINGS":[{"id":removed_id,"name":"grouping_name","groups":["group_name"]}]
I get back information about a member... It does not say they are interested in that group name..
lists/member-info.json
"GROUPINGS":[{"id":removed_id,"name":"grouping_name","form_field":"hidden","groups":
[{"name":"group_name","interested":false},{"name":"other_group_name","interested":false},
{"name":"other_group_name2","interested":false},{"name":"other_group_name3","interested":false}]}]
I do not understand how I can get these users to show up as "subscribed" to a group within my grouping. I have been trying for nearly 5 hours now. I have tried:
making sure the groups: value is an [] array.
trying out making that value a string (which surprisingly did not throw errors)
capitalizing "GROUPINGS" when sending it in merge_vars
not capitalizing "groupings" when sending in merge_vars
using the lists/update-member.json method to update these groups
using the name of my interest group instead of grouping in the grouping array.
using the option replace_interests on both true and false
In conclusion,
I had to email mailchimp a support ticket. Without changing my code at all - it works this morning. Interestingly enough, mailchimp was experiencing many issues yesterday with servers being down and alleged email hackings.
To be clear about which version of my code worked - following the API exactly for v2.
It was an error on mailchimp's end.
{
"id":"MY LIST ID",
"email":{
"email":"THE EMAIL TO SUBSCRIBE"
},
"merge_vars":{
"GROUPINGS":[
{
"id":THE GROUPING ID,
"groups":[
"THE NAME OF MY GROUP"
]
}
]
},
"double_optin":false,
"replace_interests":true,
"apikey":"MY API KEY"
}
Also, in case you are curious, replace_interests is true when the groups you send are supposed to replace existing groups the subscriber is interested in. False indicates that the groups you pass should be added to the interest groups.
If you are updating a member (method: lists/update-member), it may be best to set replace_interests to false just to make sure it does not overwrite your existing interest groups (possibly with blank groups). The default value for it is true, so this could be another place prone to error.