Dominique
Dominique

Reputation: 27

Google script run at a specific day (day of the year)

I need help with a script, I have already had help before on this platform with this script. It's a protection script based on a day and different range.

At the moment : it per month (1-32 day) Is it possible to set the day of the year (1-365) ? It will work better in my script.

Here is the original post

function AddProtectionToColumn() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var range = GetRange(ss);
      var protectSs = range.protect().setDescription('Protection automatique');
      var me = Session.getEffectiveUser();
      protectSs.addEditor(me);
      protectSs.removeEditors(protectSs.getEditors());
      if (protectSs.canDomainEdit()) {
        protectSs.setDomainEdit(false); 
      }
    }

    function GetRange(ss){

  var today = new Date().getDate(); 
  var protections = ss.getSheets()[0].getProtections(SpreadsheetApp.ProtectionType.RANGE);

  if (today == 289){ // day of the year
    return ss.getRange("J1:K4");
  }
  else if (today == 299){ 
    return ss.getRange("J5:K10");
  }
}

Thank you for your help!

Upvotes: 0

Views: 83

Answers (1)

Cooper
Cooper

Reputation: 64032

Getting the day of the year

parseInt(Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "D"));

Utilities.formatDate()

Simple Date Format

So in your code:

function GetRange(ss){
  var today = parseInt(Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "D"));
  var protections = ss.getSheets()[0].getProtections(SpreadsheetApp.ProtectionType.RANGE);
  if (today == 289){ // day of the year
    return ss.getRange("J1:K4");
  }
  else if (today == 299){ 
    return ss.getRange("J5:K10");
  }
}

Upvotes: 1

Related Questions