在动态范围内设置公式Google表功能

发布于 2025-02-14 00:46:16 字数 612 浏览 2 评论 0原文

我正在使用此简单功能来快速使用公式(在第二行中)自动填充各个列。但是,我发现,如果我碰巧要在一个点上修改SMTH(例如C4),然后添加更多行并需要重新运行,它将再次从C2完成,并覆盖我进行的任何修改再次使用公式。 这就是为什么我想要更动态的东西(将公式设置在最后一行中,引用各自行中的其他列/单元格)。我想到的快速修复不会将其视为正确的范围。我试图避免使用循环或其他复杂解决方案,而只是引用最后一行,但似乎不起作用。我可以对此代码进行简单的修复。 您可以在此处查看电子表格示例

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();

var FillC = ss.getRange(2,3,lr-1);
ss.getRange("C2").copyTo(FillC);
//ss.getRange("C"&lr).setFormula('A'&lr&'B'&lr)
//ss.getRange("C2").setFormula (='A'& lastrow(in this case 5) & 'B'& lastrow(5)
}

I'm using this simple function to quickly autofill the respective column with a formula (in the 2nd row). However, I found that if I happen to want to modify smth in a cell (say C4) at one point, then add more rows and need to re-run it will do it from C2 again and will overwrite any modifications I've made with the formula again.
This is why I'd like something more dynamic (set the formula in the last row, referencing other columns/cells from that respective row). The quick fixes I thought of don't take it as a correct range. I've tried to avoid using loops or other complex solutions and just referencing the last row, but it doesn't seem to work. Is there an easy fix I could make to this code.
You can see spreadsheet example here here. I hope it makes sense.

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();

var FillC = ss.getRange(2,3,lr-1);
ss.getRange("C2").copyTo(FillC);
//ss.getRange("C"&lr).setFormula('A'&lr&'B'&lr)
//ss.getRange("C2").setFormula (='A'& lastrow(in this case 5) & 'B'& lastrow(5)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2025-02-21 00:46:17

我相信您的目标如下。

  • 当单元格“ d2:d4”具有值时,您想要一个脚本以将公式= a5& b5放在单元格“ d5。

在这种情况下,如何使用R1C1?修改脚本时? 如何

,以下修改脚本

function myFunction() {
  // Ref: https://stackoverflow.com/a/44563639
  Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) {
    const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious();
    return search ? search.getRow() : offsetRow;
  };

  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var row = ss.get1stNonEmptyRowFromBottom(4);
  ss.getRange(row + 1, 4).setFormulaR1C1("=R[0]C[-3]&R[0]C[-2]");
}
  • ?以下。

    • 来自

        ss.getRange(row + 1,4).setFormular1c1(“ = r [0] c [-3]& r [0] c [-2]”);
       
    • to

        ss.getRange(row + 1,4,ss.getlastrow() -  row -1).setFormular1c1(“ = r [0] C [-3]& r [0] C [-2 ]”);
       

I believe your goal is as follows.

  • When the cells "D2:D4" have values, you want a script for putting a formula =A5&B5 to the cell "D5.

In this case, how about using R1C1? When your script is modified, how about the following modification?

Modified script:

function myFunction() {
  // Ref: https://stackoverflow.com/a/44563639
  Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) {
    const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious();
    return search ? search.getRow() : offsetRow;
  };

  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var row = ss.get1stNonEmptyRowFromBottom(4);
  ss.getRange(row + 1, 4).setFormulaR1C1("=R[0]C[-3]&R[0]C[-2]");
}
  • For example, for the above situation, when you want to put the formulas to the cells from "D5" to the last row, please modify them as follows.

    • From

        ss.getRange(row + 1, 4).setFormulaR1C1("=R[0]C[-3]&R[0]C[-2]");
      
    • To

        ss.getRange(row + 1, 4, ss.getLastRow() - row - 1).setFormulaR1C1("=R[0]C[-3]&R[0]C[-2]");
      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文