2016-01-20 33 views
0

我在Google表格中有一個預訂程序,用戶可以選擇他們的名字(使用數據驗證來提供電子郵件列表),然後細胞被置於保護之下,沒有其他用戶可以更改預訂。谷歌腳本如果語句不檢查用戶與輸入的數據

發生的奇怪事情是,一個人可以輸入另一個人的電子郵件,然後該單元受到輸入的電子郵件而不是用戶的保護。用戶可以輸入非電子郵件字符串,並且不保護單元格。

想要的結果是,如果用戶的電子郵件和輸入的數據相同,請保護這些單元格,否則它可以自由編輯。

任何幫助,將不勝感激!

function onEdit(e){ 
    var CurrentUser = Session.getEffectiveUser().getEmail() 
    var range_DataEntered = SpreadsheetApp.getActiveRange(); 
    var DataEntered = range_DataEntered.getValue(); 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var SheetName = SpreadsheetApp.getActiveSheet(); 
    var Cell = ss.getActiveCell(); 

    if (CurrentUser = DataEntered) { 
     var protection = range_DataEntered.protect() 

     // Ensure the current user is an editor before removing others. Otherwise, if the user's edit 
     // permission comes from a group, the script will throw an exception upon removing the group. 

     protection.addEditor(CurrentUser); 
    if (protection.canDomainEdit()) { 
    protection.setDomainEdit(false); 
} 
    } 
    else { 
    //remove protection, set everyone to edit 
    range_DataEntered.protect().setDomainEdit(true); 
    } 
} 

回答

0

if (CurrentUser = DataEntered)必須 if(CurrentUser === DataEntered)

=將分配一個值不檢查等價。

+0

哇。非常感謝。我試過==,它沒有工作然後=但完全不知道===存在。有時間挖掘JavaScript,但更多。 –