我为一家运输公司工作,我想要实现的是,每次驾驶员登录或通过双向无线电移动时,我都想点击空单元格并获得实时时间。我知道在Google工作表中有一个Now()函数,但如果我使用所有单元格将具有相同的值,我想在单击空单元格时获取当前时间。
我使用脚本编辑器创建了一个解决方法按钮,可以显示进出时间,但我想保留工作表的原始格式,下面是代码:
function setValue(cellName, value){
SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).setValue(value);
}
function getValue(cellName){
return SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).getValue();
}
function getNextRow(){
return SpreadsheetApp.getActiveSpreadsheet().getLastRow() + 1;
}
function setGeorge(){
setValue('M1','George,CSH2501')
}
function setTerry(){
setValue('M1','Terry,CSH2502')
}
function setDavid(){
setValue('M1','David,PSH3502')
}
function setSandra(){
setValue('M1','Sandra,PSH3508')
}
function setElioCalle(){
setValue('M1','ElioCalle,PSH3504')
}
function setWarren(){
setValue('M1','Warren,PSH3501')
}
function setShu(){
setValue('M1','Shu,PSH3503')
}
function setPaul(){
setValue('M1','Paul Prkash,PSH3507')
}
function setMei(){
setValue('M1','Mei Tsang,PSH3506')
}
function setJames(){
setValue('M1','James Clark,PSH3505')
}
function setElvira(){
setValue('M1','Elvira,PSH3509')
}
function addRecord(b, c, d){
var row = getNextRow();
setValue('A'+ row, b);
setValue('B'+ row, c);
setValue('C'+ row, d);
}
function punchIn(){
addRecord(getValue('M1'), new Date(), 'IN');
}
function punchOut(){
addRecord(getValue('M1'), new Date(), 'Out');
}
答案 0 :(得分:0)
= Now()是一个易失性函数,每次更改电子表格中的某些数据或刷新工作表时,Excel都会重新计算它。要克服的唯一方法是通过脚本,我使用绘图工具创建按钮,并在每次驱动程序移动和清除时分配函数punchIn和punchOut:
function punchIn(){
var sheet = SpreadsheetApp.getActiveSheet();
var now = new Date();
sheet.getRange('E3').setValue(now);
}
function punchOut(){
var sheet = SpreadsheetApp.getActiveSheet();
var now = new Date();
sheet.getRange('H3').setValue(now);
}
唯一的问题是我必须为每个单元格和每个按钮执行此操作,如下图所示。如果有办法简化代码,任何建议将不胜感激。