如何将数组写入Google Spreadsheet? [英] How to write an array to Google Spreadsheet?

查看:97
本文介绍了如何将数组写入Google Spreadsheet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个具有整数值的数组,并尝试一次将其写入Google Spreadsheet:

I'm building an Array with integer values and trying to write it to a Google Spreadsheet in one go:

var myArray=new Array();
    for (i=1;i<=100;i++){
      myArray[i]=i;
    };

    ss.getRange(8,4,1,100).setValue(myArray);

这是在正确的单元格上写的,但是每个单元格的内容是:

This is writing on the right cells but the content of each cell are:

[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, ...]

推荐答案

尝试一下,这将为您提供100个单元格,其值从0到99:

Try this, which gives you 100 cells with values from 0 to 99:

var myArray = [];

for (i=0 ; i<100 ; i++){
  myArray[i] = i;
}

sheet.getRange(8, 4, 1, 100).setValues([myArray]);

对您不起作用的一个原因是您使用的是setValue()而不是setValues().另一个原因是,如果您一直使用setValues(),那么您将试图将1D数组强制转换为本质上是2D数组.要解决该问题,请确保使用setValues([myArray]),它从一维myArray而不是setValue(myArray)构成2D数组.

One reason this wasn't working for you is that you were using setValue() instead of setValues(). Another reason is that, had you been using setValues(), you would have been trying to force a 1D array into what is essentially a 2D array. To solve the problem, be sure to use setValues([myArray]), which makes a 2D array from the one dimensional myArray, instead of setValue(myArray).

这篇关于如何将数组写入Google Spreadsheet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆