4

I need to create a document (something like a Google Sheets) where multiple users could insert their names, but within a limited time span. They could edit the document only from Monday (8pm) to Friday (2pm); so on the weekends it would be "locked" for editing. But somebody would have to unlock the document Monday morning and lock it again on Friday afternoon so users wouldn't have access to it or couldn't change the data on it while it's locked for the weekend.

My colleague who created this document in a Google Sheets is now solving this by "ticking" away the users on Fridays and "ticking" them in on Mondays again. But it takes a lot of time this way.

Is there a way to make this function of "locking" and "unlocking" the document automatic?

1
  • 1
    You can use a script to add and remove them. Oct 11, 2014 at 18:37

1 Answer 1

2

With the following script you can restrict access (view only) to your Google Spreadsheet.

Code

function setUsers() {  
  var date = new Date(), day = date.getDay(), hour = date.getHours();
  var monday = 1, friday = 5;
  if((day == monday && hour == 8) || (day == friday && hour == 14)) {
    var sProp = PropertiesService.getScriptProperties();
    if(!sProp.getProperty('day')) { 
      sProp.deleteAllProperties();
      sProp.setProperty('day', day);
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      switch(day) {
        case monday:
          ss.addEditors(ss.getViewers());
          break;
        case friday:
          var editors = ss.getEditors(); 
          for(var i = 0, iLen=editors.length; i < iLen; i++) {
            var user = editors[i];
            ss.removeEditor(user);
            ss.addViewer(user);
          }
          break;
      }      
    }    
  }
  return;
}

Trigger

enter image description here

Explained

The trigger is only fired when it's a Monday or a Friday and only on those specific times. After that it will check whether it hasn't been triggered once before, by looking up in the scripts memory. If nothing is found for that day, then delete all other entries and create a new one. This way, if the script is triggered again, it will find an entry and skip to return. The switch statement makes it possible to switch between the days and the different actions to make. On Mondays, the existing viewers need to be added as editors. This can be done via a batch operation (at once). On Fridays the editors need to be removed first in order to add them as viewers.

Notes

I've tested it against two of my own accounts and it worked. As far as I know, no messages are being send about the user being added as viewer or as an editor (which is the case when you do that manually). In the editor mode, the viewer mode is being enforced automatically. This isn't the case when the user is in the viewer mode. There the user needs to refresh the window (F5).

If you the set trigger on a 5 minutes interval, then in theory the time frame can be missed by 5 minutes. If you choose to pick the 1 minute interval, then this will be minimized to 1 minute.

Another option would be to create a trigger each and every time. This way the set time can be programmatically triggered (even thought the triggers are bound to be executed give or take 15 minutes, see reference2).

Add the code under Tools > Script editor and press the bug button to authenticate the script. Furthermore, create a trigger by selecting Resources > Current project's triggers.

Setback

There's one huge setback to this solution however. Everyone, added as an editor, will be able to see and change the code and there's no way to avoid that, see reference1.

Reference

  1. https://stackoverflow.com/q/16075446/1536038
  2. https://developers.google.com/apps-script/reference/script/clock-trigger-builder
1
  • 1
    There IS a way arround (1). Just make the script standalone, not part of the spreadsheet. Use openById from the script.
    – Zig Mandel
    Dec 24, 2014 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.