我正在尝试创建一个 Google Apps脚本,它会在我们的域中编制所有 Gmail用户的列表,并在 Google表格<中填充信息/强>
我想要检索的一些用户信息如下:
有没有人有类似的东西或可以帮助我开始的GAS?
谢谢!
Amit Desai。
答案 0 :(得分:0)
这是一个几乎可以满足您所需要的脚本。它包括一些辅助功能,以获得更好的布局和可读性。
注意:AdminDirectory是您必须激活的高级Google服务(脚本编辑器&gt;资源&gt;控制台中的高级Google服务+管理SDK,如上所述)
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");
}
}
答案 1 :(得分:0)
您需要将Domain替换为客户
function writeToSpreadsheet(){
var values = [],
users = [],
userListQuery = {},
nextPageToken = '',
listObject = {
customer : 'customer ID' //you need to replace this with your customer ID
maxResults: 500,
},
i = 0,
activeSpreadsheet;
do {
if (nextPageToken && nextPageToken !== '') {
listObject.pageToken = nextPageToken;
}
userListQuery = AdminDirectory.Users.list(listObject);
// if there are more users than fit in the query a nextPageToken is returned
nextPageToken = userListQuery.nextPageToken;
// Add the query results to the users array
users = users.concat(userListQuery.users);
} while (nextPageToken);
for (i = 0; i < users.length; i += 1) {
values.push([users[i].name.fullName, users[i].primaryEmail]);
}
SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange(2, 1, values.length, values[0].length).setValues(values);
}