We start by getting the email address from the active tutor ('getActiveUser') logged into the spreadsheet. We then get details from the Tutors sheet (which was populated during the Bulk add Drive permissions post). Now the relevant tutor name can be acquired from an email address if match (see Lookup email address post for further details). Back on the Group Feedback tab it is necessary to 'split' the Tutor 1: and Tutor 2: strings in turn so that we just have the names (so John Smith rather than Tutor 1: John Smith as in my example). A simple if can now determine whether there is a match and return the tutorName variable.
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
function getActiveUser(sheet) { | |
// get active Tutor email address | |
var user = Session.getActiveUser().getEmail(); | |
Logger.log('Active Tutor email is: ' + user); | |
// get data from Tutors sheet to lookup name from email address | |
var tutorsSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Tutors'); | |
var tutorsSheetData = tutorsSheet.getRange(2, 1, 2, 2).getValues(); | |
// lookup active Tutor email address for corresponding name in Tutors sheet | |
var tutorName = ''; // create empty variable | |
if (user == tutorsSheetData[0][0]) { | |
Logger.log(user + ' matches ' + tutorsSheetData[0][0]); | |
var tutorName = tutorsSheetData[0][1]; | |
Logger.log('Tutor name is therefore: ' + tutorName); | |
} | |
else if (user == tutorsSheetData[1][0]) { | |
Logger.log(user + ' matches ' + tutorsSheetData[1][0]); | |
var tutorName = tutorsSheetData[1][1]; | |
Logger.log('Tutor name is therefore: ' + tutorName); | |
} | |
// get Tutor 1 name from sheet, split just for name | |
var tutor1Name = sheet.getRange(1, 3).getValue().split(': '); | |
Logger.log('Tutor 1 name is: ' + tutor1Name[1]); | |
// get Tutor 2 name from sheet, split just for name | |
var tutor2Name = sheet.getRange(1, 7).getValue().split(': '); | |
Logger.log('Tutor 2 name is: ' + tutor2Name[1]); | |
// find out if user is Tutor 1 or Tutor 2 | |
if (tutorName == tutor1Name[1]) { | |
Logger.log(tutorName + ' matches ' + tutor1Name[1]); | |
var tutorName = 'You are 1'; | |
} | |
else if (tutorName == tutor2Name[1]) { | |
Logger.log(tutorName + ' matches ' + tutor2Name[1]); | |
var tutorName = 'You are 2'; | |
} | |
else { | |
Logger.log('No one matches'); | |
} | |
return tutorName; | |
} |
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
function testGetActiveUser() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var groupSheet = ss.getSheetByName('Group Feedback'); | |
// run function to determine if Tutor 1 or 2 | |
var whoAmI = getActiveUser(groupSheet); | |
Logger.log('Value of whoAmI is: ' + whoAmI); | |
} |
No comments:
Post a Comment