Run google sheets incl. script algorithm, if user is not an editor

30 views
Skip to first unread message

Robert Voigt

unread,
Aug 13, 2019, 4:30:38 PM8/13/19
to Google Apps Script Community

Dear Community,

I'm new to this kind of programming and was formerly only slightly working with excel. Therefore this is a very basic question.
I created a google sheet file wich is doing simple calculation with scripteditor algorithm. Calculation is activated by input the user gives into a pop-up window (activated by button click).

The script is actually working fine. But I have the following problem:
I'm the editor and the google sheets file is also write protected (means no cell can be edited by others). But I need others to use this sheet in that way that they click the button, put a number in the pop-up and the script does the calculation.
As a normal user (no editor, no writing rights due to sheet protection) it is just possible to click the button, the pop-up window comes and a number can be written in. But then the script is not processing, instead a failure message comes up which says that user has no editor rights.
I was trying to work around that protection, with putting specific mail adresses as editor as soon as script is starting and erasing in the end again. But it does not work, because the script does never start if you are not an editor from beginning.

Is there a possibility to use such script calculation for a protected sheet, even if user is not an editor from the start? 
Such functionality is in some cases very helpfull and is often used in excel documents.

Thank you very much in advance. Your helpfull advice will be very much appreciated.
with best regards,
Robert

------------------------------------------------------------------
As information: script looks as following.
------------------------------------------------------------------

function test() { 
    
  var ui = SpreadsheetApp.getUi();
  
  var antwort = ui.prompt('put in a number',ui.ButtonSet.OK_CANCEL);

 // Regain sheet protection for the active sheet.
      var sheet = SpreadsheetApp.getActiveSheet();
      var protection = sheet.protect().setDescription('Tabellenblatt geschützt');
      protection.removeEditors(protection.getEditors());
      protection.addEditors(["me@mail","example1@mail","example2@mail"]);
  
// 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.getActiveUser().getEmail();  
//    protection.removeEditors(protection.getEditors());
//      protection.addEditor([me]);
//      if (protection.canDomainEdit()) {
//      protection.setDomainEdit(false);
//      } 
 
// Remove sheet protection from the active sheet, if the user has permission to edit it.
      var sheet = SpreadsheetApp.getActiveSheet();
      var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
      if (protection && protection.canEdit()) {
      protection.remove();
      }

// Process the user's response.
  {
  ...
  ...
  }
  
// Regain sheet protection for the active sheet.
      var sheet = SpreadsheetApp.getActiveSheet();
      var protection = sheet.protect().setDescription('table protected');
      protection.removeEditors(protection.getEditors());
      protection.addEditors(["me@mail","example1@mail","example2@mail"]);
  

Eric Koleda

unread,
Aug 14, 2019, 2:30:27 PM8/14/19
to Google Apps Script Community
In short, no, that isn't possible. All edits made by those scripts count as coming from the user, and the user doesn't have permission. An alternative is develop a web app in Apps Script, which runs as you (the developer). Then users would be run the script and make edits, since it would all happen under your identity.

- Eric

Robert Voigt

unread,
Aug 16, 2019, 5:06:17 AM8/16/19
to Google Apps Script Community
Dear Eric,

thank you very much for your response on short notice. I was already expecting that it is not working.
Now I "simple" have to create a web app to get the functionality I need. 

Final question: Do you have advice / link which is helpful basic training / tutorial how to create such web app?

Thank you again& best regards
Robert
Reply all
Reply to author
Forward
0 new messages