I have a sheet that I want multiple users in my organisation to use as a dashboard, with the data displayed changing depending on the logged-in user. Everything works fine, and I have a simple script triggered onOpen that populates the current user's email address into the sheet and all data filtering is done based on it:
function getEmail () {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeRange = activeSheet.getActiveRange();
activeSheet.getRange(3, 3).setValue(Session.getActiveUser());
Logger.log(Session.getActiveUser());
}
The problem is that because the trigger is set to onOpen, if one user opens the sheet and, while the sheet is still open for the first user, a second user then opens the sheet, the onOpen trigger runs not just for the second user who just opened the sheet, but also for the first user, who's data now changes to display the data only meant for the second user.
In other words, the onOpen trigger is executed each time the sheet is opened for all users with the sheet currently open, not just for the user who just opened the sheet.
Is there a way to ensure the onOpen trigger is only executed for a user the first time that user opens the sheet - not any user? If not - can anyone think of a solution to this other than having a different sheet for each user?
activeSheet.getRange(3, 3).setValue(Session.getActiveUser());
. This modifies the spreadsheet - for all users - overwriting the value with the name of the (last) user running the script.