我試圖在單元格中第一次輸入后鎖定 Google 表格中的單元格并使用以下代碼并獲得正確的結果。但是,在此代碼中,第一次輸入后單元格被鎖定,但所有者和編輯者可以對鎖定的單元格進行進一步編輯,但是,我想為輸入資料的人鎖定單元格,即只有所有者才能進行編輯鎖定后到牢房enter code here。
function onEdit(e){
let protection = e.range.protect();
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
上述任何幫助將不勝感激。以下是作業表的鏈接:
https://docs.google.com/spreadsheets/d/14qFZOKYUiQL4gKuLfdHPQwOgQ5LoY5P7KZyHN8ev-qY/edit?usp=sharing
uj5u.com熱心網友回復:
如果您的腳本是由 OnEdit 的簡單觸發器運行的,我認為您的問題的原因可能是由于這個。在這種情況下,請使用可安裝的 OnEdit 觸發器。
當您安裝可安裝的 OnEdit 觸發器時,請重命名函式名稱。因為當函式被安裝為可安裝觸發器時,當一個單元格被編輯時,異步程序會運行onEdit2 次。onEdit請注意這一點。
那么,下面的修改呢?
修改后的腳本:
function installedOnEdit(e) {
let protection = e.range.protect();
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
function installTrigger() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
ScriptApp.newTrigger("installedOnEdit").forSpreadsheet(ss).onEdit().create();
}
- 在這個腳本中,當
installTrigger運行時,可安裝的 OnEdit 觸發器被安裝到函式installedOnEdit中。這樣,當不是電子表格所有者的用戶編輯單元格時,該單元格就會受到保護。
參考:
- 可安裝觸發器
添加:
關于您的以下新問題,
我也想為編輯器鎖定作業表,即只有所有者才能編輯作業表。
在這種情況下,請按如下方式修改上述腳本。
修改后的腳本:
function installedOnEdit(e) {
let protection = e.range.getSheet().protect();
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
- 順便說一句,在這個腳本中,它假設您是電子表格的所有者,并且您安裝了 OnEdit 觸發器。因此,如果您想使用 安裝 OnEdit 觸發器
installTrigger,請由所有者運行腳本。請注意這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/486507.html
