范围方法getValues()返回和setValues()接受什么?
我想从我的工作表中获得范围。如
const ss = Spreadsheet.getActive(),
sh = ss.getSheetByName("Sheet1"),
rg = sh.getRange("A1:C1"),//has 1,2,3
values = rg.getValues();
console.log(values);
日志显示
[[1,2,3]]
,您可以看到我收到了所有三个元素。但是,当我记录数组的长度( array.length
)时,它只是1(而不是3)。当我使用 .indexof
或 .concludes
测试元素的存在时,它说-1或 false
。
const values = /*same as logged above*/[[1,2,3]];
console.log(values.indexOf(2));//got -1 expected 1
console.log(values.includes(1));//got false expected true
为什么?
我对 setValues()
也有同样的问题。
rg.setValues([1,2,3]);//throws error
错误是
“参数(number [])不匹配电子表格app.range.setValues的方法签名。”
我的具体问题是: getValues()
返回到底是什么?这是一种特殊的数组吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文档摘录:
从官方文档 )返回
始终返回a 两个尺寸值阵列
一维数组是
二维数组的数组中
有/是数组中的数组。
首先,它由行索引:IE,外部阵列的行作为内数阵列。然后每个内部阵列都有列元素。考虑以下简单电子表格:
a1:a3
包含3行,每行包含1列元素。这表示为[[1],[2],[3]]
。同样,以下范围代表以下数组。尝试根据a1表示法猜测数组结构:列表
列的行
结构
.length
.length
[[1],[2],[3],[3]
[[1,2,3]]
[[1,2],[2,3]]
[[2,3],[3,4],[4,[4,[4,] 5]]
[[2,3,4],[3,4,5]]
请注意二维如何提供方向。
请参阅下面的实时可视化:
在上面的示例中,我们具有转换为JavaScript号码类型的电子表格编号类型元素。您可以使用
type(type()
)。对应的JavaScript类型参考是在这里
检查使用
给定二维数组结构,要访问一个值,需要两个格式
array [row] [column]
的索引。在上表中,如果检索a2:c3
,则访问c3
,请使用values [1] [2]
。[1]
是范围A2:C3的第二行。请注意,范围本身从第二行开始。因此,在中,第二行给定范围是行3
[2]
是第三列c
。注意:
setValues()
将接受与对应于该阵列相对应的相同数组结构范围
设置。如果尝试使用一维数组,则错误被扔了。
与上述错误相关的答案:
< +列的+数字+列+in+the+range“>相关搜索
/包括使用严格的类型检查。当您将原语与数组对象进行比较时,它们将无法使用。您可以使用
参考:
Documentation excerpts:
From The official documentation,
getValues()
returnsIt ALWAYS returns a two dimensional array of values.
One dimensional array is
Two dimensional array is
There is/are array(s) inside a array.
It is indexed by row first: i.e., The outer array has rows as inner array. Then each inner array has column elements. Consider the following simple spreadsheet:
A1:A3
contains 3 rows and each row contains 1 column element. This is represented as[[1],[2],[3]]
. Similarly, The following ranges represent the following arrays. Try to guess the array structure based on the A1 notation:Notation
of Rows
of columns
Structure
.length
.length
[[1],[2],[3]]
[[1,2,3]]
[[1,2],[2,3]]
[[2,3],[3,4],[4,5]]
[[2,3,4],[3,4,5]]
Note how the two dimension provides direction.
See live visualization below:
In the above example, We have Spreadsheet Number type elements converted to JavaScript number type. You can check spreadsheet type using
=TYPE()
. Corresponding JavaScript type reference is hereCheck using
Given the two dimensional array structure, to access a value, two indexes of format
array[row][column]
is needed. In the above table, ifA2:C3
is retrieved, To accessC3
, Usevalues[1][2]
.[1]
is second row in range A2:C3. Note that the range itself starts on second row. So, second row in the given range is row3
[2]
is third columnC
.Notes:
setValues()
will accept the same array structure corresponding to therange
to set. If a 1D array is attempted, the erroris thrown.
Related answers to the above error:
indexOf/includes uses strict type checking. They won't work when you compare primitives against array objects. You can use Array.flat to flatten the 2D array to a 1D one. Alternatively, Use a plain old for-loop to check something.
References:
如果您要填充将写入电子表格范围的数组,则可以将其初始化为2个昏暗数组,因此:
If you are going to be populating an array that will be written to a spreadsheet range you can initialize it as a 2 dim array like so:
我遇到了类似的问题,并且偶然发现了
flat()
方法,用于使[[1,2,3]]数组变平[1,2,3]。果然,不仅flat()
以> getValues()
返回的对象的2个DIM数组,而且还将对象重新构成正确的数字和字符串! huzzah!表格数据的屏幕截图
这是我的测试脚本:
I was having a similar problem, and stumbled across the
flat()
method for flattening a [[1,2,3]] array to [1,2,3]. Sure enough, not only didflat()
take the 2 dim array of objects returned bygetvalues()
, but it reconstituted the objects back into proper numbers and strings! Huzzah!Screenshot of sheet data

Here's my test script: