How to add editors to the list of protected sheet

954 views
Skip to first unread message

loke meng ong

unread,
Apr 5, 2022, 12:17:35 PM4/5/22
to Google Apps Script Community
Hi , there. Can anyone help me. How do I add list of emails who can edit a google sheet. I can only add myself but not others. The script I'm using is below. Kindly help. Cheers. Ong

function protectRange() {
// Protect the active sheet except B2:C5, then remove all other users from the list of editors.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect().setDescription('Protected sheet');
protection.setUnprotectedRanges([sheet.getRange('1:1'),sheet.getRange('A:A')]);

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

}

Clark Lind

unread,
Apr 9, 2022, 12:52:13 PM4/9/22
to Google Apps Script Community
I believe that after you remove everyone, you will need to add the new editors back in. Something like this:

  var me = Session.getEffectiveUser();
  protection.addEditor(me);
  protection.removeEditors(protection.getEditors());
  if (protection.canDomainEdit()) {
    protection.setDomainEdit(false);
  }
  editors.forEach( (email) => {
      protection.addEditor(email)
      })

OngLM

unread,
Apr 14, 2022, 9:17:57 AM4/14/22
to google-apps-sc...@googlegroups.com
Thank you so much for your advice

Sent from my iPhone

On 10 Apr 2022, at 12:52 am, Clark Lind <cwl...@gmail.com> wrote:

I believe that after you remove everyone, you will need to add the new editors back in. Something like this:
--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/WC4jiw75J3o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/17af84b7-c7bb-437f-90ee-9d1ef2b2ba3dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages