I have this code to protect rows of my sheet based on a tickmark in Col21.
But the code takes 1 to 3 seconds to execute, so when I check multiple boxes in succession, it skips executing some rows protect script.
How can I make the OnEdit work so that I can protect multiple in quick succession?
function installedOnEdit(e) {
const sheetName = "請求管理"; // Please set sheet name.
const checkbox = 21; // In this sample, the checkboxes are put in the column "B".
const range = e.range;
const sheet = range.getSheet();
if (sheet.getSheetName() != sheetName || range.columnStart != checkbox)
return;
const p = sheet
.getProtections(SpreadsheetApp.ProtectionType.RANGE)
.find((r) => {
const temp = r.getRange();
return (
temp.getRow() == range.rowStart &&
temp.getColumn() == range.columnStart &&
temp.getNumColumns() == 21
);
});
if (!p && range.isChecked()) {
const p = range.offset(0, 0, 1, 21).protect().setWarningOnly(true).setDescription(rownum);
} else if (p && !range.isChecked()) {
p.remove();
}
}
New contributor
Skanda Narayanan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4