使用名字从 AdminDirectory 目录中获取电子邮件 ID
Fetch email ID from AdminDirectory directory using first name
我正在尝试使用从电子表格获得的 his/her 名字从管理目录中获取用户的电子邮件 ID,它只是检查目录中是否存在电子邮件 ID,如果是,则显示 his/her 电子邮件 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 脚本的新手,如果这是一个愚蠢的问题,请忽略。
我能够使用 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);
}
谢谢。
我正在尝试使用从电子表格获得的 his/her 名字从管理目录中获取用户的电子邮件 ID,它只是检查目录中是否存在电子邮件 ID,如果是,则显示 his/her 电子邮件 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 脚本的新手,如果这是一个愚蠢的问题,请忽略。
我能够使用 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);
}
谢谢。