Reputation: 21
I have a google sheet ("Test") where column 2 (B) contains the names of the contactpersons from my Google Contacts. I want to place in column 1 (A) the resourceName of the contactperson.
The Cell in Column 1 (A) contains no data. (There has the result to be placed after running the function)
The Cell in Column 2 (B) contains in row 2 the name "Mark Gerrits".
In GoogleContacts "Mark Gerrits " has the "person/cxxxxxxxx" as the resourceName (actually: "people/cxxxxxxxx")
What i want to achieve is that by using the People.searchContact method Method: people.searchContacts, in Cell A2 the resourceName of "Mark Gerrits" is placed.
These are the steps
open an inputbox where i fill in the rownumber corresponding with the row of the contactperson for which i want to find the resourceName (ic: row 2)
search - with the method People.SearchContacts - in the Google Contact the person with the name in cell (rownumber, 2) > "Mark Gerrits"
finds the corresponding resourceName of that person (ic: )
places the found resourceName in the same row - rownumber - but the first column (column 1 or A): "people/cxxxxxxxx
I used the function from this entry Format for Google People API searchContacts()?
Many Thanks to HotDogWater and Oz Ben-David !
I modified and tested this function over and over again.
What I was expecting is that the resourceName of the person corresponding with his unstrucuredName (the information provided in the google sheet) would be placed in the google sheet.
Sometimes the function worked and put the found resourceName in the cell, sometimes it gives no resourceName at all (of course i checked if the person exitsts!)
function getResourceNameFromCellValue() {
//see documented: function getNameFromEmail()
var spreadsheetId="xxxxxxxxxxxxxxxxx";
// Open spreadsheet with the ID
var spreadsheet = SpreadsheetApp.openById(spreadsheetId);
// Name of the sheet
var dataSheetName = "Test";
// Get the sheet
var sheet = spreadsheet.getSheetByName(dataSheetName);
//var numberRow = 2;
var rowNumber = Browser.inputBox("Bepaal rijnummer van Contactpersoon", "Voer het nummer van de rij in:", Browser.Buttons.OK_CANCEL);
// Give the rownumber in the sheet for which Contact you want the resourceName
var rowAsNumber = Number(rowNumber);
var NameColumn = 2;
var Name = sheet.getRange(rowAsNumber,NameColumn).getValue();
Name = Name.toString();
//var name = Name;
var resourceName = Name;
var response;
Browser.msgBox('Succes','The resourceName of the Person with unstructuredName "' + Name + '" is placed in the Cell.',Browser.Buttons.OK);
People.People.searchContacts({ //Empty the cache
query: '',
readMask: 'names,emailAddresses',
});
response = People.People.searchContacts({
query: Name,
readMask: 'names,emailAddresses'
});
// Check if the response contains contacts
if (response.results && response.results.length > 0) {
//name = response.results[0].person.names[0].displayName;
resourceName = response.results[0].person.resourceName;
} else {
People.OtherContacts.search({ //Clear the cache
query: '',
readMask: 'names,emailAddresses',
});
response = People.OtherContacts.search({
query: Name,
readMask: 'names,emailAddresses'
});
// Check if the response contains contacts
if (response.results && response.results.length > 0) {
// name = response.results[0].person.names[0].displayName;
resourceName = response.results[0].person.resourceName;
}
}
//Logger.log(name);
Logger.log(response.results[0].person.resourceName);
Logger.log(resourceName);
// put the value of the result (name) in cell
//sheet.getRange(2, 1).setValue(name);
//sheet.getRange(rowAsNumber, NameColumn).setValue(resourceName);
sheet.getRange(rowAsNumber,2);
}
Upvotes: 0
Views: 66