'Creating users in Google Apps from Apps Script
I am using Google Apps Script to create users in Google Apps in bulk via spreadsheet
my spreadsheet has 4 columns where i have firstname, lastname, emailId, and password, as these are required to create users.
My following script works fine and create users without issue, however it stops at a point if any user already exists, and does not move forward to attempt other ones in spreadsheet.
I want it to loop through the whole spreadsheet, skip the ones that already exists, and create the ones who don't
Any help is appreciated.
function createUsers() {
var ss = SpreadsheetApp.getActive()
var sheet = ss.getSheetByName("Create Users")
var values = sheet.getDataRange().getValues()
for(i=1; i <values.length; i++)
{
var fName = values[i][0]
var lName = values[i][1]
var email = values[i][2]
var password = values[i][3]
var status = values[i][4]
var user = AdminDirectory.Users.insert({
"primaryEmail": email,
"password": password,
"orgUnitPath": "/MDM Testing",
"name": {
"familyName": lName,
"givenName": fName
}
})
Logger.log(user);
}}
Solution 1:[1]
You can add If condition to check duplicate user as follows.
function createUsers() {
var ss = SpreadsheetApp.getActive()
var sheet = ss.getSheetByName("Create Users")
var values = sheet.getDataRange().getValues()
for(i=1; i <values.length; i++)
{
var fName = values[i][0]
var lName = values[i][1]
var email = values[i][2]
var password = values[i][3]
var status = values[i][4]
// Check user exist or not by email address.
var userFromDirectory = AdminDirectory.Users.get(email);
if(email != userFromDirectory.primaryEmail){
var user = AdminDirectory.Users.insert({
"primaryEmail": email,
"password": password,
"orgUnitPath": "/MDM Testing",
"name": {
"familyName": lName,
"givenName": fName
}
})
Logger.log(user);
} // end of if
}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nitin Dhomse |
