将2D数组复制到另一个工作表

时间:2014-06-18 13:42:04

标签: javascript google-apps-script google-sheets

我想从几个2D数组中复制单元格的值,这些数组位于最终工作表的中间(由函数生成,借助于包含数据的多个工作表)到另一个工作表中执行函数后的数据,以避免破坏数据,因为我没有找到my last questions 的解决方案。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

不确定这是不是您的追随者,但我在Google开发者网站上发现了这一点:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheets()[0];
var destination = ss.getSheets()[1];

var range = source.getRange("B2:D4");

// This copies the data in B2:D4 in the source sheet to
// D4:F6 in the second sheet
range.copyValuesToRange(destination, 4, 6, 4, 6);

https://developers.google.com/apps-script/reference/spreadsheet/range#copyValuesToRange(Sheet,Integer,Integer,Integer,Integer)

希望这有帮助。

答案 1 :(得分:0)

我已经汇总了一个可能有帮助的快速示例。我想要找到的示例中的数据是位于四张电子表格的第二张表中的“2b2”。我想将它所在的列复制到第4张。它不漂亮,但它完成了这项工作。 : - )

function testCopy() {
  //get the active spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

 //create an array of all the sheets in our spreadsheet
 var sheets = ss.getSheets();

 // Declare the destination sheet to copy the data to
 var destination = sheets[3];

 //create an index for the sheet that hold the data we want to copy
 var targetSheet = 0;

//loop through each of the sheets to check for the data we are looking for
for (var i = 0; i < sheets.length -1; i++) {

  // Create a 2D array that holds all of the data we want to test from the sheet
  var rowData = sheets[i].getRange('A1:C3').getValues();

  //Loop through each row of data in the 2D array
  for (j = 0; j < rowData.length; j++) {

    // Create an array of the cells in the row
    var cells = rowData[j];

   //loop through each of the cells
   for (k = 0; k < cells.length; k++) {

    // check to see if the cell has the data we are looking for and if so set the   
    // target sheet index to the current sheet
    if (cells[k] == '2b2'){
      targetSheet = i;
    }
   }
  }
}

//set the range of the data we want copy for this example a single column
var copyRange = sheets[targetSheet].getRange('B1:B3');

// copy the data to the destination sheet into D4:D6
copyRange.copyValuesToRange(destination, 4, 4, 4, 6);

}

不太清楚压痕发生了什么。 : - (