Does anyone know if there's a way to access the old (ie, pre-edit) formula, as opposed to oldValue, of the edit event object? I thought e.oldFormula would work, but it's undefined and not even in the documentation.
I'm working on a script to require a password when editing certain ranges but, in order to undo the edit if the user fails to provide the correct password, I need to know what was in the cell before. For cells that contain numbers or strings, e.oldValue works great, but most of the cells I want to protect contain formulas (that's why I want to protect them) so e.oldValue stops the data from updating like it should.
Here's the code I've got that only works if the cell contains no formulas (other than that, it works great:
// Prompt for password before allowing edit of formula cells
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getActiveCell();
var editRange = e.range;
var editCol = editRange.getColumn();
if (editCol != 4 ) {
var password = "321"; // not actual password
var passwordAttempt = Browser.inputBox('Enter Password to edit protected range, or hit cancel to cancel edit:', Browser.Buttons.OK_CANCEL);
if(passwordAttempt == password) {
Browser.msgBox('Edit Accepted');
} else {
if (passwordAttempt != 'cancel'){
Browser.msgBox('Incorrect password. Contact James Atkins for password to edit protected ranges.');
}
if(e.oldValue){
Browser.msgBox('old value is defined as ' + e.oldFormula);
e.range.setValue(e.oldValue);
} else {
e.range.clear();
}
}
FYI, I'm doing this because the built-in protected ranges in Google Sheets don't distinguish between a user changing the contents of a cell by manually editing it and by activating a script that changes the contents. I want to allow all users to activate scripts that sort and clear certain ranges, but not let them mess with the contents manually.