google-apps-script - 谷歌表格不尊重我的脚本中的 IF 条件 - 为什么?
问题描述
我试图锁定 col“A”中的所有单元格,而不是包含低于 3 的数字,但无论条件如何,它都会锁定它经过的每个单元格。也许我在某个地方放错了支架?还是写这个更简单的方法?这是我的第一次尝试。我习惯在 excel 中使用 VBA,但这要困难得多。
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Rezervace');
function lockRanges() {
//First cell to lock
var row = 1;
var col = 1;
// Get last row with data in sheet
var lastRow = sheet.getLastRow();
for (var i = row; i < lastrow; i++) {
//Lock current cell
if(sheet.getRange(row,col).getValue() < 3)
{
lockRange(row, col);
row = row + 1;
};
};
}
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);
}
}
解决方案
修改点:
- 在您的循环中,is
row
时加 1 。在这种情况下,增长为,并且单元格每个 都被锁定。我认为你的问题可能是由于这个。sheet.getRange(row, col).getValue() < 3
true
row
1, 2, 3,,,
row
- 在
getValue
循环中使用时,处理成本会很高。- 在此修改中,首先,从列“A”中检索值,并使用检索到的值保护单元格。
当这些点反映到你的脚本中时,它变成如下。
修改后的脚本:
function lockRanges() {
var me = Session.getEffectiveUser();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Rezervace');
var values = sheet.getRange("A1:A" + sheet.getLastRow()).getValues();
values.forEach(([a], i) => {
if (a < 3) {
var protection = sheet.getRange("A" + (i + 1)).protect().setDescription('Protected, row ' + (i + 1));
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
});
}
参考:
编辑:
关于您在评论中的以下新问题,
不知道那张纸出了什么问题,但是当我制作一张新纸时,它的工作原理:)非常感谢!有没有办法检查单元格是否已经被锁定,所以它不会连续锁定单元格 20 次?我的猜测是用 <3 条件检查它,但我对代码的理解还不是很好 ''' values.forEach(([a], i) => { if (a < 3) && (sheet.getRange( "A" + (i + 1)).getDescription()='受保护,行' + (i + 1)) '''
在这种情况下,我认为需要检查“A”列的行是否受到保护。为此,我将上面的脚本更新如下。
修改后的脚本:
function lockRanges() {
var me = Session.getEffectiveUser();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Rezervace');
// Retrieve the protected rows of the column "A".
var protectedRows = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE).reduce((ar, p) => {
var range = p.getRange();
if (range.getColumn() == 1) ar.push(range.getRow());
return ar;
}, []);
// Protect the rows.
var values = sheet.getRange("A1:A" + sheet.getLastRow()).getValues();
values.forEach(([a], i) => {
if (a < 3 && !protectedRows.includes((i + 1))) {
var protection = sheet.getRange("A" + (i + 1)).protect().setDescription('Protected, row ' + (i + 1));
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
});
}
- 运行此脚本时,首先检索列“A”的受保护行号。并且,使用检索到的行号,“A”列的行受到保护。这样,已经被保护的相同行不受保护。
推荐阅读
- arrays - 从json响应访问对象值反应本机
- java - 使用 for 循环嵌套下拉菜单会导致过时的元素引用错误
- c# - 如何在 C# 中执行通用 SQL 命令?
- rest - EnvelopeDefinition:EventNotification API 类型的基本身份验证
- ios - 在 XCODE 中使用 Distr 证书组装后,Iphone 无法安装我的应用程序
- java - 使用 Kotlin 属性访问语法设置 Java 字段是否安全
- arrays - 根据多个数组条件计算表中的行数
- javascript - 从ajax分配javascript类中的字段
- python - Mnist 与 Tensorflow Premade Estimator,评估时输入尺寸不匹配
- tensorflow - Keras layer.weights 和 layer.get_weights() 给出不同的值