Copy non-empty Range from one tab, and paste at end of another tab, and copy down formulas to the right of the paste range

天大地大妈咪最大 提交于 2020-05-17 07:46:08

问题


I used this to copy the non-empty cells from One tab (CopyFromTab), to another tab (PasteToTab). However, when it adds new rows to the PasteToTab... the formulas to the right of the pasted content (on the PasteToTab) do not copy down (on the PasteToTab).

Is there a way for this to Copy Formulas down in new rows on the PasteToTab (these formulas that need to be copied down are in cells to the right that use the data from the pasted content)

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);
  } }

REFERENCE: Copy non-empty range into new sheet, and drag down formulas


THIS IS THE WORKING SCRIPT THAT: ADDS ROWS TO BOTTOM OF PASTE SHEET, PASTES DATA FROM COPYFROM SHEET, AND COPIES DOWN FORMULAS.. THANK YOU FOR YOUR HELP @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);



  } }

回答1:


Use: 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);

The range created must include the last row that has the formula. For the code above, if B5 has a formula in it, this code will not work. Instead the range would need to be "B1:B5" or just "B5"



来源:https://stackoverflow.com/questions/61757102/copy-non-empty-range-from-one-tab-and-paste-at-end-of-another-tab-and-copy-dow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!