Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Google脚本编辑器中将公式添加到函数_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

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;
参考文献


感谢您的反馈。我不知道如何创建一个新函数(?)并在停止后将其添加到punchOut 4段/列?@LevGrodzinskiy请编辑您的问题,以澄清您需要的内容。感谢您寻求澄清,我将关闭此函数,因为我可能已经找到了替代方法。