如何使用Google Script将数组数据追加到电子表格列中? [英] How to append array data to spreadsheet column at once using Google Script?

查看:130
本文介绍了如何使用Google Script将数组数据追加到电子表格列中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将addArray []的数据一次追加到A列的末尾。但是,我的数组并不是多维的,因此无法使用此方法:

这是我向数组添加的方式:

  var toAdd = []; 
var thisurl =红苹果http://awebsite.com/1.jpg;
toAdd.push(thisUrl);

这就是我想一次追加数组的方式。

  function AddToSpreadsheet()
{
var data = SpreadsheetApp.getActiveSheet()。getRange('A12:A')。getValues );
var toAdd = [红苹果http://awebsite.com/1.jpg,
青苹果http://1awebsite.com/2.jpg,
红色苹果http://1awebsite.com/3.jpg,
烂苹果http://rottenApple.com]; (i = 0; i< data.length; ++ i){$ b($) $ b if(data [i] [0] ==){break;}
}
SpreadsheetApp.getActiveSheet()。getRange(12 + i,1,toAdd.length,1)。 setValues方法(TOADD);
}

}

如果我使用上面的代码i得到这个错误:

 无法将数组转换为对象[] []。 (line 

我的addArray不是多维数组!我如何将它转换为多维数组,如下例所示所以我可以使用上面的代码?

多维数组示例:

pre $ var toAdd = [[red apple http://awebsite.com/1.jpg],
[green apple http://1awebsite.com/2.jpg],
[ red apple http://1awebsite.com/3.jpg],
[rotten apple http://rottenApple.com]];

编辑:我在开始时转换了数组并且工作了!

  var values_array = [红苹果http://awebsite.com/1.jpg,
青苹果http://1awebsite.com/2.jpg,
红苹果http://1awebsite.com/3.jpg,
烂苹果http://rottenApple.com];

var toAddArray = [];
for(i = 0; i toAddArray.push([toAdd [i]]);
}
...........

这样我插入整个新数组到A列:

  ............ 
SpreadsheetApp.getActiveSheet()。 getRange(12 + i,1,toAddArray.length,1).setValues(toAddArray);


解决方案

您需要转置阵列。
您可以通过创建一个新数组并将值作为具有一个值的数组附加值来实现。



在调用setValues(toAdd)之前尝试

  var toAddArray = []; 
for(i = 0; i toAddArray.push([toAdd [i]]);
}

将设定值调用替换为

  SpreadsheetApp.getActiveSheet()。getRange(12 + i,1,toAdd.length,1).setValues(toAddArray); 

设定值函数需要一个矩阵作为输入。由于没有本地矩阵,它需要一个阵列数组,其中每个子阵列具有相同的长度并且不包含数组。每个数组中的每个数组都是一行,并且每个数组的每个元素都是成一列。

如果你想要一维数组中的一列行,你需要一个包含一个元素数组的数组。如果你想编写列,你可以创建一个包含所有值的数组的数组。


I am trying to append data of addArray[] to end of column A at once. However, my array is not multidimensional to be able to use this method:

this is how i add to array:

  var toAdd=[];
  var thisurl="red apple http://awebsite.com/1.jpg";
  toAdd.push(thisUrl);

And this is the way i would like to append the array at once

function AddToSpreadsheet()
{
 var data = SpreadsheetApp.getActiveSheet().getRange('A12:A').getValues();
    var toAdd=["red apple http://awebsite.com/1.jpg",
              "green apple http://1awebsite.com/2.jpg",
              "red apple http://1awebsite.com/3.jpg",
              "rotten apple http://rottenApple.com"];



  if(toAdd.length > 0){
    for(i = 0; i < data.length; ++i){
      if(data[i][0] == ""){break;}
    }
    SpreadsheetApp.getActiveSheet().getRange(12+i, 1, toAdd.length, 1).setValues(toAdd);
  } 

}

if i use this above code i get this error:

Cannot convert Array to Object[][]. (line 

My addArray is not multidimensional array ! How i can convert it to multidimensional array like the following example so i can use the above code ?

Multidimensional array example :

var toAdd=[["red apple http://awebsite.com/1.jpg"],
               ["green apple http://1awebsite.com/2.jpg"],
               ["red apple http://1awebsite.com/3.jpg"],
               ["rotten apple http://rottenApple.com"]];

Edit: I converted the array at start and worked!

var values_array=["red apple http://awebsite.com/1.jpg",
               "green apple http://1awebsite.com/2.jpg",
               "red apple http://1awebsite.com/3.jpg",
               "rotten apple http://rottenApple.com"];

  var toAddArray = [];
for (i = 0; i < toAdd.length; ++i){
    toAddArray.push([toAdd[i]]);
}
........... 

and this way i inserted the whole new array to column A:

............
     SpreadsheetApp.getActiveSheet().getRange(12+i, 1, toAddArray.length, 1).setValues(toAddArray);
}

解决方案

You need to "transpose" the array. You can do that by creating a new array and appending the values as arrays with one value.

before you call setValues(toAdd) try

var toAddArray = [];
for (i = 0; i < toAdd.length; ++i){
    toAddArray.push([toAdd[i]]);
}

And replace the set values call with

 SpreadsheetApp.getActiveSheet().getRange(12+i, 1, toAdd.length, 1).setValues(toAddArray);

The set values function needs a matrix as an input. Since there is no native matrix it requires an array of arrays where each of the subarrays have the same length and contain no arrays.
Each array within the "matrix" is a row and each element of each arrays goes into a column.
If you want one column of rows from a one dimensional array you need an array with one-element arrays. If you want to write columns you make an array containing one array of all values.

这篇关于如何使用Google Script将数组数据追加到电子表格列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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