Google表格脚本修改-从第4行开始插入复选框



我使用下面的复选框脚本,这是由另一个stackoverflow用户提供的。

然而,我想脚本开始插入复选框和清除从第4行和向下,即不添加复选框在第1-3行或清除在第1-3行,如果在col5上1-3行的单元格被编辑。

有人可以修改代码来实现这一点。提前感谢。

function onEdit(e) {
const watchSheets = /^(Sheet1|Sheet2|Sheet3)$/i;
const watchColumn = 5;
const checkboxColumns = [1, 2, 4];
if (e.range.columnStart === watchColumn && e.source.getActiveSheet().getName().match(watchSheets)) {
checkboxColumns.forEach(checkboxColumn => {
const checkboxCell = e.source.getActiveSheet().getRange(e.range.rowStart, checkboxColumn);
if (e.value) {
checkboxCell.insertCheckboxes();
} else {
checkboxCell.clearDataValidations();
checkboxCell.clearContent();
}
});
}
}

工作代码为:

function onEdit(e) {
const watchSheets = /^(Sheet1|Sheet2|Sheet3)$/i;
const watchColumn = 5;
const checkboxColumns = [1, 2, 4];
if (e.range.columnStart === watchColumn && e.source.getActiveSheet().getName().match(watchSheets) && e.range.rowStart >= 4) {
checkboxColumns.forEach(checkboxColumn => {
const checkboxCell = e.source.getActiveSheet().getRange(e.range.rowStart, checkboxColumn);
if (e.value) {
checkboxCell.insertCheckboxes();
} else {
checkboxCell.clearDataValidations();
checkboxCell.clearContent();
}
});
}
}

在您的脚本中,如何修改以下内容?

:

if (e.value) {
checkboxCell.insertCheckboxes();

:

if (e.value && e.range.rowStart >= 4) {
checkboxCell.insertCheckboxes();
  • 在本次修改中,当编辑的行数超过第4行(包括第4行)时,运行checkboxCell.insertCheckboxes()的脚本。

最新更新