2016-01-20 40 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,但更多。 –