How to password protect a google sheet

pervis

I have a Google Sheet with data that is protected - the user can't edit the sheet. When the AskUser function is ran, a pop-up box appears that asks the user if they wish to "unprotect" the sheet. When they click "No", they will be told nothing will happen. When they click "Yes", they will be told they need to submit a password. I have three problems:

  1. I don't know how to set a password.

  2. I don't know how bring up a dialogue box where the user can submit a password.

  3. I don't know how to make it check if it's the correct password.

I've tried downloading pre-password protected sheets and checking the code, but they use a weird HTML format. Here's what I have so far:

  SpreadsheetApp.getUi() 
      .createMenu('Custom Menu')
      .addItem('Show alert', 'showAlert')
      .addToUi();
}

function AskUser() {
  var ui = SpreadsheetApp.getUi(); 

  var result = ui.alert(
     'Worksheet Protected!',
     'Do you want to unprotect the worksheet?',
      ui.ButtonSet.YES_NO);


  if (result == ui.Button.YES) {
    //This is where I want the password dialogue box prompt
    ui.alert('The worksheet will be unprotected after you submit the right password.');
  } else {

    ui.alert('The worksheet will not be unprotected.');
  }
}

I'm completely new to Javascript and Google Sheets.

WilliamNHarvey

Setting a password

Use PropertiesService to store a password

function setPassword(password) {
  let prop = PropertiesService.getUserProperties();
  prop.setProperty("sheetpassword", password);
}

Bringing up password form

Create an html form in a file, eg. passwordform.

Then when you want to show the form, call createHtmlOutputFromFile

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(HtmlService.createHtmlOutputFromFile('passwordform'));

On form submit, call the check password function

onclick='google.script.run.checkPassword(document.getElementById("passwordInput"));'

Checking password

Get the property from PropertiesService and see if it equals

function checkPassword(password) {
  let prop = PropertiesService.getUserProperties();
  if (prop.getProperty("sheetpassword") === null || prop.getProperty("sheetpassword") !== password) {
    return false; // Doesn't match DB
  } 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to protect a sheet via Google Sheets API?

Password protect google chrome

How to protect password in python

How to password protect my excel sheet after saving them using vb script

how do I use the text in a txt file, as the password to protect and unprotect sheet?

Protect specified range in google Sheet with GAS

Protect specified cell in google Sheet with GAS

Protect a range of a google sheet when a checkbox is checked

how to password protect Elastic search

How to protect password and username in Sqoop?

How to protect swagger documentation with password

How to protect a PDF with a username and password?

How to password protect Grub menu

How can I get this to protect a sheet on scripting basis (Google Apps Script)?

Apache POI - How to protect sheet with options?

How to protect a sheet then unprotect specific cells

How to send Password Protect SMS/Text Message

How to password protect printing on a HP Officejet?

How to password protect gzip files on the command line?

How to password protect a zipped Excel file in Java?

How can I password protect localstorage data?

How to protect SqlLocalDB database file with custom password

How to password protect a folder in Windows Explorer?

How to create a secure login to protect the password information

How to Protect Grub Boot Loader by Password

How do I protect a password stored on server?

How do I protect my password on the website?

Password Protection for Exported Data from Google Sheet

Can I protect the name of a Google sheet? Or reference the sheet by position rather than name?