I am working on a Google Sheets add-on that calls Permissions: insert
as described in a Stack Overflow answer:
function addUserPermission(fileId, email, notify) {
Drive.Permissions.insert({
'value': email,
'type': 'user',
'role': 'reader',
'withLink': false
},
fileId,
{
'sendNotificationEmails': notify
});
}
The notify
flag is true
if the user should receive an email notification of the added permission, false
if they should not.
If the email
value is associated with a Google account (such as [email protected]
), this works whether notify
is true
or false
. If the email
value is not associated with a Google account, an exception is thrown if notify
is false
:
ScriptError: GoogleJsonResponseException: API call to drive.permissions.insert failed with error: Bad Request. User message: "You are trying to invite [email protected]. Since there is no Google account associated with this email address, you must check the "Notify people" box to invite this recipient."
Is there any way to tell before calling Drive.permissions.insert
that the email address is not associated with a Google account?
CodePudding user response:
This is only possible for Admin Users
If you are an admin for your Google Workspace, you may use AdminDirectory.Users.get("userEmail")
to check whether the google account (under your google workspace) exists or not. You may use the following script as the base for your code:
function getUser(userEmail) {
try {
const user = AdminDirectory.Users.get(userEmail);
Logger.log("Email exists");
} catch (err) {
Logger.log('Email does not exist. Error: %s', err.message);
}
}
NOTE: This script only works if:
- You are an administrator in your Google Workspace.
- The google account is registered under your Google Workspace.
- AdminDirectory service is added to the Apps Script project.
Reference
For further details, you may read the following: