将变量设置为零javascript

时间:2016-07-13 04:58:29

标签: javascript

我正在尝试将变量设置为数字零,并基于计数 关于IF声明。但是,当我运行下面的代码时,ChangeOver立即被选为1,参与第3个IF语句,抛弃其他所有内容。如何从0开始计算每个变量并相应地计算IF语句?

function clearX() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var BlankStart = 0;
  var CountX = 0;
  var NewBlanks = 0;
  var ChangeOver = 0;

  for (var i=5;i<10;i++) {
    var AValue = sheet.getRange(i,1).getValue();

    if ((BlankStart > 1) && (CountX > 3)) {    //If initital blanks and more than 3 X's identified, next blanks are new blanks (FlipBlank)
      ChangeOver = 1;
    }
    if ((AValue === "") && (ChangeOver = 0)) {  //If Col A is blank and less than 3 X's encountered yet (FlipBlank) 
      BlankStart = BlankStart + 1;
    }
    if ((AValue === "") && (ChangeOver = 1)) {  //If Col A is blank after 3 or more X's (FlipBlank), start new blanks
      NewBlanks = NewBlanks + 1;
    }
    if (AValue === "X") {  //If Col A = 'X' 
      CountX = CountX + 1;
    }
    sheet.getRange(i,17).setValue(BlankStart);
    sheet.getRange(i,18).setValue(CountX); 
    sheet.getRange(i,19).setValue(ChangeOver);    
    sheet.getRange(i,20).setValue(NewBlanks);  
  }                         

}

2 个答案:

答案 0 :(得分:0)

我的完全没有明确你的问题,但是如果我猜错了你输错了你的谓词:

function clearX() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var BlankStart = 0, CountX = 0, NewBlanks = 0;
  var ChangeOver = 0;

  for (let i = 5; i < 10; i++) {
    let AValue = sheet.getRange(i, 1).getValue();

    if (BlankStart > 1 && CountX > 3)    //If initital blanks and more than 3 X's identified, next blanks are new blanks (FlipBlank)
      ChangeOver = 1;

    if (AValue && ChangeOver === 0)  //If Col A is blank and less than 3 X's encountered yet (FlipBlank) 
      BlankStart++;

    if (AValue && ChangeOver === 1)  // If Col A is blank after 3 or more X's (FlipBlank), start new blanks
      NewBlanks++;

    if (AValue === "X")  // If Col A = 'X' 
      CountX++;

    sheet.getRange(i,17).setValue(BlankStart);
    sheet.getRange(i,18).setValue(CountX); 
    sheet.getRange(i,19).setValue(ChangeOver);    
    sheet.getRange(i,20).setValue(NewBlanks);  
  }                         

}

答案 1 :(得分:0)

这已经解决了。我只需要在我的IF语句中将“= 0”更改为“=== 0”。谢谢!