2013-01-24 40 views
0

我试图写一个文本框的通用服务器处理器,其突出的文本时,文本框获得焦点:如何访问GAS中的源代码小部件的值?

function onFocusHighlight(e) { 
    var app = UiApp.getActiveApplication(); 
    var widget = app.getElementById(e.parameter.source); 
    var widgetValue = e.parameter.widgetName; // how can I get widgetName from source??? 
    widget.setSelectionRange(0, widgetValue.length); 
    return app; 
} 

我能确定widgetValue从e.parameter.source?

回答

0

var widget = app.getElementById(e.parameter.source).setName("widgetName")

然后得到widgetValue:

var widgetValue = e.parameter.widgetName

检查https://developers.google.com/apps-script/uiappsetName使用。

+0

聪明,但这种方法实际上并不实际工作。看来我需要传入的实际名称。 – Eclipzer

0

我发现,只要widgetNamewidgetId都是一样的我能确定widgetValue如下:

function onFocusHighlight(e) { 
    var app = UiApp.getActiveApplication(); 
    var widgetId = e.parameter.source; 
    var widgetName = widgetId; // name MUST match id to retrieve widget value  
    var widgetValue = e.parameter[widgetName]; // not sure why this syntax works??? 

    var widget = app.getElementById(widgetId); 
    widget.setSelectionRange(0,widgetValue.length); 

    return app; 
} 

我唯一的问题是现在理解如何/为什么e.parameter[widgetName]语法的实际工作。拥有不依赖于widgetNamewidgetId的解决方案具有相同的价值也是非常好的。

0

只需按照你自己的答案建议

你也许可以把它由具有转换表的地方,从他们的ID获取部件名称工作。

例如,你可以定义:

ScriptProperties.setProperties({'widgetID1':'Name1','widgetID2':'name2'},true) 

然后

widgetvalue = e.parameter[ScriptProperties.getProperty(e.parameter.source)] 

我没有测试此代码,但它似乎是合乎逻辑;-),告诉我,如果它没有(我现在没有时间测试它)

编辑:按预期工作,这里是一个test sheet来显示结果,测试代码如下。

function Test(){ 
    var app = UiApp.createApplication().setTitle('test'); 
    app.add(app.createLabel('Type anything in upper textBox and then move your mouse over it...')) 
    var p = app.createVerticalPanel() 
    var txt1 = app.createTextBox().setId('txt1').setName('Name1').setValue('value in TextBox1') 
    var txt2 = app.createTextBox().setId('txt2').setName('Name2').setValue('waiting to mouseOver textBox1') 
    p.add(txt1).add(txt2) 
    app.add(p) 
    var handler = app.createServerHandler('mouseOver').addCallbackElement(p); 
    txt1.addMouseOverHandler(handler); 
    ScriptProperties.setProperties({'txt1':'Name1','txt2':'Name2'},true);//save the name, only txt1 will be used here 
    var ss=SpreadsheetApp.getActiveSpreadsheet() 
ss.show(app) 
} 

function mouseOver(e) { 
    var app = UiApp.getActiveApplication(); 
    var widget = app.getElementById(e.parameter.source); 
    var widgetValue = e.parameter[ScriptProperties.getProperty(e.parameter.source)]; // ScriptProperties knows the Widget's name 
    app.getElementById('txt2').setValue(widgetValue) ;// Use the other widget to show result 
    return app; 
}