簡體   English   中英

Google App 腳本從保護中刪除編輯器

[英]Google App script Remove Editors From Protection

在 Google 表格中,我嘗試使用腳本從除工作表所有者之外的所有保護中刪除編輯器,為此我使用以下代碼,但在運行代碼后,整個保護被刪除,但是代替刪除我想要的保護從保護中刪除除工作表所有者之外的所有用戶。 此外,當代碼運行時,一次只刪除一個保護,我想將它應用於所有保護。

function remove(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE)[0];
if (protection && protection.canEdit()) {
protection.remove();
}}

上述任何幫助將不勝感激。

為了使代碼只允許電子表格所有者能夠編輯受保護的范圍,您應該進行的更改已包含在 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();
      }
    });
}

資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM