[英]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.
但我最近注意到它的一个问题。
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.