Created
October 7, 2016 14:32
-
-
Save Shiggiddie/9b02ec05f5102ab8613295a7b65eaf4a to your computer and use it in GitHub Desktop.
Cooking with GAS - Subverting User-permission Authorization in Spreadsheet onEdit function
This file contains 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
// Ideally one would get user information from onEdit using onEdit's event's .user attribute, | |
// however GAS does not give authorization to do so for the onEdit function (without some minor caveats). | |
// The following subverts this slightly by allowing the user to include their own user information that is | |
// then stored on the user's properties for the file/script. | |
function onEdit(e) { | |
var user = PropertiesService.getUserProperties().getProperty('user'); | |
if (!user) { | |
var ui = SpreadsheetApp.getUi(); | |
while (!user) { | |
var response = ui.prompt('Please enter your name:'); | |
user = response.getResponseText(); | |
} | |
// This next line is cruital to not spamming the user each time they edit a cell | |
PropertiesService.getUserProperties().setProperty('user', user); | |
} | |
// Set a comment on the edited cell to indicate when it was changed. | |
var range = e.range; | |
range.setNote('Value updated to: ' + e.value + '\nBy user: ' + user + '\n' + 'Modified at: ' + new Date()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment