Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Lock a Column in Google Sheets with Apps Script

· 6 min read · Home Tech

In this article
  1. What column protection does in Google Sheets
  2. Set up and run the script
  3. Choose who can edit the protected column
  4. Target a different column or sheet
  5. Remove or update a protection
  6. Common issues and limitations
  7. Frequently Asked Questions

To lock a column in Google Sheets with a script, use Google Apps Script to create a protected range. Open Extensions > Apps Script, add a function such as the example below, save it, and run it; the script protects column C on the active sheet so only the spreadsheet owner can edit it by default.

function protectColumnC() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const protection = sheet.getRange('C:C').protect();
  protection.setDescription('Protect column C');
  protection.setWarningOnly(false);
}

This applies Google Sheets range protection, not a lock that encrypts or hides the column. Protected ranges help control editing by collaborators, but they do not prevent people with sufficient access from viewing or copying the data. Choose the permissions carefully, and test the script on a copy if the sheet is important.

What column protection does in Google Sheets

A protected range restricts who can edit the selected cells. In Sheets, protection is a collaboration control: authorized editors may still edit, while other editors are blocked or shown a warning, depending on the protection settings. It is useful for keeping formulas, headings, reference values, or approved inputs from being changed accidentally.

Protection is different from hiding a column. Hiding changes what appears on screen but does not restrict edits or access to the underlying values. Protection is also not a confidentiality feature: spreadsheet editors may be able to see the protected data, and users with sufficient permissions can change or remove protections. Do not use it to store passwords, private records, or other information that must be confidential.

The script’s getRange('C:C') selects the whole of column C on the chosen sheet. You can use a narrower range, such as C2:C100, if the heading should remain editable or you only want to protect the rows currently in use.

Set up and run the script

  1. Open the Google spreadsheet in a web browser and choose Extensions > Apps Script.
  2. In the script editor, add the function from the example. If the project already contains code, add the function without deleting unrelated code.
  3. Save the project, then select protectColumnC in the function selector and click Run.
  4. On the first run, review the authorization prompt and grant the requested permissions if you trust the script. Apps Script needs spreadsheet access to create the protection.
  5. Return to the sheet and check the protected-range indicator or open Data > Protect sheets and ranges to review the protection.

The function uses the active sheet, meaning the sheet selected when it runs. If a spreadsheet has several tabs, confirm the correct tab is active first. To target a sheet by name instead, replace the first line inside the function with const sheet = SpreadsheetApp.getActive().getSheetByName('Data');, using the exact tab name in place of Data. If that name is wrong, the script will not obtain a sheet to protect.

Choose who can edit the protected column

Calling protect() creates a protection object. By default, the protection is not warning-only; the example makes that behavior explicit with setWarningOnly(false). It does not, by itself, define a custom list of allowed editors. Review the range in the Sheets protection panel and configure its permissions for the people who should retain editing access.

To set editors through Apps Script, you can remove existing editors from the protection and add specific email addresses. For example, after creating the protection, add these lines to the function:

protection.removeEditors(protection.getEditors());
protection.addEditor('editor@example.com');

Replace the sample address with the account that should be allowed to edit. The person running the script must have sufficient spreadsheet permissions, and Google Workspace policies or account settings may affect who can be added. Do not assume that adding one editor automatically removes every other form of access; check the protection’s editor list and test using a suitable account.

If you want a warning rather than a hard restriction, use protection.setWarningOnly(true). Editors will be warned when they try to change the range, but they can proceed. Warning-only mode is useful as a reminder, not as a reliable way to preserve data.

Target a different column or sheet

Change the range string to match what you want to protect. For instance, 'A:A' selects all of column A, while 'D2:D' starts at D2 and continues down the column. A bounded range such as 'B2:B500' can be easier to manage when only a known set of rows should be protected.

For a named sheet, combine getSheetByName() with the range selection. A complete version is:

function protectInputColumn() {
  const spreadsheet = SpreadsheetApp.getActive();
  const sheet = spreadsheet.getSheetByName('Data');
  if (!sheet) throw new Error('Sheet named Data was not found.');
  const protection = sheet.getRange('C2:C').protect();
  protection.setDescription('Protect data-entry column');
  protection.setWarningOnly(false);
}

This protects column C from row 2 onward on the tab named Data, leaving its first row outside the protected range. It does not automatically expand a bounded range if you choose one; an open-ended range such as C2:C covers cells below the current data as well.

Remove or update a protection

You can manage protections without code through Data > Protect sheets and ranges. Select the relevant protected range in the sidebar to change its description, edit permissions, or remove the protection. This is often the simplest option for a one-time adjustment.

For a script, the protection object returned by protect() can be used to change settings. If you need to remove a protection programmatically, first find the matching protection rather than removing every protection on the sheet. Multiple ranges may have different purposes and editors. A safe workflow is to give each protection a distinct description, inspect existing protections, then update only the one with the description you expect.

Running the sample function repeatedly can create additional protections on the same range. It is not an idempotent setup script: repeated runs may leave duplicate entries to review. Before automating it on a schedule or calling it from another function, check for an existing protection and update it instead of blindly creating another.

Common issues and limitations

  • The wrong tab was protected: the first example uses the active sheet. Select the intended tab or target it by name.
  • The script asks for authorization: this is expected the first time a project uses spreadsheet services. Only authorize code you understand and trust.
  • Some collaborators can still edit: check the protected range’s allowed editors and the account’s spreadsheet permissions. Protection does not override all owner or administrator capabilities.
  • The column is still visible: protection controls editing, not viewing. Use sharing permissions to control who can open the spreadsheet.
  • Repeated runs create clutter: inspect the protection panel and remove duplicates or revise the script to find and update an existing range.

Google Sheets’ built-in range protection is often enough for a stable column. A script is most useful when you need to apply the same setup repeatedly, target sheets by name, or integrate protection into a larger workflow. For a simple one-off change, the menu avoids maintaining code and is easier for other spreadsheet editors to understand.

Frequently Asked Questions

Can I lock a column so nobody can edit it?
Protect the range and configure its allowed editors. The spreadsheet owner and users with sufficient administrative or ownership permissions may still be able to change protections, so this is not an absolute lock against every privileged user.

Can I protect a column but leave the header editable?
Yes. Select a range starting below the header, such as C2:C, instead of protecting C:C.

Does protecting a column hide its contents?
No. Protected ranges restrict editing; they do not conceal values. Control access to the spreadsheet itself if readers should not see the data.

Can I lock a column in Google Sheets without Apps Script?
Yes. Use Data > Protect sheets and ranges, add a range such as C:C, and set its permissions in the sidebar. Apps Script is optional.

Will this script work in Excel?
No. It uses Google Apps Script and Google Sheets services. Excel uses different protection features and automation tools, so this code cannot be pasted into an Excel workbook as-is.