Funções para serem utilizadas dentro de um GoogleSheets.
Created
January 23, 2023 15:20
-
-
Save augustohp/59e383644c6ee83515ec8beb4526a7a2 to your computer and use it in GitHub Desktop.
Coleção de AppScripts para serem usados com Google Sheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Popula, num Sheets vinculado, a lista de usuários de um domínio (Google Workspace). | |
* | |
* @link https://developers.google.com/admin-sdk/directory/reference/rest/v1/users | |
* @link https://www.labnol.org/code/20494-spreadsheet-list-users-google-apps-domain | |
*/ | |
function getDomainUsersList() { | |
const settings = { | |
domain: "example.org", // GoogleApps domain name | |
sheetName: "Contas", // Will be created if doesn't exists | |
maxResults: 100 | |
}; | |
if (settings.sheetName.length == 0|| settings.domain.length == 0) { | |
throw new Error("Settings need to be set up properly."); | |
} | |
var users = []; | |
var usersListOptions = { | |
domain: settings.domain, | |
customer: "my_customer", | |
maxResults: settings.maxResults, | |
projection: "basic", // Fetch basic details of users | |
viewType: "admin_view", | |
orderBy: "email", // Sort results by user's emails | |
}; | |
do { | |
var response = AdminDirectory.Users.list(usersListOptions); | |
response.users.forEach(function (user) { | |
users.push([ | |
(user.thumbnailPhotoUrl) ? '=IMAGE("'+user.thumbnailPhotoUrl+'",1)' : "", | |
user.name.fullName, | |
user.primaryEmail, | |
user.creationTime, | |
user.lastLoginTime, | |
user.isAdmin, | |
user.suspended | |
]); | |
}); | |
// For domains with many users, the results are paged | |
if (response.nextPageToken) { | |
usersListOptions.pageToken = response.nextPageToken; | |
} | |
} while (response.nextPageToken); | |
// Insert data in a spreadsheet | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheetByName(settings.sheetName) || ss.insertSheet(settings.sheetName, 1); | |
sheet.getRange(2, 1, users.length, users[0].length).setValues(users); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment