2

I would like the sheet protected from 6pm - 10pm every day. I have the following script but it doesn't seem to protect the sheet at the designated time. I have this script with the Time-based trigger, day, 6-10pm. Any help fixing this script is appreciated.

function protectAll() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  for (var i = 0; i < sheets.length; i++) {
    sheets[i].protect().setDescription("Temporary protection");
  }
}

1 Answer 1

1

I believe, You're looking for something like time-driven trigger

So, there are at least two options for Your case:

  1. Manual setup, which does not seam very versatile, but You can setup a call of particular functions at hour length window every day - Edit > Current project's triggers > Create new trigger

  2. Programmatically, using something like following:

  function setup_triggers() {
    ScriptApp.newTrigger("protectAll")
          .timeBased()
          .atHour(18)
          .everyDays(1)
          .create();

    ScriptApp.newTrigger("unprotectAll")
          .timeBased()
          .atHour(22)
          .everyDays(1)
          .create();
  }

Beware that the trigger will be executed according to the timezone of the script (not sure what exactly does in mean in the context of Apps Script).

EDIT: right, apparently You could specify timezone as well. As far official example goes, it would be something like following:

ScriptApp.newTrigger("protectAll")
      .timeBased()
      .atHour(18)
      .everyDays(1)
      .inTimezone("America/Los_Angeles")
      .create();

Your Answer

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

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