Google apps script 无法使用google脚本保护基于星期几的范围
在GoogleSheets中,我试图制作一个脚本,在解锁时根据一周中的某一天锁定一个范围12小时。 这样做的目的是,用户将无法在该列所代表的日期编辑该列,但允许选择少数管理员 我遇到的问题是这个范围。保护似乎不被视为一个功能,我的编辑器也没有得到许可,只有我得到了。 我试图通过谷歌来遵循这个模板 几乎没有成功Google apps script 无法使用google脚本保护基于星期几的范围,google-apps-script,google-sheets,Google Apps Script,Google Sheets,在GoogleSheets中,我试图制作一个脚本,在解锁时根据一周中的某一天锁定一个范围12小时。 这样做的目的是,用户将无法在该列所代表的日期编辑该列,但允许选择少数管理员 我遇到的问题是这个范围。保护似乎不被视为一个功能,我的编辑器也没有得到许可,只有我得到了。 我试图通过谷歌来遵循这个模板 几乎没有成功 var meeditor = "me@gmail.com" var myeditors = ["editor@gmail.com", "editor@gmail.com", "ed
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();
}
}
}
}
var meeditor=”me@gmail.com"
var myeditors=[”editor@gmail.com", "editor@gmail.com", "editor@gmail.com" "editor@gmail.com"]
var sheet=SpreadsheetApp.getActive().getSheetByName(“注册”);
var d=新日期();
var n=d.getDay();
如果(n==1){//星期一
var范围=['C5:C20'];
var rangesToUnProtect=['B5:B20'];
}
如果(n==2){//星期二
变量范围=['D5:D20'];
var rangesToUnProtect=['C5:C20'];
}
如果(n==3){//
var范围=['E5:E20'];
var rangesToUnProtect=['D5:D20'];
}
如果(n==4){//星期四
变量范围=['F5:F20'];
var rangesToUnProtect=['E5:E20'];
}
如果(n==5){//friday
风险值范围=['G5:G20'];
var rangesToUnProtect=['F5:F20'];
}
如果(n==6){//星期六
变量范围=['H5:H20'];
var rangesToUnProtect=['G5:G20'];
}
如果(n==0){//sunday
var范围=['B5:B20'];
var rangesToUnProtect=['H5:H20'];
}
var protection=range.protect().setDescription('不再注册');
protection.addEditor(meeditor)//保持编辑状态
保护。加法器(myeditor)
protection.removeditors(protection.getEditors())/删除所有其他用户的权限
if(protection.canDomainEdit()){
protection.setDomainEdit(false);
对于(var i=0;i
在这方面我是个新手,我肯定我做的不是最好的。我如何正确地定义它?这让人想起了这个bug,但它应该被修复。出现以下错误的原因是,您没有将范围声明为范围,而是声明为字符串,
保护
方法是特定于范围类
对象或工作表类
对象的方法,不适用于字符串
对象
TypeError:range.protect不是一个函数
为了获得一个范围,您应该从以下内容更改所有var范围
变量:
var range = ['RANGE1'];
var rangesToUnProtect = ['RANGE2'];
为此:
var range = sheet.getRange("RANGE1");
var rangesToUnProtect = sheet.getRange("RANGE2");
参考
- )