最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - else if in google script - Stack Overflow

programmeradmin3浏览0评论

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 setFormula depending 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.

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")
    }

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 setFormula depending 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.

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")
    }
Share Improve this question edited Mar 27, 2016 at 20:53 Yasel 3,1204 gold badges42 silver badges49 bronze badges asked Mar 27, 2016 at 19:53 captCCcaptCC 1051 gold badge3 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 9

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:

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")
  }
}  
发布评论

评论列表(0)

  1. 暂无评论