user1455837
user1455837

Reputation: 1

How to lock a sheet for current user with Google script

I'am trying to lock a sheet via Google script. I want to lock this script to all user. I'am using what I found in documentation: https://developers.google.com/apps-script/reference/spreadsheet/protection

// Protect range A1:B10, then remove all other users from the list of editors.
 var ss = SpreadsheetApp.getActive();
 var range = ss.getRange('A1:B10');
 var protection = range.protect().setDescription('Sample protected range');

 // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
 // permission comes from a group, the script will throw an exception upon removing the group.
 var me = Session.getEffectiveUser();
 protection.addEditor(me);
 protection.removeEditors(protection.getEditors());
 if (protection.canDomainEdit()) {
   protection.setDomainEdit(false);
 }

The problem is that the script that is supposed to lock the sheet is ran by a user that should see the sheet locked.

My question is: how can I lock a sheet for the current user using Google script?

Upvotes: 0

Views: 1497

Answers (2)

Richard
Richard

Reputation: 11

You can't. See https://developers.google.com/apps-script/reference/spreadsheet/protection#removeeditorsemailaddresses

Key sentence: "Neither the owner of the spreadsheet nor the current user can be removed."

Wish we could.

Upvotes: 1

Pushkar
Pushkar

Reputation: 11

Do not think you can do that.. I tried it but did not work. In my opinion, access is given by a user, hence you cannot restrict the user initiating/running the script in itself. It's a little tricky

Upvotes: 1

Related Questions