Javascript 在Google脚本编辑器中将公式添加到函数
使用现有代码,需要向函数中添加动态公式 该脚本设计用于简单的时间跟踪,在谷歌工作表中使用按钮进行启动/停止时间跟踪Javascript 在Google脚本编辑器中将公式添加到函数,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,使用现有代码,需要向函数中添加动态公式 该脚本设计用于简单的时间跟踪,在谷歌工作表中使用按钮进行启动/停止时间跟踪 函数设置值(cellName,value{ SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).setValue(value); } 函数getValue(cellName){ 返回SpreadsheetApp.getActiveSpreadsheet().getRange(cellName.getValue(); }
函数设置值(cellName,value{
SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).setValue(value);
}
函数getValue(cellName){
返回SpreadsheetApp.getActiveSpreadsheet().getRange(cellName.getValue();
}
函数getNextRow(){
返回SpreadsheetApp.getActiveSpreadsheet().getLastRow()+1;
}
函数addRecord(a、b、c、d){
var row=getNextRow();
设置值('A'+行,A);
设置值('B'+行,B);
设置值('C'+行,C);
设置值('D'+行,D);
}
函数punchIn(){
addRecord(getValue('A1')、new Date()、'START'、(“”);
}
函数punchOut(){
addRecord(getValue('A1')、new Date()、'STOP'、(“”);
}
在punchOut函数值D中,我想从之前的开始时间中减去停止时间。理论上,它应该是当前行中的单元格D减去前一行中的单元格D,而不是变得复杂&寻找前一行中同一天的开始时间。那么,只有当前行中的D减去前一行中的D
这是一张输出的图片,虽然在D列中有一个手动公式。谷歌应用程序脚本基于JavaScript,因此您可以使用最后一个脚本进行加法和减法等算术运算
- 使用
获取活动范围getActiveRange()
- 使用偏移量(…)获取上一行
value1
,value2
,只需添加如下代码行:
var result = value2 - value1;
参考文献