google-apps-script - 从一个选项卡复制非空范围,并粘贴到另一个选项卡的末尾,然后将公式复制到粘贴范围的右侧
问题描述
我用它来将非空单元格从一个选项卡 (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);
} }
解决方案
//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”
推荐阅读
- android - 如何在 Android 中获得等效的 Google Text-to-Speech 音高
- rust - 如何复制另一个结构的字段?
- python - 将 django 频道与现有 django 3.1 wsgi 代码集成时出错
- mongodb - MongoDB View vs Function 抽象查询和传递的变量/参数
- c - 错误:二进制的无效操作数>>(有'float'和'int')
- api - 为什么我只得到 id 作为回应?IGDB 接口
- python - “缺少空格”是不是很糟糕?
- python - 如何从 (m,) 值的 Sympy 矩阵中获取“广播” (m,n) 数组,该矩阵应该以 (n,) numpy 数组作为参数?
- python - 在 c# 应用程序中使用 python 项目
- python - 选择一个有效的选项。该选择不是可用的选择之一。Django 错误