设置范围保护每周基于时间的触发器

时间:2020-06-08 17:52:44

标签: javascript datetime google-apps-script google-sheets triggers

我正在尝试编写一个脚本,以每周保护不同范围的工作表。我已经弄清楚了每周的锁定并触发它。但是,每个星期都需要一个新范围。如果第一周是;

var range = sheet.getRange(4, 5, 5, 6);

第二周需要;

var range = sheet.getRange(10, 5, 5, 6);

,依此类推。因此,只有我的“行”输入将需要根据激活触发器的星期进行更改。

当前完整脚本如下:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Multiple'); //INSERT SHEET NAME HERE


function lockRange(row, col){
  var range = sheet.getRange(10, 5, 5, 6);

  // Create protection object. Set description, anything you like.
  var protection = range.protect().setDescription('Date1');

 // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
 // permission comes from a group, the script will throw an exception upon removing the group.
 var me = Session.getEffectiveUser();
 protection.addEditor(me);
 protection.removeEditors(protection.getEditors());
 if (protection.canDomainEdit()) {
   protection.setDomainEdit(false);
 }
}

1 个答案:

答案 0 :(得分:0)

答案:

您可以使用PropertiesService在脚本的属性中存储下一周。

代码修改:

function lockRange(row, col){
  var week = PropertiesService.getScriptProperties().getProperty("week");

  if (!week) {
    week = PropertiesService.getScriptProperties().setProperty("week", 4)
  }
  else {
    week = PropertiesService.getScriptProperties().setProperty("week", parseInt(week) + 6)
  }  

  week = PropertiesService.getScriptProperties().getProperty("week");

  var range = sheet.getRange(parseInt(week), 5, 5, 6);

  // continue your code...
}

function clearProperties() {
  // run this function ONCE to clear any properties that are being stored in
  // the script from testing/previous runs.
  PropertiesService.getScriptProperties().deleteAllProperties();
}

我希望这对您有帮助!

参考文献: