我想对我的 Google 表格进行编码以检查(“扫描”)活动表格以查找“今天”的日期 - 然后,根据日期,保护该行中的所有列(女巫中的行找到今天的日期) ,这样只有所有者才能更改它(保护其他人)。
在论坛的帮助下,我找到了一些“解决方案”,但无法使其发挥作用。我认为 for 循环有问题(只是预测)。
脚本应该检查今天的日期,读取有多少列是活动的,并锁定今天+所有以前日期的行。我仍然缺少锁定所有具有所有权的行的保护,当然可以工作:)
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('MY SHEET');
function lockRanges() {
var dateRange = ss.getRange(2, 1, ss.getLastRow()-2, 1);
var curDate = Utilities.formatDate(new Date(), "GMT+1", "dd/M/YYYY");
var val = dateRange.getDisplayValues();
var row;
var col;
var end = ss.getLastRow();
//Find last active column
for (var y = 0; y <= ss.getLastColumn(); y++) {
col = y + 1;
}
//Loop through all rows
for(var x = 0; x <= end; x++){
if(val[x][0]> curDate){
row = x;
break;
}
}
var protection = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE)
if(protection.length > 0){
var range = ss.getRange(2, 1, row, col);
protection[0].setRange(range);
}
else{
ss.getRange(2, 1, row, col).protect();
}
}
此脚本包含在Apps Script to Build a Data Protection and only allow Spreadsheet Owner to edit
中提出的建议OP脚本评论
var dateRange = ss.getRange(2, 1, ss.getLastRow()-2, 1);
ss.getLastRow()-1
.Logger.log("The date range is "+dateRange.getA1Notation())
以显示定义的范围 .
var val = dateRange.getDisplayValues();
var curDate = Utilities.formatDate(new Date(), "GMT+1", "dd/M/YYYY").toString()
//Find last active column
- 不需要循环,只需使用ss.getLastColumn()
for (var y = 0; y <= ss.getLastColumn(); y++) {
getLastColumn()
返回索引号,但“y”初始化为零。所以语句运行的次数是one more than the number of columns
.for (var y = 0; y < ss.getLastColumn(); y++) {
//遍历所有行:
for(var x = 0; x <= end; x++){
end = ss.getLastRow();
. val.length
:数据数组中的元素数。
“脚本应该检查今天的日期”但是
val[x][0]> curDate
测试大于今天的日期。val[x][0] === curDate
是正确的条件。
// 获取行:
row = x;
row = x+2
保护
这种保护完全是错误的。参考文档:Class Protection和Apps Script to Build a Data Protection and only allow Spreadsheet Owner to edit
function blockRanges() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('MY SHEET');
var lr = ss.getLastRow()
var lc = ss.getLastColumn()
// assume row 1 = header
var dateRange = ss.getRange(2, 1, lr-1, 1);
var val = dateRange.getDisplayValues()
// Logger.log("DEBUG: date range = "+dateRange.getA1Notation())
// get todays Date
var curDate = Utilities.formatDate(new Date(), "GMT+1", "dd/M/YYYY").toString()
// Logger.log("curDate: "+curDate)
//Loop through date rows looking for a match
for (var x = 0; x < val.length; x++){
if(val[x][0] === curDate){
// get the row;
// add one for the header and one for zero-based loop
row = x+2;
// set protection
var protectionRange = ss.getRange(row,1,1,lc)
// Logger.log("DEBUG: the protection range = "+protectionRange.getA1Notation())
var protection = protectionRange.protect().setDescription(curDate);
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
// Set the spreadsheet owner as the only editor
var editors = protection.getEditors();
for (var i = 0; i < editors.length; i++) {
protection.removeEditor(editors[i]);
}
protection.addEditor("[email protected]");
}
}
}