即使值不在范围内,如果条件继续,则 Google 表格

Google Sheet if condition continue even if value not in range

提问人:Lobbyx3 提问时间:9/22/2023 更新时间:9/22/2023 访问量:27

问:

function checkRowCol(row, col) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getActiveSheet();
  if (13 <= row <= 16 || 29 <= row <= 32 || 45 <= row <= 48 || 61 <= row <= 64 || 77 <= row <= 80 || 93 <= row <= 96 || 109 <= row <= 112 || 125 <= row <= 128) {
    
    if (col == 6 || col == 12 || col == 18){
      if (13 <= row <= 16 || 29 <= row <= 32 || 45 <= row <= 48 || 61 <= row <= 64 || 77 <= row <= 80 || 93 <= row <= 96 || 109 <= row <= 112 || 125 <= row <= 128) {
      ownedUpdate(row, col);
      }
    }
    // complete
    else if(col == 3 || col == 9 || col == 15) {
      if (13 <= row <= 16 || 29 <= row <= 32 || 45 <= row <= 48 || 61 <= row <= 64 || 77 <= row <= 80 || 93 <= row <= 96 || 109 <= row <= 112 || 125 <= row <= 128) {
        if (ws.getRange(row, col + 3).getValue() == false && ws.getRange(row, col ).getValue() == true) {
          ws.getRange(row, col).setValue(false);
          SpreadsheetApp.getActive().toast("You cannot complete without owning the item sir");
        }
        else {
            completedUpdate(row, col);
            if (ws.getRange(row, col).getValue() == true){
              ownedUpdate(row, col + 3);
            }
        }
      }
    }
  }
}

我有这个代码块,我已经制作了如果第 6 行第 17 行被更改,所以什么都不会发生,但由于某种原因,它仍然会进入 if 打印(“先生,您无法在不拥有该项目的情况下完成”),我似乎不明白为什么。 我尝试将范围放在“(13 <= row <= 16)....” 正如你所看到的,我已经尝试过用这个条件做额外的如果。 但出于某种原因,当选择第 17 行时,它仍然会进入......不明白为什么

尝试输入值 row = 17, col= 3 结果是

if (ws.getRange(row, col + 3).getValue() == false && ws.getRange(row, col ).getValue() == true) {
          ws.getRange(row, col).setValue(false);
          SpreadsheetApp.getActive().toast("You cannot complete without owning the item sir");
        }

尝试了多个 IF,甚至尝试了不应该以此结果结尾的否定 IF,但仍然......这就是我得到的...... :帮助:

javascript if google 表格 条件语句

评论


答:

0赞 AztecCodes 9/22/2023 #1

代码中的问题源于代码各个部分中的条件和类似条件。在 中,不可能像这样链接比较运算符。相反,您应该使用单独的比较并合并逻辑运算符以正确组合它们。(13 <= row <= 16)JavaScript(either && or ||)

固定代码:

function checkRowCol(row, col) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getActiveSheet();
  if ((13 <= row && row <= 16) || (29 <= row && row <= 32) || (45 <= row && row <= 48) || (61 <= row && row <= 64) || (77 <= row && row <= 80) || (93 <= row && row <= 96) || (109 <= row && row <= 112) || (125 <= row && row <= 128)) {

    if (col == 6 || col == 12 || col == 18) {
      if ((13 <= row && row <= 16) || (29 <= row && row <= 32) || (45 <= row && row <= 48) || (61 <= row && row <= 64) || (77 <= row && row <= 80) || (93 <= row && row <= 96) || (109 <= row && row <= 112) || (125 <= row && row <= 128)) {
        ownedUpdate(row, col);
      }
    }
    // Complete
    else if (col == 3 || col == 9 || col == 15) {
      if ((13 <= row && row <= 16) || (29 <= row && row <= 32) || (45 <= row && row <= 48) || (61 <= row && row <= 64) || (77 <= row && row <= 80) || (93 <= row && row <= 96) || (109 <= row && row <= 112) || (125 <= row && row <= 128)) {
        if (ws.getRange(row, col + 3).getValue() == false && ws.getRange(row, col).getValue() == true) {
          ws.getRange(row, col).setValue(false);
          SpreadsheetApp.getActive().toast("You cannot complete without owning the item sir");
        } else {
          completedUpdate(row, col);
          if (ws.getRange(row, col).getValue() == true) {
            ownedUpdate(row, col + 3);
          }
        }
      }
    }
  }
}