我正在使用以下腳本來保護 Google 表格(腳本在打開表格時運行)。該保護將應用于除作業表所有者之外的所有用戶,即只有所有者才能訪問受保護的范圍。我想更改保護標準,即基于電子郵件的所有者和特定用戶可以訪問受保護的范圍。
function installedOnOpen(e) {
const sheetNames = ["Sheet1"]; // Please set the sheet names you want to protect.
const sheets = e.source.getSheets().filter(s =>
sheetNames.includes(s.getSheetName()));
if (sheets.length == 0) return;
sheets.forEach(s => {
const p = s.getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (p.length > 0) {
p.forEach(pp => pp.remove());
}
const lastRow = s.getLastRow();
if (lastRow != 0) {
const newProtect = s.getRange(1, 1, lastRow, s.getMaxColumns()).protect();
newProtect.removeEditors(newProtect.getEditors());
if (newProtect.canDomainEdit()) newProtect.setDomainEdit(false);
}
});
}
上述任何幫助將不勝感激。
uj5u.com熱心網友回復:
我相信你的目標如下。
- 你想保護細胞。在這種情況下,您希望按所有者和其他特定用戶編輯單元格。
在這種情況下,如何進行以下修改?
修改后的腳本:
function installedOnOpen(e) {
const editors = ["###"]; // Added: Please set the email addresses you want to give the permission as the editor.
const sheetNames = ["Sheet1"]; // Please set the sheet names you want to protect.
const sheets = e.source.getSheets().filter(s => sheetNames.includes(s.getSheetName()));
if (sheets.length == 0) return;
sheets.forEach(s => {
const p = s.getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (p.length > 0) {
p.forEach(pp => pp.remove());
}
const lastRow = s.getLastRow();
if (lastRow != 0) {
const newProtect = s.getRange(1, 1, lastRow, s.getMaxColumns()).protect();
newProtect.removeEditors(newProtect.getEditors());
newProtect.addEditors(editors); // Added
if (newProtect.canDomainEdit()) newProtect.setDomainEdit(false);
}
});
}
- 在此修改中,假設該函式
installedOnOpen由可安裝的 OnEdit 觸發器運行。請注意這一點。
參考:
- 添加編輯器(電子郵件地址)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/494459.html
標籤:谷歌应用脚本
上一篇:在所選單元格之前添加行
下一篇:保持電子表格同步
