Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Spreadsheet Script getValues - Force int instead of string

Is there a way to force .getRange().getValues() to return an int? Although only numbers exist in my range, it is returning them as strings. I would like to avoid using parseInt in every one of my statements or creating a separate array with converted values.

Or is that the only solution, to get the array and then parseInt the entire array in a loop?

like image 672
user1469541 Avatar asked Jun 20 '12 14:06

user1469541


People also ask

How do I convert a string to an int in Google script?

parseInt() returns a number, that's all you need.

How do I trigger a script in Google Sheets?

Under Run, select the name of function you want to trigger. Under Events, select either Time-driven or the Google App that the script is bound to (for example, From spreadsheet). Select and configure the type of trigger you want to create (for example, an Hour timer that runs Every hour or an On open trigger).

How do I lock a cell in a script in Google Sheets?

A protected range can protect either a static range of cells or a named range. A protected sheet may include unprotected regions. For spreadsheets created with the older version of Google Sheets, use the PageProtection class instead. // Protect range A1:B10, then remove all other users from the list of editors.


1 Answers

you can do this easily using the unary '+' operator as follows:

First get your values from your spreadsheet using getValue() or getValues(). Suppose you get two such values, and store them in A = 1 and B = 2. You can force them to be recognized as numbers by using any math binary operator except for +, which concatenates strings, so A - B = -1, while A + B will return '12'.

You can force the variables to be numbers simply by using the + unary operator with any variable that might be interpreted as a string. For example, +A + +B will return the correct value of 3.

like image 90
hoogamaphone Avatar answered Oct 05 '22 09:10

hoogamaphone