-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgoogle_create.gs
More file actions
77 lines (66 loc) · 2.51 KB
/
google_create.gs
File metadata and controls
77 lines (66 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*******************************************************************************************************************************************
Create a new user in your Google Workspace.
You will need to enable at least Directory API and admin SDK
https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/insert
You need to run is as admin with appropriate access.
---------
This script has some custom settings that you will probably want to change.
- orgUnitPath
*/
// Generic SHA512 implementation
function SHA512(input_string) {
var hexstr = '';
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_512, input_string);
for (i = 0; i < digest.length; i++) {
var val = (digest[i] + 256) % 256;
hexstr += ('0' + val.toString(16)).slice(-2);
}
return hexstr;
}
// Assemble set of payloads
function loadUserData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var Google_create = SpreadsheetApp.setActiveSheet(ss.getSheetByName("Google_create"));
var lastRow = Math.max(Google_create.getRange("A1:A").getLastRow(), 1);
var lastColumn = Google_create.getLastColumn();
var userArray = []
if (lastRow > 1) {
var data = Google_create.getRange(2, 1, lastRow - 1, lastColumn).getValues(); //(2,1,lastRow,6) start row, start column, number of rows, number of columns
for (const row of data) {
let makeUser = row[7]
if (makeUser) {
userArray.push(
{
"name": {
"givenName": row[1],
"familyName": row[2],
},
"primaryEmail": row[0], // All the values here are based on columns in a sheet A=0, B=1
"recoveryEmail": row[3],
"orgUnitPath": "/Onboarding",
"changePasswordAtNextLogin": true,
"password": SHA512(Math.random()).substring(row[0].length, row[0].length + 30),
}
);
};
}
}
return userArray
};
/****
This is an actual call we are making to google.
We will iterate over entries in dataArray making API call for each.
*/
function makeUser(userArray) {
var userArray = loadUserData(); // You this only if you want to run this step manually.
if (userArray.length > 0) {
for (var i = 0; i < userArray.length; i++) {
try {
// var update = AdminDirectory.Users.insert(userArray[i]); // This will update your org, you have to un-comment it to work.
console.info("User made : " + update.id)
} catch (e) {
console.error('makeUser() yielded an error: ' + e);
}
};
}
};