首页 > 解决方案 > 新行的脚本并复制根据行索引自动增加的公式?

问题描述

我找到了我正在寻找的这个脚本,它几乎按照我想要的方式工作。

该脚本监视 F3 中的数字并将金额复制到新行。

剩下的唯一问题是它将精确公式复制到新行/单元格中,但不会像我简单地将其复制并粘贴到新行时那样增加行引用。

我四处搜索,发现可以使用 CopyTo 方法结合 getFormulas/setFormulas 使公式遵循行索引,并将其中没有公式的单元格留空。

我有另一个脚本可以做到这一点,但我想用这个来解决这个问题,因为我也需要这个中的计数功能。

由于我开始了解更多关于这方面的知识并掌握事情的窍门,我需要一些帮助:)

这是下面的脚本:

function insertXRows() {
  var sheets = SpreadsheetApp.getActiveSheet();
  var count = sheets.getRange(3,6).getValue(); //F3
  Logger.log(count);

  //Get my to be copied line
  var copyLine = sheets.getRange(9,2,1,7); //Fix these values depending on your row that you want to copy.
  while(count>0) {
    Logger.log(count);
    //Get last line of my data
    var lastRow = sheets.getDataRange().getValues().length;

    var newRow = sheets.getRange(lastRow+1, 2, 1, 7); //Fix these values depending on your row that you want to copy.
    for (var i = 0; i<copyLine.getValues()[0].length; i++) {
      if (copyLine.getFormulas()[0][i]!="") { //Check formulas first
        sheets.getRange(newRow.getRow(), newRow.getColumn()+i).setFormula(copyLine.getFormulas()[0][i]);
      } else if (copyLine.getValues()[0][i]!="") {
        sheets.getRange(newRow.getRow(), newRow.getColumn()+i).setValue(copyLine.getValues()[0][i]);
      }
    }
    count--;
  }
}

标签: google-sheets-formula

解决方案


推荐阅读