强制int而不是字符串

强制int而不是字符串

本文介绍了Google Spreadsheet Script getValues - 强制int而不是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法强制.getRange()。getValues()返回一个int?虽然在我的范围内只有数字,但它将它们作为字符串返回。我想避免在我的每个语句中使用parseInt或创建一个单独的数组与转换值。

或者是唯一的解决方案,获取数组,然后parseInt整个数组在循环?

解决方案

使用一元运算符'+'可以很容易地做到这一点,如下所示:或 getValues()从电子表格中获取值C $ C>。假设你有两个这样的值,并将它们存储在 A = 1 和 B = 2 中。你可以使用除< + 之外的任何数学二元运算符来强制它们被识别为数字,这会连接字符串,所以 A - B = -1 ,而 A + B 将返回'12'。



您可以强制变量通过使用 + 一元运算符以及任何可能被解释为字符串的变量来简化数字。例如, + A + + B 将返回正确的3。


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?

解决方案

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.

这篇关于Google Spreadsheet Script getValues - 强制int而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:33