首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onEdit只在粘贴(编辑)多行后更新顶部行

onEdit只在粘贴(编辑)多行后更新顶部行
EN

Web Applications用户
提问于 2014-01-15 17:30:51
回答 2查看 3.1K关注 0票数 3

我使用谷歌电子表格,并有一个脚本,更新“最后一封电子邮件”列的日期戳,当一个雇员输入一个电子邮件地址到前一栏。当我的员工一次粘贴多行电子邮件地址时,就会出现此问题。当他这样做时,脚本只在粘贴的顶部行放置日期标记。我目前使用的代码是:

代码语言:javascript
复制
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);}
    }
  }
}
}

是否有办法进行更正,以便当他同时粘贴多行时,所有行上的日期标记都会更新?

EN

回答 2

Web Applications用户

发布于 2016-04-27 14:30:56

可以编写onEdit以接受包含编辑范围的事件参数。将函数声明更改为接受参数“event”,并将r改为event.range而不是activecell,得到了我认为您想要的行为。

代码语言:javascript
复制
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);}
    }
  }
}
}
票数 4
EN

Web Applications用户

发布于 2014-01-15 18:41:55

通过Google脚本中的onEdit触发器是不可能的(我希望@TomHorwood证明我错了)。用于粘贴的第一个单元格仅用于触发onEdit,正如您自己描述的那样。

我可以想到以下两种可能有帮助的选择:

  1. 时基触发器
  2. 通过菜单选项手动触发

第一个选项将自动更新日期,无论您将其设置为什么范围。第二个选项将允许您一次粘贴多个值,然后触发选定的范围。

在这两种情况下,您都需要重写代码。如果你需要帮助,就再问一遍。

票数 1
EN
页面原文内容由Web Applications提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://webapps.stackexchange.com/questions/54527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档