将所有用户别名下载到Google电子表格

时间:2019-03-16 01:52:26

标签: email google-apps-script google-sheets google-admin-sdk

我正在尝试列出我们网络中的所有用户别名。但是,我有点麻烦。我在这里使用一些固定的代码。我知道别名是一个数组,但按用户访问它,然后仅列出别名使我看到两倍。任何帮助或提示,我们将不胜感激!

  function AliasDomainUsersList() {

  var users_alias = [];
  var options_alias = {
    domain: "northstarmoving.com",     // Google Apps domain name
    customer: "my_customer",
    maxResults: 100,
    projection: "full",      // Fetch basic details of users
    viewType: "domain_public",
    orderBy: "email"          // Sort results by users
  }

  do {
    var response = AdminDirectory.Users.list(options_alias);
    response.users.forEach(function(user) {
      users_alias.push([user.name.fullName, user.primaryEmail]);
    });

    // For domains with many users, the results are paged
    if (response.nextPageToken) {
      options_alias.pageToken = response.nextPageToken;
    }
  } while (response.nextPageToken);

  // Insert data in a spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Users-Aliases") || ss.insertSheet("Users-Aliases", 1);
  sheet.getRange(1,1,users_alias.length, users_alias[0].length).setValues(users_alias);

}

2 个答案:

答案 0 :(得分:0)

我认为类似的方法可能有效。我尚未对此进行测试,因为我是我的公司帐户中唯一的人。

function AliasDomainUsersList() {
  var users_alias = [];
  var options_alias = {
    domain: "northstarmoving.com",     // Google Apps domain name
    customer: "my_customer",
    maxResults: 100,
    projection: "full",      // Fetch basic details of users
    viewType: "domain_public",
    orderBy: "email"          // Sort results by users
  }

  do {
    var response = AdminDirectory.Users.list(options_alias);
    for(var i=0;i<response.users.length;i++) {//you will probably want to do this your way. 
      users_alias.push([response.users[i].name,response.users[i].primaryEmail]);
    }
    // For domains with many users, the results are paged
    if (response.nextPageToken) {
      options_alias.pageToken = response.nextPageToken;
    }
  } while (response.nextPageToken);

  // Insert data in a spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Users-Aliases") || ss.insertSheet("Users-Aliases", 1);
  sheet.getRange(1,1,users_alias.length, users_alias[0].length).setValues(users_alias);
  var sheet2=ss.getSheetByName('AnotherSheet')
  var rg2=sheet2.getRange(1,1,users_alias.length,1);
  var vA2=rg2.getValues();
  for(var i=0;i<vA2.length;i++) {
    vA2[i][0]=users_alias[i][1];
  }
  rg2.setValues(vA2);
}

答案 1 :(得分:0)

设法制定解决方案(在一些帮助下)。在这里:

  function getDomainUsersList() {

  var users = [];
  var options = {
    domain: "northstarmoving.com", // Google Apps domain name
    customer: "my_customer",
    maxResults: 500,
    projection: "full",      // Fetch details of users
    viewType: "domain_public",
    orderBy: "email"          // Sort results by users
  }

  do {
    var response = AdminDirectory.Users.list(options);
    response.users.forEach(function(user) {
      users.push([user.name.fullName, user.primaryEmail]);
    });

    // For domains with many users, the results are paged
    if (response.nextPageToken) {
      options.pageToken = response.nextPageToken;
    }
  } while (response.nextPageToken);

  // Insert data in a spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Users") || ss.insertSheet("Users", 1);
  sheet.getRange(1,1,users.length, users[0].length).setValues(users);

}