问题
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