Google Sheets Password Protect Script? Real Fixes

How-to basics · Updated November 23, 2026

Yes, you can password-protect a Google Sheets script, but not with a single built-in toggle. The practical method is to use Google Apps Script’s LockService combined with a custom password prompt, or to restrict access to the script itself through the container’s sharing settings. Below is the full breakdown of how script protection actually works in Google Sheets, what it can and cannot do, and step-by-step ways to secure your code and its execution.

What “Password Protect Script” Actually Means

When people search for “password protect script,” they usually want one of three things:

  • Prevent others from viewing or editing the script code – the Apps Script project attached to a Google Sheet.
  • Require a password before a script runs – for example, a custom menu item that only authorized users can click.
  • Restrict who can trigger or execute the script – using Google’s built-in authorization and sharing controls.

Google Sheets does not have a “set password on this macro” feature like Excel’s VBA project password. Instead, you combine Google’s sharing permissions, Apps Script’s authorization system, and a bit of custom code to achieve similar results.

How Google Sheets Script Access Works

Every script attached to a Google Sheet lives in an Apps Script project. That project is tied to the Google Drive file (the sheet). The script’s visibility follows the sheet’s sharing settings:

  • If the sheet is shared with “Viewer” or “Commenter,” those users can open the script editor (Extensions > Apps Script) and see the code, unless the script project has its own sharing restrictions.
  • If the sheet is shared with “Editor,” those users can edit the script code as well.
  • Scripts run under the authority of the user who triggers them (for simple triggers) or under the authority of the person who installed the trigger (for installable triggers).

To prevent someone from viewing your script, you must change the script project’s sharing settings separately. In the Apps Script editor, click the “Share” button (top-right), then under “General access,” choose “Restricted.” Only people explicitly added can open the script project. However, if those people are also editors of the sheet, they can still open the script editor and see the code if the project is shared with them. The only way to fully hide code is to not share the script project with anyone except the owner.

Step-by-Step: Restrict Script Viewing

  1. Open your Google Sheet.
  2. Click Extensions > Apps Script.
  3. In the Apps Script editor, click the blue Share button in the top-right.
  4. Under “General access,” select Restricted.
  5. Add only the people who absolutely need to see the code. Do not add general viewers or editors of the sheet.
  6. Click Done.

This prevents anyone who is not explicitly listed from opening the script project. But note: if you later share the sheet with a new editor, that person will not automatically get script access unless you add them to the script project too.

How to Add a Password Prompt to a Script

If you want a script to ask for a password before running a sensitive function, you can write a simple prompt using ui.prompt(). Here’s a basic example:

function protectedAction() {
  var ui = SpreadsheetApp.getUi();
  var password = 'secret123'; // Change this to your own password
  var response = ui.prompt('Enter password to continue:', ui.ButtonSet.OK_CANCEL);
  if (response.getSelectedButton() !== ui.Button.OK) {
    return;
  }
  var entered = response.getResponseText();
  if (entered !== password) {
    ui.alert('Incorrect password. Action cancelled.');
    return;
  }
  // Place your protected code here
  ui.alert('Access granted. Running protected action...');
}

To use this, open the script editor, paste the code, save, and then run protectedAction from the editor or assign it to a custom menu. When run, it will prompt the user for a password. This is not strong security—anyone with edit access to the script can see the password in the code—but it works for casual protection.

For a more secure approach, store the password in a separate Google Sheet or in Script Properties, but know that anyone with script access can still read it. True security requires server-side validation, which is beyond a simple sheet script.

Using LockService to Prevent Unauthorized Concurrent Runs

Sometimes “password protect” really means “prevent other users from running the script at the same time” or “make sure only one person can run it.” Google Apps Script offers LockService to handle concurrency. It doesn’t ask for a password, but it ensures that only one execution of a critical section happens at a time:

function criticalUpdate() {
  var lock = LockService.getScriptLock();
  lock.waitLock(30000); // Wait up to 30 seconds for the lock
  try {
    // Your protected logic here
    SpreadsheetApp.getActiveSpreadsheet().getRange('A1').setValue('Updated');
  } catch (e) {
    console.error(e);
  } finally {
    lock.releaseLock();
  }
}

This is useful when multiple users might trigger the same script simultaneously, causing data conflicts. It does not restrict who can run the script, but it serializes runs.

Common Problems and Misconceptions

“I set the sheet to view-only, so my script is safe.” Not true. Viewers can still open the script editor and see the code if the script project is shared with them. You must restrict the script project separately.

“I can password-protect a macro like in Excel.” Excel allows you to lock VBA code with a password. Google Sheets has no equivalent. The closest is restricting the script project’s sharing, but even then, the owner can always see the code.

“If I hide the script, the sheet still works for others.” Yes, but only if the script is deployed as an add-on or as a web app with its own authorization. For a simple bound script, users need access to the script to run it, unless you use installable triggers that run under your account.

“Can I make a script run only for certain email addresses?” Yes, you can check the user’s email in the script using Session.getActiveUser().getEmail() and compare it to an allowlist. This is a common pattern:

function restrictedFunction() {
  var email = Session.getActiveUser().getEmail();
  var allowed = ['boss@example.com', 'admin@example.com'];
  if (allowed.indexOf(email) === -1) {
    throw new Error('Access denied for ' + email);
  }
  // rest of the function
}

This is more reliable than a password prompt because the email is tied to the Google account.

Alternatives: Web Apps and Add-ons

For stronger protection, consider deploying your script as a web app. A web app runs on Google’s servers, and you can control access by setting “Execute as” to “Me” and “Who has access” to “Only myself” or “Anyone with Google account.” Users interact with a web interface, not the sheet directly, so they never see the code. This is the recommended way to protect sensitive logic.

Another option is to publish the script as an add-on for your domain. Add-ons are reviewed by Google and have their own authorization scopes; users install them without seeing the source code. However, developing an add-on is more complex and requires a Google Cloud project.

Frequently Asked Questions

Can I password-protect a Google Sheets script like in Excel VBA?
No. Google Sheets does not have a built-in feature to lock script code with a password. The closest you can get is restricting the script project’s sharing settings to prevent others from opening it, or using a custom password prompt in the code.

How do I stop someone from viewing my script code?
In the Apps Script editor, click Share, then set General access to Restricted. Only add people who must see the code. Remember that if you share the sheet with editors, they won’t automatically get script access unless you add them to the script project.

Is a password prompt in a script secure?
No. Anyone with edit access to the script can read the password in the code. For sensitive operations, use an email allowlist with Session.getActiveUser().getEmail() or deploy a web app with restricted access.

What is LockService used for?
LockService prevents concurrent executions of a script. It’s useful when multiple users might trigger the same script at the same time, causing data conflicts. It doesn’t restrict who can run the script, only that runs happen one at a time.

Can I make a script run only for specific users?
Yes. Check the user’s email in the script and compare it to an allowlist. If the email isn’t allowed, throw an error or return early. This works for both simple and installable triggers.

What’s the best way to protect a sensitive script?
Deploy it as a web app with “Execute as” set to “Me” and access restricted to yourself or specific Google accounts. This keeps the code on the server, and users only see the web interface, not the script.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.