I'm new to this area, but I'm currently working on a lock
sheet for the user
. The script is working if I'm the owner
so the only problem is if I run it on the user side. I read somewhere that I need to use a Web App
or doGet
function for the sheet to read the script as the owner. I tried these solutions on these threads, but I still can't make it work. I don't know if the script is the problem or I'm the problem so that's why I'm asking this here. I'm hoping that someone can help me with this matter.
Here a replicate spreadsheet that I'm working on: Spreadsheet
Threads that I tried:
- How to enable not authorized users to protect the spreadsheet
- Working with Protected Sheets and Ranges “View Only” mode
- Google Script having a conflict with scripts
- Google Sheets - Run script with Sheet Protection
Here's my script for the Lock function
function Lock(sheetName1) {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName1);
var protection = sheet1.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection && protection.canEdit() && sheet1.protect().setDescription('Sample protected range')) {
protection.remove();
}
var confirm = Browser.msgBox('Confirmation','Are you sure you want to lock this sheet?',Browser.Buttons.YES_NO);
if(confirm=='yes'){
LockSheet(sheet1);
Browser.msgBox("The sheet is locked!");
}
}
function LockSheet(sheet1) {
var protection = sheet1.protect().setDescription('Sample protected sheet');
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}