我使用谷歌电子表格,并有一个脚本,更新“最后一封电子邮件”列的日期戳,当一个雇员输入一个电子邮件地址到前一栏。当我的员工一次粘贴多行电子邮件地址时,就会出现此问题。当他这样做时,脚本只在粘贴的顶部行放置日期标记。我目前使用的代码是:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Master Sheet" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 10 ) { //checks the column
if( r.getValue() === '' ) {//don't update the timestamp if the cell is edited to a blank vaue
}
else
{
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) {// only update the timestamp if it has not already been updated
//Format the current date into datetime format (adjust to display how you want)
var dateTime = Utilities.formatDate(new Date(), "EST-8", "MM-dd-yyyy");
// Set the cell
nextCell.setValue(dateTime);}
}
}
}
}是否有办法进行更正,以便当他同时粘贴多行时,所有行上的日期标记都会更新?
发布于 2016-04-27 14:30:56
可以编写onEdit以接受包含编辑范围的事件参数。将函数声明更改为接受参数“event”,并将r改为event.range而不是activecell,得到了我认为您想要的行为。
function onEdit(event) {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Master Sheet"" ) { //checks that we're on the correct sheet
var r = event.range;
if( r.getColumn() == 10 ) { //checks the column
if( r.getValue() === '' ) {//don't update the timestamp if the cell is edited to a blank vaue
}
else
{
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === '' ) {// only update the timestamp if it has not already been updated
//Format the current date into datetime format (adjust to display how you want)
var dateTime = Utilities.formatDate(new Date(), "EST-8", "MM-dd-yyyy");
// Set the cell
nextCell.setValue(dateTime);}
}
}
}
}发布于 2014-01-15 18:41:55
通过Google脚本中的onEdit触发器是不可能的(我希望@TomHorwood证明我错了)。用于粘贴的第一个单元格仅用于触发onEdit,正如您自己描述的那样。
我可以想到以下两种可能有帮助的选择:
第一个选项将自动更新日期,无论您将其设置为什么范围。第二个选项将允许您一次粘贴多个值,然后触发选定的范围。
在这两种情况下,您都需要重写代码。如果你需要帮助,就再问一遍。
https://webapps.stackexchange.com/questions/54527
复制相似问题