Xaan2000
Xaan2000

Reputation: 21

Javascript working with empty cells

I am working with Google Sheets. I want to build a custom function that adds 5 cells together. This code worked just fine:

function addData(a,b,c,d,e){
    return a+b+c+d+e
}

When I put the values: 80,80,80,80,80...I appropriately get 400. The problem happens when one of the cells in empty. When I have the values:

___,80,80,80,80 I get 80808080

80,___,80,80,80 I get 808080

80,80,___,80,80 I get 16080

80,80,80,___,80 I get 24080

80,80,80,80,___ I get 320 <--- correct answer

The function appears to add correctly until it hits an empty cell and then just tacks on the remaining values instead of continuing to add them.

I am very new to JavaScripting so I really do not know where to begin my research. Any help would be greatly appreciated. I asked this question before but I feel like this is more concise as to what I am trying to accomplish.

Upvotes: 2

Views: 44

Answers (1)

mohagali
mohagali

Reputation: 411

you get different results because each column has different format try to convert string type to integer or let script like that and change the column format to number

function addData(a,b,c,d,e){
    return parseInt(a)+parseInt(b) +parseInt(c) +parseInt(d) +parseInt(e) 
}

Upvotes: 1

Related Questions