Home > Software engineering >  Value of 0 is being flagged as an empty cell in Apps Script, how do I get 0 to not be flagged?
Value of 0 is being flagged as an empty cell in Apps Script, how do I get 0 to not be flagged?

Time:10-31

After combing several cells into an array, I am checking over that array to confirm that the User has not missed any inputs through a quick check for empty cells. It works beautifully every time, unless some of those values are the number 0. When any input is 0, it triggers the flag asking the User to enter a value into each cell. 0 should be an acceptable value for the purposes of this tool, so I want to allow that while not allowing missed (blank) cells. I've been digging through documentation from Apps Script and searching StackOverflow for similar issues, but I've come up blank so far.

Below is the code I have for this function.

       
     console.log(sourceVals)

     const anyEmptyCell = sourceVals.findIndex(cell => cell == "");
     if(anyEmptyCell !== -1){
     const ui = SpreadsheetApp.getUi();
     ui.alert(
       "Input Incomplete",
       "Please enter a value in ALL input cells before submitting",
       ui.ButtonSet.OK
     );
     return;
    

CodePudding user response:

Try it like this:

function testie() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const vs = sh.getRange("A1:A23").getValues().flat();
  const anyEmptyCell = vs.findIndex(cell => cell === "");//just change the comparison operator
  if (anyEmptyCell !== -1) {
    const ui = SpreadsheetApp.getUi();
    ui.alert(
      "Input Incomplete",
      "Please enter a value in ALL input cells before submitting",
      ui.ButtonSet.OK
    );
  }
}

My Data:

A
1 COL1
2 17
3 14
4 1
5 19
6 14
7 1
8 11
9 3
10 16
11 0
12 19
13 8
14 2
15 15
16 10
17 19
18 12
19 2
20 1
21 11
22
23

It finds an empty cell at index 21 which is row 22 and there is a zero in the column

  • Related