简体   繁体   English

Google Apps 脚本:仅复制和粘贴公式

[英]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.

相关问题 Google Apps 脚本/Google 表格 - 仅复制 IF - Google Apps Script/ Google Sheets - Only copy IF 如何使用 Google 应用程序脚本复制和粘贴“Over the Cells”图像? - How to copy and paste "Over the Cells" images using Google apps script? Google Apps 脚本:展平一系列公式 - Google apps script : Flatten a range of formulas 通过 Google Apps 脚本在 Google forms 中禁用文本选择、复制、剪切和粘贴 - Disable text selection, copy, cut and paste in Google forms through Google Apps Script 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script 如何加快速度惊人的慢谷歌表格应用程序脚本功能,将公式从一列复制到新插入的列 - How to speed up surprisingly slow Google Sheets Apps Script functions that copy formulas from one column to newly inserted column 如何在 Google Apps 脚本中创建的自定义公式中隐藏正在加载...? - How to hide Loading... in custom formulas created in Google Apps Script? Google表格脚本:如何每天自动复制公式 - Google Sheets script: how to automatically copy down formulas each day 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] Google Apps脚本。 *复制*文件 - Google Apps Script. *Copy* a file
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM