【问题标题】:Unable to protect a range based on day of week using google scripts无法使用谷歌脚本保护基于星期几的范围
【发布时间】:2020-03-03 22:32:25
【问题描述】:

在谷歌表格中,我正在尝试编写一个脚本来锁定一个基于星期几的范围,持续 12 小时,然后解锁。 这个想法是,用户将无法在它所代表的那一天编辑该列,但允许选择少数管理员。

我遇到的问题是 range.protect 似乎不被视为一个函数,我的编辑没有获得许可,只有我这样做。 我试图通过谷歌https://developers.google.com/apps-script/reference/spreadsheet/protection来遵循这个模板 收效甚微。

  var meeditor = "me@gmail.com"
  var myeditors = ["editor@gmail.com", "editor@gmail.com", "editor@gmail.com" "editor@gmail.com"]
  var sheet = SpreadsheetApp.getActive().getSheetByName("sign ups");
  var d = new Date();
  var n = d.getDay();
  if (n == 1)  {  //monday
    var range = ['C5:C20'];
    var rangesToUnProtect = ['B5:B20'];
    }
  if (n == 2) { //tuesday
    var range = ['D5:D20'];
    var rangesToUnProtect = ['C5:C20'];
  }
  if (n == 3) {  //wednesday
    var range = ['E5:E20'];
    var rangesToUnProtect = ['D5:D20'];
  }
  if (n == 4) {  //thursday
    var range = ['F5:F20'];
    var rangesToUnProtect = ['E5:E20'];
  }
  if (n == 5) {  //friday
    var range = ['G5:G20'];
    var rangesToUnProtect = ['F5:F20'];
  }
  if (n == 6) {   //saturday
    var range = ['H5:H20'];
    var rangesToUnProtect = ['G5:G20'];
  }
  if (n == 0) {  //sunday
    var range = ['B5:B20'];
    var rangesToUnProtect = ['H5:H20'];
  }
  var protection = range.protect().setDescription('no more signing up');

  protection.addEditor(meeditor) //keep myself editor
  protection.addEditors(myeditors)

  protection.removeEditors(protection.getEditors());  /remove permissions from all others
  if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
    for (var i = 0; i < rangesToUnProtect.length; i++) {  \\remove the protection the next day
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }

}
}
}

我在这方面比较陌生,我确定我没有以最佳方式做到这一点。我该如何正确定义?这让人想起了这个错误https://issuetracker.google.com/issues/148894990,但它应该被修复。

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    您收到以下错误的原因是因为您没有将范围声明为范围,而是声明为字符串,并且 protect 方法是特定于Range Class 对象或Sheet Class 对象,不适用于string 对象。

    TypeError: range.protect 不是函数

    为了获得范围,您应该从这里更改所有var range 变量:

    var range = ['RANGE1'];
    var rangesToUnProtect = ['RANGE2'];
    

    到这里:

    var range = sheet.getRange("RANGE1");
    var rangesToUnProtect = sheet.getRange("RANGE2");
    

    参考

    【讨论】:

      猜你喜欢
      • 2022-12-03
      • 2017-07-24
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多