Google脚本在特定日期(一年中的某天)运行

时间:2019-10-16 19:20:38

标签: google-apps-script google-sheets

我需要有关脚本的帮助,之前在此平台上使用该脚本已经获得了帮助。 这是基于一天和不同范围的保护脚本。

目前:每月(1-32天) 是否可以设置一年中的某一天(1-365)?在我的脚本中它将更好地工作。

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");
  }
}

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

获取年度日期

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

Utilities.formatDate()

Simple Date Format

所以在您的代码中:

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");
  }
}