Eka*_*Eno 1 google-sheets google-apps-script
我想以某些单元格每天在特定时间自动锁定的方式对我的Google表格单元格进行编码.我应该能够编辑它,但不能编辑我的合作者.
我怎么把它关掉?
在样本表(此处链接)中,您将看到某些日期(15/7/2015-17/7/2015).我想对它进行编码,以便每天(例如A1:B6)每天锁定,例如晚上10点.
Pun*_*ern 11
由于@NightShadeQueen建议使用时间驱动的触发器和类范围的protect()方法的组合.
如果您的工作表看起来不像样本表,则必须修改代码.特别是lockRanges()-function,它定义了应该保护哪些范围.目前,它从单元格开始,A1一次向下逐行7行,直到达到表格的末尾.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1'); //INSERT SHEET NAME HERE
function lockRanges() {
//First cell to lock
var row = 1;
var col = 1;
// Get last row with data in sheet
var lastRow = sheet.getLastRow();
//Loop until last row in sheet is passed
while(row <= lastRow){
//Lock current cell
lockRange(row, col);
// Go to row 7 steps down from current (the next date)
row = row + 7;
}
}
function lockRange(row, col){
var range = sheet.getRange(row, col);
// Create protection object. Set description, anything you like.
var protection = range.protect().setDescription('Protected, row ' + row);
// 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);
}
}
Run Code Online (Sandbox Code Playgroud)
Run=lockRangesEvents= Time-driven,Day timer,10pm to 11pm(或者只要你想)注意:时间可能略有随机化(了解更多)
就是这样,现在范围将在您选择时受到保护.与手动保护的范围一样,它们显示在" 数据">"受保护的工作表和范围"中.
有什么不清楚的?问一下!