google-sheets-formula - 新行的脚本并复制根据行索引自动增加的公式?
问题描述
我找到了我正在寻找的这个脚本,它几乎按照我想要的方式工作。
该脚本监视 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--;
}
}
解决方案
推荐阅读
- azure - Azure 负载均衡器主机状态
- elasticsearch - 如何在 6.2 版本中启动弹性搜索时修复“java.lang.ArrayIndexOutOfBoundsException”异常
- jquery - getJSON 请求运行缓慢
- html - rotate(90deg) 不适合打印
- javascript - 如何实现依赖于查询字符串的弹出窗口?
- jquery - 即使条件为假,if 语句仍在执行
- c# - MemoryCache 的 UpdateCallback 坏了,只对过期物品有效
- java - Docker 运行小型 Java 类:无法找到或加载主类(即使它存在)
- wordpress - 如何在 gatsby-source-wordpress 中获取 ACF 分类值选项列表
- python - 为什么如果第一个元素为True,OR返回True,但即使第一个元素为True,ANY仍会检查所有元素