在 Google 表格中,我試圖使用腳本從除作業表所有者之外的所有保護中洗掉編輯器,為此我使用下面的代碼,但在運行代碼后,整個保護被洗掉,但是代替洗掉我想要的保護從保護中洗掉除作業表所有者之外的所有用戶。此外,當代碼運行時,一次只洗掉一個保護,我想將它應用于所有保護。
function remove(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE)[0];
if (protection && protection.canEdit()) {
protection.remove();
}}
上述任何幫助將不勝感激。
uj5u.com熱心網友回復:
為了使代碼只允許電子表格所有者能夠編輯受保護的范圍,您應該進行的更改已包含在 Tainake 對您之前的問題Google Sheet Remove Editors After First Edit的回答中
從問題:
此外,當代碼運行時,一次只洗掉一個保護,我想將它應用于所有保護。
sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE)回傳一個物件陣列。要洗掉所有保護,您的代碼必須遍歷此陣列的所有元素。做到這一點的許多方法之一是使用Array.prototype.forEach,即
function remove() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE)
.forEach(protection => {
if (protection && protection.canEdit()) {
protection.remove();
}
});
}
資源
- https://developers.google.com/apps-script/guides/sheets
- https://developers.google.com/apps-script/reference/spreadsheet/protection
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/486504.html
