I learned from [this question] (Automatic Lock/Protect Rows/cells in Google Sheets at a specific time in other Cells) on how to lock certain cells with certain conditions and Jess's answer works perfectly fine.
I am just wondering if I could add more codes to unlock the cells at a certain conditions as well. e.g: if users do thing1, lock the cell B1, if users do thing2, unlock the cell B1 for future editing.
Here the code I revised based on Jess's answer, the blocking (removeEditor) works fine, but the "else if" condition I added to unblock the cell did not work, and the cells are NOT unblocked under the condition.
function onEdit(e) {
var ss1=SpreadsheetApp.getActiveSpreadsheet();
var sheet1=ss1.getSheetByName("Sheet Name");
var active = sheet1.getActiveCell();
var nextnum = sheet1.getRange(active.getRow(),8).getValue();
var shouldbeblocking= sheet1.getRange(active.getRow()-1,7)
var them = Session.getActiveUser();
var theiremails = them.getEmail();
// to unlock the cell, NOT WORKING
if (nextnum == 22) {
shouldbeblocking.protect().addEditor(theiremails);
shouldbeblocking.setNote("unLocked") ;
}
// to lock the cell (codes working fine)
else if (nextnum == 1) {
shouldbeblocking.protect().removeEditor("[email protected]") ;
shouldbeblocking.setNote("Locked") ;
}
}
As per @ross's suggestion, I added Logger.log
onto my code (within the if statement), and below is the screenshot of the execution transcript, it seems like the code runs through the if statement, and oddly the "unlocked" setNote
works cuz I can see the "unlocked" note on the cell, but that cell is still blocked:
nextnum
ever reaches the22
value; 2)getActiveUser().getEmail()
result is the same as the email inremoveEditor()
? – Oleg ValterLogger.log
inside yourif
statement that you think working to make sure that it is actually getting into that part of the code. Also could you show your execution transcript (go toview > execution transcript
) to view this then add the contents to the question. – rossif
statement (btw, about [2] - are sure or you called something liketheiremails==='[email protected]'
and it returnedtrue
?) – Oleg Valter