'Google Spreedsheets Script That Allows Viewers to do Selective Actions

I have a Google Spreedsheets document with a very simple script that shows and hides columns if someone selects a specific value in a cell (sheet.showColumns/sheet.hideColumns).

I want the people who access this document to be able to do this command, but not to edit anything else. But if I turn them into only viewers the script does not work because showing and hiding columns is already an editing action.

Do you know how I can give a selective permission to people that are gonna access this, to be able to do this command but to not edit anything else.

Thanks



Solution 1:[1]

How about use protect class. Reference is here. 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 throws an exception upon removing the group.
    var me = Session.getEffectiveUser();
    protection.addEditor(me);
    protection.removeEditors(protection.getEditors());
    if (protection.canDomainEdit()) {
      protection.setDomainEdit(false);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 liquidkat