【问题标题】:How do I make an apps script that will automatically input a timestamp when a cell is updated to certain criteria?如何制作一个应用程序脚本,当单元格更新为特定条件时自动输入时间戳?
【发布时间】:2021-03-24 19:23:05
【问题描述】:
【问题讨论】:
标签:
google-apps-script
google-sheets
【解决方案1】:
您可以使用Triggers。
触发器让 Apps 脚本在某个特定的情况下自动运行一个函数
发生事件,例如编辑单元格。
试试这个:
function onEdit(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
if(row > 1 && col == 4){
var selected = e.value;
var date = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy-HH:mm:ss");
var sh = e.range.getSheet();
var colEdit = 0;
if(selected == "Started"){
colEdit = parseFloat(col+2);
}else{
colEdit = parseFloat(col+3);
}
var editedRange = sh.getRange(row,parseInt(colEdit),1,1);
editedRange.setValue(date);
//Optional: Uncomment the code below if you want to add protection to the cell
//This will make the cell uneditable to the other users.
//editedRange.protect();
}
}
注意:由于您是电子表格的所有者,因此您不能将单元格设为不可编辑。但您可以添加Protection 使其他人无法编辑。
输出: