Sub topic 13
Automating Sharing and Protection with Apps Script
This chapter assumes you have a Google account, can create and open a Google Sheet, and understand sheet tabs and cell ranges. It also assumes basic JavaScript, such as functions, variables, and comments. Apps Script is Google's JavaScript-based automation platform. A script can change sharing settings and protect sheet content, but it runs under an authorized Google account.
How the script works
A spreadsheet has two related objects:
- The
Spreadsheetobject represents the workbook and its sheets. - The Drive
Fileobject represents the file stored in Google Drive.
Spreadsheet sharing methods handle viewers and editors. Drive file methods handle commenters and general link access. The first time you run a script, Google asks you to review and approve its permissions.
Create a bound script by opening the sheet and choosing Extensions > Apps Script. Then add this function:
function shareWithPeople() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addViewer('[email protected]');
ss.addEditor('[email protected]');
// A spreadsheet has no addCommenter method, so use its Drive file.
const file = DriveApp.getFileById(ss.getId());
file.addCommenter('[email protected]');
}Replace the example addresses with real Google accounts. The function adds access. It does not remove existing access, so run it carefully if you need a precise access list.
Setting general access
General access controls what happens when someone has the link but has not been named individually. Use Restricted when only explicitly shared accounts should open the file. Use Anyone with the link when the link itself grants access.
The following function gives anyone with the link Viewer access:
function makeLinkViewOnly() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const file = DriveApp.getFileById(ss.getId());
file.setSharing(
DriveApp.Access.ANYONE_WITH_LINK,
DriveApp.Permission.VIEW
);
}Use DriveApp.Permission.COMMENT or DriveApp.Permission.EDIT when those roles are appropriate. An administrator may block these settings. A work or school account can also limit sharing to people inside the organization.
Protecting a sheet
Sharing an account as Editor gives that account broad access. Protection adds a second control. It can prevent editors from changing a whole sheet or selected range.
This function protects the active sheet and leaves editing available only to the account running the script:
function protectActiveSheet() {
const sheet = SpreadsheetApp.getActiveSheet();
const protection = sheet.protect()
.setDescription('Totals - owner only');
protection.removeEditors(protection.getEditors());
protection.addEditor(Session.getEffectiveUser());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}To protect only a range, use sheet.getRange('A1:F20').protect() instead of sheet.protect(). Protection does not hide data. A viewer can still read protected cells, and an editor may be able to copy visible data.
> callout note > Test scripts on a copy first. Sharing changes affect real accounts immediately, and protection can prevent collaborators from making expected edits.
Common errors and safe practice
- Do not treat protection as a replacement for sharing. Sharing decides who can open the file; protection decides who can edit protected content.
- Do not store passwords or private tokens in the script.
- Check the active spreadsheet before running a function that changes permissions.
- Keep a record of the accounts and roles that the script grants.
- Use the narrowest general-access role. Prefer Viewer over Editor when recipients only need to read.
- Review the Share dialog after automation to confirm the result.
Exercise
Which object should a script use to give someone Commenter access, and which object should it use to protect a sheet?
Show answer
Use the Drive File object for Commenter access and the Sheet object for protection.