How to add a contact without a name in GAS? - google-apps-script

I have a collection of contact info that I would like to add to Google Contacts via the ContactsApp. Is some cases this info is just an e-mail address without a name.
contact = ContactsApp.createContact("tmp1", "tmp2", "email");
The above doesn't seem to accept "" as an argument. Neither do the following:
contact.setGivenName(".");
contact.setFamilyName(":");
contact.setFullName("*");
I could use " ", but that is not ideal. Is there any way to have the names be "" just like when an e-mail address is saved from a gmail message sent without the name being available?

Just use null
contact= ContactsApp.createContact(null,null,"email#somewhere.com");

Related

ContactsApp.getContact() for contact with multiple emails returns a different contact for each email address

My company allows employees to have multiple email addresses in their account.
I am attempting to write an input form where someone can enter any of the emails for a contact and we'll resolve it to the same person.
I've tried doing this via ContactsApp.getContact(email).getPrimaryEmail() to resolve all different inputs to the same primary email, but it doesn't work as expected. Each email I search for returns a different Contact object with only a single email (the one I used to search).
Even if I use ContactsApp.getContact(email).getEmails() to list all emails to the employee, I can see it only returns one at a time.
When going to contacts.google.com, I see the information I expected. Searching for any of the emails will return the same contact, with the same primary email and all other emails listed.
Is there something I'm doing wrong? Or is this how ContactsApp works. If so, is there a workaround?
Thanks!
getPrimaryEmail() will only return an email address if the contact in question has had a default email set and this can only be done via the Google Contacts App, not on the web (go figure).
To solve your problem I would suggest loading all the contacts into an array variable first and then searching this for matching email addresses. I would do the data retrieval on page/view load (so it can be reused without multiple server calls) but have included it all together here for conciseness.
What I found strange about the ContactsApp (and presumable People API too) is that the contacts returned are just empty objects (when logged) with just a bunch of methods on them. However, once you have an array of objects you can write your own properties to those objects for easier access of data.
Therefore I would first of all retrieve all the users contacts, then add a property of .emails to each contact object in the array and then use .some, perhaps, to check if a match appears in .emails (which will also be an array).
Something like:
let strSearch = 'someone#somewhere.net' // EMAIL address to search based on user input
let arrContacts = ContactsApp.getContacts();
let contacts = arrContacts.map(contact => {
let emails = contact.getEmails();
contact.emails = emails.map(email => email.getAddress());
return contact;
});
let foundContact = contacts.filter(contact => contact.emails.some(email => email === strSearch));
Remember .getEmails() returns another array of objects that have the method .getAddress() on them in order to retrieve the actual address, that's why I'm getting an array of email objects with let emails = contact.getEmails(); then using emails.map here to put the actual email addresses into an array stored in the contact.emails property (that doesn't exist so we're actually creating it here).
After that we're filtering the contacts array down to a contact where the searched email address is matched to one of the email addresses in the contact.emails array. I haven't tested it so the last line might need playing around with slightly but I've used something very similar so would expect it to work.
You can then use foundContact[0] to reference the contact found and use the further methods of .getFullname() .getId() etc. to retrieve their data as required. If you need the contacts phone numbers or geographical address that's a whole other process of returning an array of objects using .getPhones() or getAddresses(), but I think that's beyond the scope of this question.

How to retrieve a single contact with gdata? Getting a 'not supported' error

I'm copying the code here https://developers.google.com/google-apps/contacts/v3/#retrieving_a_single_contact.
And here's my code:
Dim cr = ContactAuthentication()
Dim groups = GetGroups(cr)
Dim entry As Contact
entry = cr.Retrieve(Of Contact)(New Uri("https://www.google.com/m8/feeds/contacts/default/full/38B2D4F80D96B2C2"))
On the last line, it falls with the following error:
Google.GData.Client.GDataRequestException: 'Execution of request
failed:
https://www.google.com/m8/feeds/contacts/default/full/38B2D4F80D96B2C2?max-results=100'
"The 'max-results' parameter is not supported on this
resource"
Which is odd, since I never put in a max result parameter. Also, if it makes any difference, the Google docs show an example that takes a string url as the param for Retrieve. I could not find such an overload, the closest is what I put here, using a Uri
Anyone have any ideas how I can retrieve a single contact by id for updating?
Thanks!
You may refer with this documentation: Retrieving a single contact. To retrieve a single contact, send an authorized GET request to the contact's selfLink URL:
https://www.google.com/m8/feeds/contacts/{userEmail}/full/{contactId}
With the appropriate values in place of userEmail and contactID. Be noted that the special userEmail value default can be used to refer to the authenticated user.
And as referred with this post, maybe you had setting.Pagesize = 100, which caused your uri to be https://www.google.com/m8/feeds/contacts/{userEmail}/full/{contactId}?max-results=100.
Hope this helps!

Email notification to a specific user based on value in a cell

I'm about to create a Google Form which will export to a Google Sheet. I want to be able to send an email notification to a specific email address (from a list of addresses) based on an answer (which would be in a cell in column C) when the form is submitted.
Example:
UserA answers first question which is required: "What region are you from?"
based on that answer, I want an email to be sent to a specific email address from a list of addresses.
If UserA answers South East an email will be sent to Bob#acme.com
If UserA answers North, an email will be sent to Betty#acme.com
If UserA answers West, an email will be sent to Frank#acme.com
Thanks.
You can use an object and test the incoming value for the Col C.
Example:
var emails ={"South East": "Mail1#some.com,
"North": "Mail2#some.com",
...
}
And then assuming the column c goes like..
var colC = e.namedValues['xxx'];
var recipient = emails[colC];
This is just the general methodology. You can also use switch statement.
You should validate the input before proceeding to confirm if the user value is valid.
Plenty of tuts available on email notification on form submit.

How to get/read Email ID from a from response

I have created an application where I am collecting form responses from various users. I am getting responses with email id in responses spreadsheet. As I don't want to store data in spreadsheet so I am reading data trough responses. I am facing some challenges please guide.
Query 1
while using onFormSubmit(e) I am not able to read submitted form, given code is returning null:
var form = FormApp.getActiveForm();
Logger.log('usename:' + form.getId());
error " Cannot call method "getId" of null." although if I hard coded value of formid var form = FormApp.openById('<<form_id_xyz>>'); then it is working fine and I can read responses as well.
How can I get form responses for multiple users?
Query 2
getRespondentEmail(); is not working in my case. Even I use form id <<form_id_xyz>> and trying to get email id from responses which I have captured at the time of form submission form.setCollectEmail(true); I tried following code in onFormSubmit(e) function but dint get a result:
var formResponse=form.response;
Logger.log('email id of user: ' + formResponses.getRespondentEmail());
and another way:
Logger.log('email id of user: ' + form.getRespondentEmail());
and
Logger.log('email id of user: ' + e.values[1]);
nothing works for me. Kindly guide.
Query 1: Hope it's clear in my comment.
Query 2:
Sorry to say, I don't understand your second query problem completely.
However as per your requirement I am suggesting this code.
If you have created a form you should know the form id (I assume) so try this code.
var form=FormApp.openById('your form id here');
//this returns your form which you created//
var responses=form.getResponses();
/// this will give you all responses of your form as an array////
///iterate the array to get respondent email id///
for(var i = 0; i < responses.length; i++){
Logger.log(responses[i].getRespondentEmail());
}
I think it's important to note that at the present time the answer to your question is: You can get what they enter, but you cannot get their true verified Email Address. This is explained better in this question and one of the answers details some workarounds such as publishing form as a web script.
The accepted answer displays what email address the user has typed into the form. There is no authentication to this beyond it having an # symbol thus a user could type foofoo#zoomZoom.com and it would be viewed in the forms results and scripts.
What's annoying is that google IS capturing the user's true email address because if settings are set to Allow One Response Per User, then the user is limited to one submission -- regardless of what they put as their email account. I'm not sure why Google won't provide a method to view the submitter's login email address since it has been disclosed to the user that this will be disclosed.
Microsoft Forms does capture this.

How do I hotlink email addresses in reporting services?

I have a dataset that returns with individuals email addresses. I would like the ability to just click on them and boom up pops a message addressed to that person just like in any other MS application.
I tried right-clicking the field, go to properties, then the action tab, and point the "go to url" to the email column list in the data set and no go.
This seems like a no brainer...
It has to be a valid URL link, not just an email address, so you have to prepend it with "mailto:". For example, make the navigation URL be an expression like:
="mailto:" & Fields!EmailAddress.Value
For bonus points, add a subject:
="mailto:" & Fields!EmailAddress.Value & "?subject=This report is great!"
Or even a subject and a body:
="mailto:" & Fields!EmailAddress.Value & "?subject=This report is great!&body=You deserve a raise!"
Of course, the expression for the Value property of the cell should remain just as the email address.
="MailTo:" + Fields!EMAIL.Value & "?Subject=" + Fields!SUBJECT.Value