繁体   English   中英

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

[英]Google Apps Script: Copy and Paste Formulas Only

我只需要使用 Google Apps 脚本在电子表格中复制和粘贴公式

var range = activeSheet.getRange(targetRow-1, 1, 1, activeSheet.getLastColumn());
range.copyTo(activeSheet.getRange(targetRow, 1, 1, activeSheet.getLastColumn()), {contentsOnly:false});

这给了我完整的信息,包括公式和数据。 但是,我只想复制公式(没有数据)。

CopyFormulasToRange不幸的是不存在。

使用 getFormulasR1C1 和 setFormulasR1C1 复制公式并保留相对引用。 例如:

  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

以下函数将仅复制公式。 它具有用于开始获取公式的行和列的用户设置。

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
}

我在试图弄清楚如何将公式从一行复制到另一行时发现了这个问题。 这是向电子表格添加一行以添加另一个数据集的非常常见的模式,其中每个数据集是一行,并且该行中的一些列是计算值。 您希望所有复制的公式都引用新行中的值,而不是复制公式的行。

@AllenWell 的答案复制了公式,但至少对我而言,不会将单元格引用更新为新行中的引用单元格。 使用他的代码,复制的公式仍然引用从中复制公式的行。 从那以后,我了解到这里的秘密是使用get/setFormulasR1C1而不是get/setFormulas ,因为后者使用相对引用,以便公式引用它们所在行中的值。

@MarkBird 的答案使用get/setFormulasR1C1但对我不起作用。 它似乎在新行的每个单元格中都放了一些东西,因为即使对于不包含公式的列,它也会进行复制。 这搞砸了我的逻辑,我有将结果放在多列中的公式,但前提是它们是空白的。 他的代码在更简单的情况下可以正常工作,但如果它只复制公式会更好。 这就是我的解决方案所做的。

我写了一个更复杂的 function ,它只复制公式,使公式引用新行中的值。 我抓取所有公式,然后有选择地将它们复制到新行,跳过公式列表中的空白点。 这给了我想要的行为,我认为大多数人想要在电子表格中创建新行时想要什么。 这是执行此操作的 function。 它需要一个工作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]);
      }
    }
}

注意公式是否为空的测试。 我认为这是@MarkBird 更简单的解决方案所缺少的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM