i'm creating a google sheet which translates given data from schools to a admin SDK Upload to Google Apps. I know there a create user limit of 10 per second, hence the 120ms Delay time. but, when coloring each row in sheets which is processed the speed is around 500ms - 2 seconds per entry. Which causes the script to stop at the maximum execution time, because there are more than 600 users to be added. Where does it go wrong?
function UploadUsers() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Upload");
ss.setActiveSheet(sh);
var column = sh.getRange('A1:I5000');
var values = column.getValues(); // get all data in one call
var uploadRows = 0;
while ( values[uploadRows][0] != "" ) {
uploadRows++;
}
var i=0
var uiACU = SpreadsheetApp.getUi();
var ACUsersMessage0= "Upload Users";
var ACUsersMessage1= "Indien u op OK drukt worden er : "+ uploadRows + " Users aangemaakt! "
var result = uiACU.alert(
ACUsersMessage0,
ACUsersMessage1,
uiACU.ButtonSet.OK_CANCEL);
if (result == uiACU.Button.OK) {
for (i=0; i<uploadRows;i++){
var uniqueId=[i][0];
var mailAdress=values[i][3];
var voorNaam=values[i][1];
var achterNaam=values[i][2];
var Ou=values[i][8];
var Pass=values[i][4];
Utilities.sleep(12);
AdminDirectory.Users.insert ({
"kind": "admin#directory#user",
"password" : Pass,
"primaryEmail": mailAdress,
"orgUnitPath": Ou,
"changePasswordAtNextLogin": "TRUE",
"name": {
"givenName": voorNaam,
"familyName": achterNaam,
},
"externalIds": [
{
"value": uniqueId,
"type": "account",
"customType": "gappsUniqueId"
}
]
})
ss.getRange("D"+ (i+1)).setBackground("red")
}
} else {
//Full Stop
}
}