首页 > 解决方案 > 从一个选项卡复制非空范围,并粘贴到另一个选项卡的末尾,然后将公式复制到粘贴范围的右侧

问题描述

我用它来将非空单元格从一个选项卡 (CopyFromTab) 复制到另一个选项卡 (PasteToTab)。但是,当它向 PasteToTab 添加新行时...粘贴内容右侧的公式(在 PasteToTab 上)不会向下复制(在 PasteToTab 上)。

有没有办法将公式复制到 PasteToTab 上的新行中(这些需要复制的公式位于右侧的单元格中,使用粘贴内容中的数据)

function CopyNotEmptyToDifferentTab(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CopyFromTab");  //Copy from here
var range = ss.getRange(8,11,10,6);     //You should be using .getRange(row, column, numRows, numColumns)

  var nonEmpty = range.getValues().filter(function(row) {           // filter matrix for rows
        return row.some(function(col) { // that have at least one column
          return col !== "";});});      // that is not empty



  if (nonEmpty) {

    var sh2=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PasteToTab"); //Paste here

    sh2.getRange(sh2.getLastRow()+1,1, nonEmpty.length, nonEmpty[0].length).setValues(nonEmpty);
  } }

参考: 将非空范围复制到新工作表中,然后向下拖动公式


这是工作脚本:在粘贴表底部添加行,从工作表的副本中粘贴数据,并向下复制公式。. 感谢您的帮助@Stykes

function CopyNotEmptyToDifferentTab(){

var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CopyFromTab");  //Copy from here

var range = ss.getRange(8,11,10,6);     //You should be using .getRange(row, column, numRows, numColumns)

  var nonEmpty = range.getValues().filter(function(row) {           // filter matrix for rows
        return row.some(function(col) { // that have at least one column
          return col !== "";});});      // that is not empty

  if (nonEmpty) {


    var sh2=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PasteToTab"); //Paste here
   var lastRowNumber= sh2.getLastRow();  //GETS LAST ROW AS VARIABLE
    sh2.getRange(sh2.getLastRow()+1,1, nonEmpty.length, nonEmpty[0].length).setValues(nonEmpty);

    var sourceRange = sh2.getRange(lastRowNumber,7,1,6);  //You should be using .getRange(row, column, numRows, numColumns)

sourceRange.autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);



  } }

标签: google-apps-scriptgoogle-sheets

解决方案


使用:autoFillToNeighbor()

   //range you would like to copy down can be one cell or multiple
   var sourceRange = sheet.getRange("B1:B4");

   // Results in B5:whereeverColumnAEnds 
  sourceRange.autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);

创建的范围必须包括具有公式的最后一行。对于上面的代码,如果 B5 里面有公式,这个代码就不起作用了。相反,范围需要是“B1:B5”或只是“B5”


推荐阅读