google-sheetstimerangelockingcell

Script to lock a different tab sheet everyday


So I made this spreadsheet with multiple sheets for every day of the month (1-31), there's a staff member that before closing the clinic (it's a medical clinic), every day, she types in what we made (debit, credit, cash, expenses). She only have access to the same certain ranges of every sheet; I locked every sheet with an exception to these ranges (the ones she can edit).

The thing is, when she comes back the next day of work and login in the spreadsheet she can still edit the previous days; What I have to do is to every night take out the cells exceptions for that day sheet, so she can't edit previous days after it has been passed.

Is there a script where I can set for everyday to automatically take out these ranges exceptions for certain sheets? Example: 01/01/24 20:00h lock sheet 1 with no range exceptions. 01/02/24 20:00h lock sheet 2 with no range exceptions. 01/03/24 20:00h lock sheet 2 with no range exceptions. and so on until the 31.

The ranges I have unlocked (the ones the staff member can edit) from every sheets are: (G5)/(G20:G23)/(A9:G17)/(G27)/(A47:G49)/(A33:G41)

The name of the sheets I would like the script to lock are: "1" all the way to "31"

Thanks a lot for any help.


Solution

  • Thanks to @Tedinoz I have found the answer for this.

    I edited his function to completely unprotect the current day sheet then protect again and also hide it. Works like a charm.

    All you got to do is create an installable time-driven trigger for the function.

        function bloquearDiario(e) {
    
      // Logger.log(JSON.stringify(e)) // DEBUG
      // list of trading day sheets
      var sheetNames = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
    
      var ss = SpreadsheetApp.getActiveSpreadsheet()
    
      // get the Event Object for the day of the month
      var dayOfMonth = e['day-of-month']
      // Logger.log("DEBUG: The day of the month: "+dayOfMonth)
    
      // find the index matching the day of the month
      var dayMatch = sheetNames.indexOf(dayOfMonth) // zero-based
      // Logger.log("DEBUG: dayMatch = "+dayMatch)
      // get the actual SheetNumber
      var sheetName = sheetNames[dayMatch] 
      // Logger.log("DEBUG: Trading Day sheet number = "+sheetName)
    
      // get the sheet and activate (make it the Activesheet)
      var sheet = ss.getSheetByName(sheetName);
      sheet.activate();
    
     // Remove sheet protection from the active sheet, if the user has permission to edit it.
     var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
      if (protection && protection.canEdit()) {
      protection.remove();
      }
    
      // Protect the active sheet, then remove all other users from the list of editors.
      var protection = sheet.protect();
    
      // Hide sheet.
      var sheet = SpreadsheetApp.getActiveSheet().hideSheet();
    
      // 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);
    }
      
      
      }
    

    Thanks for the help @Tedinoz