[英]Google Apps Script: Copy and Paste Formulas Only
I need to copy and paste Formulas only in a spreadsheet using Google Apps Script我只需要使用 Google Apps 脚本在电子表格中复制和粘贴公式
var range = activeSheet.getRange(targetRow-1, 1, 1, activeSheet.getLastColumn());
range.copyTo(activeSheet.getRange(targetRow, 1, 1, activeSheet.getLastColumn()), {contentsOnly:false});
This give me the full information, including the formulas and data.这给了我完整的信息,包括公式和数据。 However, I wish to only copy the formulas (without data).
但是,我只想复制公式(没有数据)。
CopyFormulasToRange
unfortunately does not exists. CopyFormulasToRange
不幸的是不存在。
Use getFormulasR1C1 and setFormulasR1C1 to copy formulas and preserve relative references.使用 getFormulasR1C1 和 setFormulasR1C1 复制公式并保留相对引用。 For example:
例如:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var currentRow = sheet.getLastRow();
var sourceRange = sheet.getRange(currentRow, 3, 1, 4);
var sourceFormulas = sourceRange.getFormulasR1C1();
currentRow++;
var targetRange = sheet.getRange(currentRow, 3, 1, 4);
targetRange.setFormulasR1C1(sourceFormulas);
https://developers.google.com/apps-script/reference/spreadsheet/range#getformular1c1 https://developers.google.com/apps-script/reference/spreadsheet/range#setformulasr1c1formulas https://developers.google.com/apps-script/reference/spreadsheet/range#getformular1c1 https://developers.google.com/apps-script/reference/spreadsheet/range#setformulasr1c1formulas
The following function will copy formulas only.以下函数将仅复制公式。 It has user settings for what row and column to start getting the formulas from.
它具有用于开始获取公式的行和列的用户设置。
function copyFormulas() {
var activeSheet,numberOfSourceColumnsToGet,sourceColumnStart,sourceFormulas,sourceRange,
sourceRowStart,targetColumn,targetRange,targetRowStart;
//USER INPUT
sourceRowStart = 1; //Row to start getting formulas from
sourceColumnStart = 2; //Column to start getting formulas from
numberOfSourceColumnsToGet = 1; //Number of columns to get formulas from
targetRowStart = 1; //Row to start copying formulas to
targetColumn = 3; //Column to start copying formulas to
//END OF USER INPUT
activeSheet = SpreadsheetApp.getActiveSheet();
sourceRange = activeSheet.getRange(sourceRowStart, sourceColumnStart, activeSheet.getLastRow(), numberOfSourceColumnsToGet);
sourceFormulas = sourceRange.getFormulas();//Get only formulas from the source range
targetRange = activeSheet.getRange(targetRowStart,targetColumn,sourceFormulas.length,sourceFormulas[0].length);
targetRange.setFormulas(sourceFormulas);//Copy the formulas to the target range
}
I found this question while trying to figure out how to copy just the formulas from one row into another row.我在试图弄清楚如何将公式从一行复制到另一行时发现了这个问题。 This is the very common pattern of adding a row to a spreadsheet to add another data set, where each set is a single row and some of the columns in that row are computed values.
这是向电子表格添加一行以添加另一个数据集的非常常见的模式,其中每个数据集是一行,并且该行中的一些列是计算值。 You want all of copied formulas to reference values in the new row, not the row from which the formulas were copied.
您希望所有复制的公式都引用新行中的值,而不是复制公式的行。
@AllenWell's answer copies the formulas, but for me at least, doesn't update the cell references to reference cells in the new row. @AllenWell 的答案复制了公式,但至少对我而言,不会将单元格引用更新为新行中的引用单元格。 With his code, the copied formulas still refer to the row from which the formulas were copied.
使用他的代码,复制的公式仍然引用从中复制公式的行。 I have since learned that the secret here is to use
get/setFormulasR1C1
rather than get/setFormulas
because the latter uses relative references such that formulas reference values in whatever row they're in.从那以后,我了解到这里的秘密是使用
get/setFormulasR1C1
而不是get/setFormulas
,因为后者使用相对引用,以便公式引用它们所在行中的值。
@MarkBird's answer uses get/setFormulasR1C1
but doesn't work for me. @MarkBird 的答案使用
get/setFormulasR1C1
但对我不起作用。 It seems to put something in every cell of the new row because it does a copy even for columns that do not contain formulas.它似乎在新行的每个单元格中都放了一些东西,因为即使对于不包含公式的列,它也会进行复制。 This screws up my logic, where I have formulas that put results in multiple columns, but only if they're blank.
这搞砸了我的逻辑,我有将结果放在多列中的公式,但前提是它们是空白的。 His code could work fine in simpler cases, but it would be better if it only copied formulas.
他的代码在更简单的情况下可以正常工作,但如果它只复制公式会更好。 That's what my solution does.
这就是我的解决方案所做的。
I wrote a more complex function that copies only the formulas in such a way that the formulas reference values in the new row.我写了一个更复杂的 function ,它只复制公式,使公式引用新行中的值。 I grab all the formulas and then selectively copy them to the new row, skipping empty spots in the list of formulas.
我抓取所有公式,然后有选择地将它们复制到新行,跳过公式列表中的空白点。 This gave me the behavior I wanted and I think what most people want when they want to create a new row in a spreadsheet.
这给了我想要的行为,我认为大多数人想要在电子表格中创建新行时想要什么。 Here is the function that does this.
这是执行此操作的 function。 it takes a
Sheet
reference and the indexes of the source and destination rows:它需要一个工作
Sheet
引用以及源行和目标行的索引:
function copyFormulas(sheet, sourceRow, targetRow) {
var formulas = sheet.getRange(sourceRow, 1, 1, sheet.getLastColumn()).getFormulasR1C1()[0];
for (var i = 0; i < formulas.length; i++) {
if (formulas[i] !== '') {
sheet.getRange(targetRow, i + 1).setFormulaR1C1(formulas[i]);
}
}
}
Notice the test for if the formula is empty.注意公式是否为空的测试。 I think this is what is missing from @MarkBird's simpler solution.
我认为这是@MarkBird 更简单的解决方案所缺少的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.