This script works. It has user id 4 which I am adding their email address to check the google admin API. The resolved column[10]
is where managers approve requests. Because some of these usernames are not correctly entered, I was looking for a way to check if they were actually correct user ids. Both .setValue()
lines work under try and catch, they also error on same users. I am trying to understand where the actual validation is happening. I guess I am trying to figure how the script knows to check the primary email address? Hope that makes sense.
function getNames() { var ss = SpreadsheetApp.getActiveSpreadsheet() var sheet = ss.getActiveSheet(); var values = sheet.getDataRange().getValues(); for(var i = 1; i < values.length; i++){ var data = values[i]; var resolved = data[10]; if(resolved === ""){ try{ var email = sheet.getRange(1+i,4).getValues() + '@buisness.com' var user = AdminDirectory.Users.get(email); var name = user.name.fullName; sheet.getRange(1+i, 5).setValue('Yes this is an employee');//How does this work? //sheet.getRange(1+i, 5).setValue(name);//This works which I expect } catch(errors){ sheet.getRange(1+i,5).setValue(errors); } } } }
Advertisement
Answer
The validation happens through the AdminDirectory.Users.get()
function with the help of the try and catch
statements.
Admin SDK Directory Service/Get User
This API function gets a user by their email address. If the email address exists, all their data will be logged as a JSON string. Otherwise, it will return an error.
Try and Catch
The try
and catch
statements belong under JavaScript Errors. The try
statement will run a code block that you place in it. If it encounters any error, it will trigger the catch
statement (which usually displays the error messages.
Your Code
Your code checks for the validity of the email address with the use of the AdminDirectory.Users.get() function inside the try
statement. If the function fails, it returns an error which triggers the catch
statement (which usually displays the error messages).