我有一个谷歌电子表格,它使用谷歌应用脚本 onEdit() 事件在输入值时提供单位转换。这在上周还有效,但似乎不再有效。除了更改 Range setComment() API 以支持 setNote() 之外,我看不出这个脚本不再工作的原因。有趣的是,执行日志似乎暗示它应该工作,但单元格没有更新:
[13-06-26 15:29:57:973 BST] Starting execution
[13-06-26 15:29:57:976 BST] Spreadsheet.getActiveSheet() [0 seconds]
[13-06-26 15:29:57:977 BST] Range.getWidth() [0 seconds]
[13-06-26 15:29:57:978 BST] Range.getHeight() [0 seconds]
[13-06-26 15:29:58:151 BST] Range.getValue() [0.173 seconds]
[13-06-26 15:29:58:151 BST] Range.setValue([13.670166224]) [0 seconds]
[13-06-26 15:29:58:151 BST] Range.setNote([Distance Conversion: 22km => 13.670166224]) [0 seconds]
[13-06-26 15:29:58:152 BST] Execution succeeded [0.176 seconds total runtime]
这是代码。感谢有关更改内容的任何提示!
function Milify( x )
{
function convert( str, p1, offset, s )
{
// p1 is the matched number
// 1 kilometre = 0.6a21371192 miles
return Number(p1) * 0.621371192;
}
var s = String( x );
var test = /(\d+(?:\.\d*)?)km/;
return s.replace( test, convert );
}
function onEdit( event )
{
var ss = event.source.getActiveSheet();
//var er = event.source.getActiveRange();
var er = event.range;
//er.setNote( "" );
//er.setNote( er.getNote() + "\n" + "Trigger Fired for " + er.getA1Notation() + " with " + er.getValue() + " " + er.getWidth() + "x" + er.getHeight() );
if ( ( er.getWidth() * er.getHeight() ) === 1 )
{
var valueOriginal = er.getValue();
var valueInMiles = Milify( valueOriginal, er );
er.setValue( valueInMiles );
er.setNote( "Distance Conversion: " + valueOriginal + " => " + valueInMiles );
}
}
谢谢。