向多行填充范围(多列) - google-apps-script [英] Fill a range (multiple columns) down multiple rows - google-apps-script

查看:93
本文介绍了向多行填充范围(多列) - google-apps-script的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地填写了一列(A),范围为A1 ...

  var ss = SpreadsheetApp.getActiveSpreadsheet()getSheetByName( 工作表Sheet); 
var lastRow = ss.getDataRange()。getNumRows();
变种rngVal = ss.getRange( A1)的getValue()
ss.getRange。( A2:A + LASTROW).setValue(rngVal)

然后我以为我在easy-street上,我试图修改/应用到一个更大的范围,填充多列范围在范围C1中找到的值... H1 ...

  var ss = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Sheet1) ; 
var lastRow = ss.getDataRange()。getNumRows();
var rngVal = ss.getRange(C1:H1)。getValues()
ss.getRange(C2:H+ lastRow).setValues(rngVal)


$ b

很明显,这比简单地在单词Value的末尾添加一个S还要多一点。



该错误读取如下:


不正确的范围高度为1,但应为10


(FYI:var lastRow = 11)

顺便说一句,如果我使用Value而不是Value s ,则会出现错误,尽管我最终得到的单元格中只有C1范围内的值。

所以我接近....或者离开。其中之一。



帮助?

解决方案

错误消息非常明确......数组的大小必须适合getValues和setValues中的范围。尝试像这样

$ b $ pre $函数myFunction(){
var ss = SpreadsheetApp.getActiveSpreadsheet() .getSheetByName( 工作表Sheet);
var lastRow = ss.getLastRow()
var rngVal = ss.getRange(C1:H+ lastRow).getValues(); //获得一个10行6列的数组
ss.getRange(C2:H+(lastRow + 1))。setValues(rngVal); //将这个数组写回到一个大小相同的范围。 (从Row2开始,它必须以lastRow + 1结束,以保持相同数量的行
}

这个函数会将范围C1:H last Row移到C2:H lastRow + 1,不确定它是否非常有用,但这不是重点; - )

编辑:对不起,我完全不明白你的要求......这是一个代码,可以从C1:H1下面的所有行中复制数据

  function myFunction(){
var ss = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Sheet1);
var lastRow = ss.getLastRow()
var rngVal = ss.getRange(C1:H1)。getValues(); //获取第一行的数组
var rowData = rngVal [0]
var newData = []
(n = 1; n ss.getRange(C2: H+ lastRow).setValues(newData); //将此数组写回到具有相同大小的范围。 (从Row2开始,它必须以lastRow + 1结束,以保持相同数量的行
}

EDIT2遵循以下评论:



一句解释:



当使用 range.getValues()时,我们得到一个二维数组,这意味着一个数组数组,可以表示为: [[data1,data2,data3],[data4,data5,data6]] data1,2& 3是第一个数组(索引0)和数据4,5 & 6是第二个数组(索引1)的值,所以如果你想获得第一个数组的值,你必须这样写: value = arrayName [0] ,这将返回一维数组 [data1,data2,data3] ,这就是我用来获取rowData的数据。



现在我们需要再次获得一个2维数组,以便能够将新数据写回到电子表格中的一个范围,因此我们创建一个新数组( var newData = [] var newData = new Array()完全一样),并且在for循环中,我们将rowData数组添加到这个新数组中......结果将是一个数组数组,这实际上就是我们正在寻找,我们可以直接写在一张单一的 setValues 语句中。


I had success filling a single column (A) with the value found in range A1...

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var lastRow = ss.getDataRange().getNumRows();  
  var rngVal = ss.getRange("A1").getValue()
  ss.getRange("A2:A"+lastRow).setValue(rngVal)

So then I thought I was on easy-street, and I tried to modify/apply that to a larger range by filling a multi-column range with the values found in range C1:H1...

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var lastRow = ss.getDataRange().getNumRows();  
  var rngVal = ss.getRange("C1:H1").getValues()
  ss.getRange("C2:H"+lastRow).setValues(rngVal)

Apparently there is a bit more to this than simply slapping an "S" onto the end of the word "Value".

The error reads as follows:

Incorrect range height, was 1 but should be 10

(FYI: var lastRow = 11)

Btw, I get no error if I use Value instead of Values, although I end up with cells full of the value found only in range C1.

So I'm close.... or way off. One of those.

Help???

解决方案

The error message is quite explicit... the size of the array must fit into the range in both getValues and setValues. Try it like this :

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var lastRow = ss.getLastRow()  
  var rngVal = ss.getRange("C1:H"+lastRow).getValues();// get an array of 10 "rows" and 6 "columns"
  ss.getRange("C2:H"+(lastRow+1)).setValues(rngVal);//write back this array to a range that has the same size. (starting from Row2 it must ends on lastRow+1 to keep the same number of "rows"
}

This function will shift the range C1:H last Row to C2:H lastRow+1, not sure it is very useful but that's not the point here ;-)


EDIT : sorry, I didn't understand exactly your requirement... here is a code that reproduce data from C1:H1 in all rows below

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var lastRow = ss.getLastRow()  
  var rngVal = ss.getRange("C1:H1").getValues();// get an array of the first row
  var rowData = rngVal[0]
  var newData = []
  for(n=1;n<lastRow;++n){newData.push(rowData)}
  ss.getRange("C2:H"+lastRow).setValues(newData);//write back this array to a range that has the same size. (starting from Row2 it must ends on lastRow+1 to keep the same number of "rows"
}

EDIT2 following your comment below :

A word of explanation :

When using range.getValues() we get a 2 dimensions array, meaning an array of arrays that can be represented as follow : [[data1,data2,data3],[data4,data5,data6]] data1, 2 & 3 are the value of the first array (index 0) and data 4,5 & 6 are the values of the second array (index 1). So if you want to get the values in the first array you have to write it like this : value = arrayName[0] and this will return a one dimension array [data1,data2,data3], that's what I used to get rowData.

Now we need to get a 2 dimension array again to be able to write back the new data to a range in the spreadsheet. Therefor we create a new array (var newData=[] or var newData = new Array() does exactly the same), and in the for loop we add the rowData array to this new array... the result will be an array of arrays, that is actually what we were looking for and we can write this directly to the sheet in one single setValues statement.

这篇关于向多行填充范围(多列) - google-apps-script的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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