I'm in the process of trying to setup a script that get the context of a user on form submit, takes the user ID (email), searches a spreadsheet to locate the user, and then find the manager's email that the user is associated with. Below is some code i've been developing and I'm able to grab the data from the spreadsheet and create an array and loop over it. What I'm wondering if anyone could help provide suggestions on the best way to structure the spreadsheet to search and locate membership and then how to do that in the script.
function getUser() {
//Uses the base class to get acive user's email
var email = Session.getActiveUser().getEmail();
//Uses the UserManager class to get info by passing in the getActive user from base class
var userFirst = UserManager.getUser(Session.getActiveUser()).getGivenName();
var userLast = UserManager.getUser(Session.getActiveUser()).getFamilyName();
//Go to View then Log to see results
return email;
}
//Function used to look up manager of user submitting PTO request
function getManager() {
var requestor = getUser();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var manager = ss.getSheetByName('Managers');
var values = manager.getDataRange().getValues();
for ( i = 0; i < values.length; i++) {
var obj = values[i];
Logger.log(obj);
for (var j in obj) {
var value = obj[j];
//Logger.log(value);
if (value == requestor){
Logger.log(value);
}
}
I started to thinking have a user list on one sheet and managers on another but wasn't sold on that, as I wasn't sure how to tie the user to a manager. Then thought maybe three columns, each column starts with a manager and each row below the manager's name users names?
I'd like the script to avoid hard coding values. Any thought or suggestion to spark ideas is so welcome.
thanks
UPDATED Ok, so I've worked on the code and now have three functions running. I've also decided to go with using a spreadsheet with two sheets, one for managers and one for employees. The sheets for each are similar. Three columns (Name, email, groupID) The group ID for the employee will match the groupID of a manager to whom that employee belongs. Here is the updated code: getUser() will get the active user, findEmployee() uses getUser to get email then matches it and returns groupID. getMnger() uses the groupID to find managers email.
I'm not sure this is the best way but I'm also learning teaching myself. (only get to play around here and there
function getUser() {
//Uses the base class to get acive user's email
var email = Session.getActiveUser().getEmail();
//Uses the UserManager class to get info by passing in the getActive user from base class
var userFirst = UserManager.getUser(Session.getActiveUser()).getGivenName();
var userLast = UserManager.getUser(Session.getActiveUser()).getFamilyName();
//Go to View then Log to see results
return email;
}
function findEmployee() {
//Gets the active Spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Gets the sheet by name
var employee = ss.getSheetByName('Roster');
//get the values in sheet
var values = employee.getDataRange().getValues();
// makes a call to function getUser() to return email of active user
var user = getUser();
// loops over data values, matches to active user email, returns group ID
for (var i=0; i < values.length; i++) {
for (var j=0; j < values[i].length; j++) {
if ( values[i][j] == user) {
return values[i][2];
//used to verify / debug
Logger.log(values[i][2]);
}
}
}
}
function getMnger(){
//Gets current spreadsheet and locates the managers sheet and grabs the data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var manager = ss.getSheetByName('Managers');
var values = manager.getDataRange().getValues();
var groupID = findEmployee();
//Loops over the data creating associative array
for (var i=0; i < values.length; i++) {
for (var j=0; j < values[i].length; j++) {
if ( values[i][j] == groupID) {
//Used to debug and verify
Logger.log(values[i][1]);
return values[i][1];
}
}
}
}
This is a work in progress. Anyone have thoughts so far on ? My biggest question now is how to return the value I want based on another value. In both findEmployee() and getMnger(), once it matches it returns a hard coded index in values[][]. i.e. return values[i][1]; OR return values[i][2];
Is there a way to ensure that hardcode values are not needed here? Basically i'm looking to take the users and find him in a column on a sheet and then return the corresponding groupID in another column beside his email. Make sense?
thanks!