我有一个谷歌电子表格,它使用谷歌应用程序脚本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 );
}
}
感谢。
答案 0 :(得分:1)
答案 1 :(得分:0)