使用名字

时间:2018-06-11 23:56:22

标签: google-apps-script gsuite

我正在尝试使用从电子表格中获取的他/她的名字从admin目录中获取用户的电子邮件ID,该电子表格只检查目录中是否存在电子邮件ID,如果是,则显示他/她的电子邮件ID,这是我的代码:

function scanSsheet(){
  var originalSpreadsheet = SpreadsheetApp.openById('xxxxxxxx-xxxxx-xxxxxxx-xxxx').getSheetByName("Form Responses 1");
  var getRange = originalSpreadsheet.getDataRange();
  var data = originalSpreadsheet.getDataRange().getValues();
  for (var i = 1; i < data.length; i++) {
    var volunteerFirstName = data[i][3];
    var volunteerLastName = data[i][4];

    Logger.log('****************Record No: ' + i + '****************');
    Logger.log('volunteerFirstName: ' + data[i][3]);
    Logger.log('volunteerLastName: ' + data[i][4]);

    Logger.log('fetchUser is called');
    var checkUser = fetchUser(volunteerFirstName);
    if(checkUser){
      Logger.log('Email exists');
    }
    else{
      Logger.log('The user doesnot exist in the directory');
    }

  }
}

//Checks if the user exists in the directory or not and display email id if yes
function fetchUser(volunteerFirstName){
   var isUser
   try{
    var email = AdminDirectory.Users.list({givenName: volunteerFirstName}); // This line should fetch email but it is not working
    var user = AdminDirectory.Users.get(email);

    Logger.log('email:' +email);
    isUser = true;
   } catch (e){
    isUser = false;
   }
 return isUser;
} 

如上所示,我使用了var email = AdminDirectory.users.list({givenName: volunteerFirstName}),就像我在G-suite admin-sdk后面的代码中看到的那样,但是我无法检索电子邮件ID,我做错了什么这里?此外,我是google-apps脚本的新手,请忽略它是否是一个愚蠢的问题。

1 个答案:

答案 0 :(得分:0)

我能够使用users-list检索它和其他一些细节:

function scanSsheet(){
  var originalSpreadsheet = SpreadsheetApp.openById('xxxxxxxx-xxxxx-xxxxxxx-xxxx').getSheetByName("Form Responses 1");
  var getRange = originalSpreadsheet.getDataRange();
  var data = originalSpreadsheet.getDataRange().getValues();
  for (var i = 1; i < data.length; i++) {
    var volunteerFirstName = data[i][3];
    var volunteerLastName = data[i][4];

    Logger.log('****************Record No: ' + i + '****************');
    Logger.log('volunteerFirstName: ' + data[i][3]);
    Logger.log('volunteerLastName: ' + data[i][4]);

    Logger.log('fetchUser is called');
    fetchUser(volunteerFirstName);

  }
}

function fetchUser(volunteerFirstName){
   var pageToken;
   var membersList = AdminDirectory.Users.list({
       domain: 'mydomain.com',
       orderBy: 'email',
       query: volunteerFirstName,
       maxResults: 100,
       pageToken: pageToken
   });  
   Logger.log('membersList:' +membersList);
   } 

感谢。