M. Atou
M. Atou

Reputation: 21

App script to Search the resourceName from a person by using the unstructuredName in a Cell in a Google Sheet and placing it a Cell of the same sheet

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

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

Answers (0)

Related Questions