Here is a script that does almost all you need. It includes a couple of helper functions to get a better layout & readability.
Note : AdminDirectory is an advanced Google service that you have to activate (script editor>ressources>Advanced Google Services + Admin SDK in console as explained)
Code :
function listAllUsers() {
var ss = SpreadsheetApp.getActive();
var pageToken, page, count=0;
var listArray = [];
listArray.push(['num','first name','last name','email','alias []','creation date','is connected','last connection','ID'])
do {
page = AdminDirectory.Users.list({
domain: 'insas.be',
orderBy: 'givenName',
maxResults: 100,
pageToken: pageToken
});
var users = page.users;
if (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
count++;
listArray.push([count, user.name.givenName, user.name.familyName, user.primaryEmail,user.aliases!=null?user.aliases:'',new Date(user.creationTime),user.agreedToTerms==true?"oui":"non",new Date(user.lastLoginTime),user.id,]);
}
} else {
Logger.log('No users found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
try{
var outputSheet = ss.getSheetByName('allMembers');
outputSheet.getDataRange();
}catch(err){
var outputSheet = ss.insertSheet('allMembers',2);
}
outputSheet.getDataRange().clear();
outputSheet.getRange(1,1,listArray.length, listArray[0].length).setValues(listArray);
outputSheet.getRange(1,6,outputSheet.getLastRow(),4).setHorizontalAlignment("center");
outputSheet.getRange(1,1,outputSheet.getLastRow(),1).setHorizontalAlignment("center");
var width = [40,150,180,250,250,95,60,125,160];
formatSheet(outputSheet,width);
}
function formatSheet(outputSheet,width){
SpreadsheetApp.flush();
if(outputSheet.getMaxRows() > outputSheet.getLastRow()){
outputSheet.deleteRows(outputSheet.getLastRow()+1, outputSheet.getMaxRows()-outputSheet.getLastRow());
};
SpreadsheetApp.flush();
if(outputSheet.getMaxColumns() > outputSheet.getLastColumn()){
outputSheet.deleteColumns(outputSheet.getLastColumn()+1, outputSheet.getMaxColumns()-outputSheet.getLastColumn());
};
outputSheet.getRange(1,1,1,outputSheet.getLastColumn()).setBorder(true,true,true,true,true,true).setBackground("#CCA").setFontWeight("bold").setHorizontalAlignment("center");
outputSheet.setFrozenRows(1);
SpreadsheetApp.flush();
for(var w=0;w < width.length;w++){
outputSheet.setColumnWidth(w+1,width[w]);
}
shader(outputSheet);
}
function shader(sh) {
sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).setBackground(null)
var L=sh.getLastRow();
var C = sh.getLastColumn();
for(var n=3;n<L;n=n+2){
sh.getRange(n,1,1,C).setBackground("#DDD");
}
}