简体   繁体   English

如果快速输入值,Google 电子表格脚本将不起作用

[英]Google Spreadsheets Script not Working if Values are Entered Quickly

I have written a following script for my Google Spreadsheet:我为我的 Google 电子表格编写了以下脚本:

function onEdit(e) 
{
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = doc.getActiveSheet();
  var cell = sheet.getActiveCell();
  var column = sheet.getActiveCell().getColumn();
  var row = sheet.getActiveCell().getRow();
  var dat = sheet.getActiveCell().getValue()+"";
  var curDate = Utilities.formatDate(new Date(), "GMT+5:30", "dd/MM/yyyy")
  var editor = doc.getViewers();
  var temp = e.oldValue;
  if(dat=="")
  {
    dat = "0.0"
  }
  if((column == 4 || column == 5) && temp != null)
  {
    cell.setComment("Value changed from ₹"+ (temp) + " to ₹" + dat + " on " + curDate);
  }

}

I use this code to insert a comment when a value is changed in the column D or E. I am using this script since a month and the code works perfect.当 D 或 E 列中的值发生更改时,我使用此代码插入注释。我使用此脚本一个月后,代码运行良好。 But I recently noted an issue with it.但我最近注意到它的一个问题。

  1. If I enter values quickly in several cells, for example, D2, D3, D4, D5, D6 etc., the code is not executed for all cells and comment is added to some random cells only.如果我在多个单元格中快速输入值,例如 D2、D3、D4、D5、D6 等,则不会对所有单元格执行代码,并且只会向某些随机单元格添加注释。 It is executed as expected only when I keep minimum interval of 1 second between entering records.只有当我在输入记录之间保持 1 秒的最小间隔时,它才会按预期执行。
  2. If I delete values from multiple cells at once, the code is not executed for any cell.如果我一次从多个单元格中删除值,则不会为任何单元格执行代码。

Thanks for reading the code.感谢您阅读代码。

Regarding the first issue, I was able to reproduce the problem using the following script:关于第一个问题,我能够使用以下脚本重现该问题:

function onEdit(e){
  var destination = e.range.offset(0,1);
  if(e.value){ // Do this when editing a range having only one cell
    destination.setValue(e.value);
  } else { // Do this when editing a range having multiple cells
    destination.setValues(e.range.getValues());
  }
}

This should be reported to Google by using the Issue Tracker .这应该使用问题跟踪器报告给 Google。

Regarding the second issue, getActiveCell returns only the active cell and it looks that the entire range is required.关于第二个问题,getActiveCell 只返回活动单元格,看起来整个范围都是必需的。 It's worth to note that e.oldValue returns null when a range with more than one cell is being edited.值得注意的是,当编辑具有多个单元格的区域时,e.oldValue 会返回 null。

The following is an alternative version of your script that works both for single cell editing as multiple cell editing at once:以下是脚本的替代版本,可同时用于单个单元格编辑和多个单元格编辑:

function onEdit(e){
  var column = e.range.getColumn();
  if(column == 4 || column == 5){
    if(e.range.getNumRows() == 1 && e.oldValue != undefined){
      e.range.setNote("Old value: " + ((e.oldValue)?e.oldValue:"0.0") + "| New value: " + ((e.value)?e.value:"0.0"));
    } 
    if(e.range.getNumRows() > 1){
      var values = e.range.getValues();
      for(var i = 1;i<=e.range.getNumRows();i++){
        e.range.getCell(i,1).setNote("Old value: Not available | New value: " +  ((values[i-1][0])?(values[i-1][0]):"0.0"));
      }
    }
  }
}

Note: setComment is not included on the official Google Apps Script reference for the Spreadsheet Service.注意:电子表格服务的官方 Google Apps 脚本参考中不包含 setComment。

Related Issues相关问题

i get this from GAS Guide about triggers may helps you我从 GAS 指南中得到这个关于触发器的信息可能对你有帮助

add a comment to edited range with this way :用这种方式向编辑的范围添加评论:

function onEdit(e){
  // Set a comment on the edited cell to indicate when it was changed.
  var range = e.range;
  range.setNote('Last modified: ' + new Date());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM