Reputation: 195
I'm looking for a script to launch a popup prompting the user for a code on startup, and it will keep all the sheets hidden but one until the user enters the correct code, if the user does not enter the correct code it will keep launching the user prompt and keep all the sheets hidden, pretty much a turn around for a password protection, this is what I have so far:
function onOpen() {
function hideAllSheetsExcept('Sheet1') {
var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();
for(var i =0;i<sheets.length;i++){
Logger.log(i);
if(sheets[i].getName()!=sheetName){
sheets[i].hideSheet();
}
}
var quest = Browser.inputBox("Enter the requested value");
if quest == '12345' then
sheets[i].showSheet();
}
}
Upvotes: 0
Views: 486
Reputation: 26836
Your code already goes into the right direction but needs the following modifications:
onOpen
through an installable one. Browser.inputBox
will not run on simple trigger.if
statement that checks for the correct password by a while
loop that keeps asking for the password as long as it is not correct.hideAllSheetsExcept
should not be nested in onOpen
, but only be called from there.See the following sample of how you can implement the desired functionality, when binding to the function called
openSheets
an installableonOpen
trigger:
function openSheets(){
hideAllSheetsExcept('Sheet1')
}
function hideAllSheetsExcept(sheetName) {
var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();
for(var i =0;i<sheets.length;i++){
Logger.log(i);
if(sheets[i].getName()!=sheetName){
sheets[i].hideSheet();
}
}
var quest="";
while (quest!='12345'){
quest = Browser.inputBox("Enter the requested value");
}
for(var i =0;i<sheets.length;i++){
sheets[i].showSheet();
}
}
Upvotes: 2