2017-08-08 33 views
1

在谷歌片,我試圖用一個腳本,使受保護的範圍。無法使用腳本的保護範圍編輯權限刪除有效用戶 - Google表格

我想腳本,使只有擁有者可以編輯受保護的範圍,並沒有其他人,但腳本有當它運行的是誰不是所有者用戶的工作。

當我登錄的電子表格的所有者,並且我從運行腳本編輯器的代碼,它工作正常 - 它創建了一個保護的範圍G1:G10只有擁有者可以編輯。

然而,當我運行該腳本登錄的同時誰是不是所有者,受保護範圍的權限允許的用戶和編輯的範圍主人能力的用戶。我所知道的this pagethis page,對谷歌的開發者文檔,但我看不到任何東西,會幫我。

這裏是我的代碼:

function setProtections() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var owner = ss.getOwner(); 
    var effectiveUser = Session.getEffectiveUser(); 
    var range = ss.getRange('G1:G10'); 
    var protection = range.protect() 
    var editors = protection.getEditors(); 
    protection.removeEditors(editors); 
    protection.addEditor(owner); 
} 

誰能幫助?

回答

0

removeEditors()不允許當前用戶被移除:

刪除用戶從編輯器實現保護片或範圍的列表中的給定的數組。請注意,如果任何用戶是具有編輯權限的Google羣組的成員,或者該域中的所有用戶都擁有編輯權限,那麼這些用戶仍然可以編輯保護區域。 電子表格的所有者和當前用戶都不能被刪除。

但是,您可以使用由業主將運行setProtections()方法創建觸發器的用戶創建了一個Installable Trigger [所有者] - 即使它被另一個用戶觸發。

實施例:Installable Trigger onEdit()

function installedOnEditTrigger (e) { 
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; 

    //Checks if the sheet edit happened in the 7th column "G" 
    if (e.range.getColumn() == 7) { 
    var range = ss.getRange('G1:G10'); 
    var protection = range.protect() 
    var editors = protection.getEditors(); 
    //Removes all editors - owners can not be removed 
    protection.removeEditors(editors); 
    } 
} 
相關問題