在动态范围内设置公式Google表功能
我正在使用此简单功能来快速使用公式(在第二行中)自动填充各个列。但是,我发现,如果我碰巧要在一个点上修改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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您的目标如下。
= a5& b5
放在单元格“ d5。在这种情况下,如何使用R1C1?修改脚本时? 如何
,以下修改脚本
来自
to
I believe your goal is as follows.
=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:
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
To