Javascript 否则如果在谷歌脚本中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36251908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:49:03  来源:igfitidea点击:

else if in google script

javascriptif-statementgoogle-sheets

提问by captCC

I'm trying to search a range of cells, I already have the formulas set up for filtering. I'm trying to determine three different values a cell could have and use setFormuladepending on which value the cell matches. Her's what I've come up with so far. It's working for the first two formulas but isn't setting "formula 3" if both cells contain data.

我正在尝试搜索一系列单元格,我已经设置了用于过滤的公式。我正在尝试setFormula根据单元格匹配的值来确定单元格可以具有和使用的三个不同值。到目前为止,她是我想出的。它适用于前两个公式,但如果两个单元格都包含数据,则不会设置“公式 3”。

function setFormulas(){
    var ss = SpreadsheetApp.getActive()          
    var sheet = SpreadsheetApp.getActiveSheet()
    var cell = ss.getActiveCell()
    var cell1 = ("C2");
    var formulaCell = ("A5");
    var cell2 = ("C3");
    var cell1isblank = SpreadsheetApp.getActiveSheet().getRange(cell1).isBlank()
    var cell2isblank = SpreadsheetApp.getActiveSheet().getRange(cell2).isBlank()


    if (cell1 == "0" ) {
        SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("formula1")
    }
    else if (cell2 == "0" ) {
        2SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("formula2")
    }

}

    else {
       SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("Formula3")
    }

回答by Ed Nelson

Fist, you have a bracket in the wrong place. Second, you need to do the true/false compare to the cellisblank variables. Also, there is a typo (2SpreadsheetApp). Try try this:

拳头,你在错误的地方有一个支架。其次,您需要对 cellisblank 变量进行真/假比较。此外,还有一个错字 (2SpreadsheetApp)。试试这个:

function setFormulas(){
   var ss = SpreadsheetApp.getActive()          
   var sheet = SpreadsheetApp.getActiveSheet()
   var cell = ss.getActiveCell()
   var cell1 = ("C2");
   var formulaCell = ("A5");
   var cell2 = ("C3");
   var cell1isblank = SpreadsheetApp.getActiveSheet().getRange(cell1).isBlank()
   var cell2isblank = SpreadsheetApp.getActiveSheet().getRange(cell2).isBlank()

    if (cell1isblank == false && cell2isblank == true) {
  SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("=formula1")
    }
    else if (cell2isblank == false && cell1isblank == true ) {
SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("=formula2")
    }
  //}
   else {
  SpreadsheetApp.getActiveSheet().getRange(formulaCell).setFormula("=Formula3")
  }
}