如何获取字符串并将其用作应用程序脚本中语法的一部分? [英] How to get a string and use it as part of the syntax in apps script?

查看:52
本文介绍了如何获取字符串并将其用作应用程序脚本中语法的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为公司中的所有用户建立一个Gmail数据库,我想通过一段代码来获取这些Gmail,并在其上应用if条件以查看它们是否匹配,但到目前为止我还没有成功.我不知道这是因为我检索到的字符串不能作为应用脚本语法读取,还是因为我做错了.这是我的代码:

I am trying to build a Gmail database for all users in our company, I want to get these Gmails through piece of code and apply an if condition on them to see if they match or not, but I am not successful so far. I don't know if it's because the string I retrieve can't be read as an apps script syntax or it's because I am doing it wrong. here is my code:

我将所有所需的Gmail放在这样的1个单元格(B3)中--->(用户=='abc1@gmail.com')||(用户=='abc2@gmail.com')

I put all needed Gmails in 1 cell (B3) like this ---> (user == 'abc1@gmail.com') || (user == 'abc2@gmail.com')

//my code
    var user = Session.getEffectiveUser();
    var mailDB         = SpreadsheetApp.openById('Sheet ID').getSheetByName('Sheet Name'); //mails database
    var cellcondition  = HeadOfficeMailDB.getRange("A3"); // cell with certain value 22
    var Gmails     = mailDB.getRange("B3").getValue(); //retrieve Gmails in the cell as one whole string
    if((Gmails) && (cellcondition == 22))
    {
      var newcell  = HeadOfficeMailDB.getRange("C3").setValue(4);
    }

我为数据库设置了2个不同的Gmail,但是当我使用数据库中未包含的第三个Gmail运行代码时,它仍然可以运行.似乎无法将字符串识别为语法,或者我做错了什么?

I set the Database with 2 different Gmails, but when I run the code with a third Gmail not included in Database, it runs anyway. it seems like it doesn't recognize the string as a syntax, or am I doing something wrong?

如果您有任何解决方案或建议或其他更好的想法来解决此类问题,请随时为我提供帮助.

kindly if you have any fix or recommendations or other better ideas to handle such issue please don't hesitate to provide me with your assistance immediately.

推荐答案

以下行

var cellcondition  = HeadOfficeMailDB.getRange("A3");

将Class Range对象分配给cellcondition.替换为

assigns a Class Range objec to cellcondition. Replace it by

var cellcondition  = HeadOfficeMailDB.getRange("A3").getValue();

A3 的值分配给 cellcondition .

评估表名!B3 ((user =='abc1@gmail.com')||(user =='abc2@ gmail.com'))),则可以使用 Set 对象.

To evaluate the value of Sheet Name!B3 ((user == 'abc1@gmail.com') || (user == 'abc2@gmail.com')) you could use eval() but doing this is an enormous security risk. It's better to store the email address as a list (separated by using a separator like a comma) then use String.prototype.split() and Array.prototype.index() or create a Set object.

相关

这篇关于如何获取字符串并将其用作应用程序脚本中语法的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆