Guest User

Untitled

a guest
Apr 16th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /*
  2. Created by Nivea Tejada.
  3. If you have any questions about this code, please
  4. feel free to contact me at NiveaTejada@Gmail.com.
  5. */
  6. //This variable is for the main sheet you will be checking/pulling your information from
  7. var SHEET_NAME = 'Sheet Name';
  8. //This variable is to allow the system to know if the user was created successfully
  9. var USER_CREATED_TEXT = 'User Created';
  10.  
  11. function createUsersForFailedRows(){
  12. var ss = SpreadsheetApp.getActiveSpreadsheet();
  13. var sheet = ss.getSheetByName(SHEET_NAME);
  14. var dataRange = sheet.getDataRange();
  15. var rowValues = dataRange.getValues();
  16. var rowNum = rowValues.length;
  17. /*
  18. This for loop will allow the script to look through every row.
  19. If the 10th column of that row, where the user status text is held, is empty that means
  20. the user was not successfully created.
  21. If the 10th column had the USER_CREATED_TEXT, then the user was in fact created and the
  22. script can skip the row as it has nothing to do.
  23. */
  24. for (var row = 1; row<rowNum; row++) {
  25. if (USER_CREATED_TEXT !=rowValues[row][10-1]){
  26. createUser_(ss,sheet, row+1);
  27. Utilities.sleep(500);
  28. Logger.log("User failed to be created - Will now attempt to create user")
  29. }
  30. else{
  31. Logger.log("Do nothing - This user was successfully created")
  32. }
  33. }
  34. }
  35.  
  36.  
  37. function createUser_(ss,sheet,row) {
  38. //Information for the user that is to be created
  39. var firstName = sheet.getRange(row, 1).getValue();
  40. var lastName = sheet.getRange(row, 2).getValue();
  41. var emailAddress = sheet.getRange(row, 3).getValue();
  42. var phoneNumber = sheet.getRange(row, 4).getValue();
  43. //User Organization Information
  44. var orgName = sheet.getRange(row, 5).getValue();
  45. var orgDepartment = sheet.getRange(row, 6).getValue();
  46. var orgTitle = sheet.getRange(row, 7).getValue();
  47. //User Address information
  48. var city = sheet.getRange(row, 8).getValue();
  49. var state = sheet.getRange(row, 9).getValue();
  50. var userEmail = firstName+"."+lastName+"@YourDomain.com";
  51. //Generate a random password string
  52. var newPassword = Math.random().toString(36).slice(-8);
  53. //Create the User
  54. var user = {
  55. primaryEmail: emailAddress,
  56. name: {
  57. givenName: firstName,
  58. familyName: lastName
  59. },
  60. addresses:[
  61. {
  62. locality:city,
  63. region: state,
  64. type :"work",
  65. primary: true
  66. }
  67. ],
  68. //This allows the user to change their password upon initial login
  69. changePasswordAtNextLogin:true,
  70. //Enter the org unit that you would like to user be belong to
  71. orgUnitPath:"",
  72. emails:[
  73. {
  74. address:emailAddress,
  75. type:"work"
  76. }
  77. ],
  78. organizations:[
  79. {
  80. name:orgName,
  81. title:orgTitle,
  82. department:orgDepartment,
  83. type :"work",
  84. primary: true
  85. }
  86. ],
  87. phones:[
  88. {
  89. type:"mobile",
  90. value:phoneNumber
  91. }
  92. ],
  93. // Sets the password to the random password generated
  94. password: newPassword
  95. };
  96.  
  97. var userSuccess = false;
  98. var userStatus = responses.getRange(row, 11);
  99. //Try to create user and add user to the Admin Directory
  100. try{
  101. user = AdminDirectory.Users.insert(user);
  102. userSuccess = true;
  103. userStatus.setValue("Creation of User was a success");
  104. }
  105. /*
  106. There are instances where the script will fail to add the user.
  107. A very common error is:
  108. - The user (entity) with the same First and Last name already exists
  109. */
  110. catch(e){
  111. Logger.log("error"+e);
  112. //This adds what the error is to the spreadsheet rather than having to check logs
  113. userStatus.setValue("The following Error Occured while creating the user: " + e);
  114. }
  115.  
  116. if (userSuccess) {
  117. var SetStatus = responses.getRange(row, 10).setValue(USER_CREATED_TEXT);
  118. Logger.log ("The user for the row "+row+" has been successfully created");
  119. } else {
  120. Logger.log ("The user for the row "+row+" was not created");
  121. }
  122.  
  123. }
Add Comment
Please, Sign In to add comment