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

Copy 2D array to another sheet

本文关键字:另一个 工作 复制 2D 数组      更新时间:2023-09-26

我想从几个2D数组中复制单元格的值,这些值位于最终工作表的中间(由一个函数生成,在几个包含数据的工作表的帮助下)到另一个工作表,该工作表将在执行函数后返回该数据,以避免粉碎数据,因为我没有找到我最后一个问题的解决方案。

谢谢你的帮助。

不太确定这是不是你想要的,但我在谷歌开发者网站上发现了这个:

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(表,整数,整数,整数,整数)

我整理了一个快速示例,可能会有所帮助。我要查找的示例中的数据是位于四表电子表格的第二页中的"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);
}

我不太清楚缩进是怎么回事。: - (

相关文章: