2013-06-12 74 views
0

我会尽量保持简短。我试图在谷歌电子表格中制​​作一个谷歌网络应用程序,它将允许我输入最小值和最大值。如何将输入值传递给使用GAS的函数

我已经能够创建GUI并将其添加到面板。但我似乎无法将正在输入的整数传递到另一个函数中。我已经尝试了所有的东西,我对创建Google脚本相对来说比较陌生,所以我很抱歉,如果这看起来有些不好的问题。

这是迄今为止所有代码:

function onOpen() { 
    var ss = SpreadsheetApp.getActive(); 
    var menuEntries = []; 
    menuEntries.push({name: "Open Dialog", functionName: "showDialog"}); 
    ss.addMenu("Min/Max", menuEntries); 
} 


//creating a panel to add the min and max of low to high for scoring 
function showDialog() { 
    max = 10; 
var app = UiApp.createApplication(); 
app.setTitle("My Applicaition"); 
    var panel = app.createVerticalPanel(); 
    var textBox = app.createTextBox(); 
    var label = app.createLabel("Set the min value for 'Low'"); 
    //had to create a hidden element with id="min" for a global value that can be updated 
    var min = app.createHidden().setValue('0').setName('min').setId('min'); 
    textBox.setName('myTextBox').setId('myTextBox'); 
    var button = app.createButton('Submit'); 
    panel.add(label); 
    panel.add(textBox); 
    panel.add(min); 
    panel.add(button); 


    //click handler for setting the value of min to the new value 
    var clickHandler = app.createServerClickHandler("responedToSubmit"); 
    button.addClickHandler(clickHandler); 
    clickHandler.addCallbackElement(panel); 
    app.add(panel); 

    var doc = SpreadsheetApp.getActive(); 
    doc.show(app); 
} 

function responedToSubmit(e) { 
    var app = UiApp.getActiveApplication(); 
    var textBoxValue = e.parameter.myTextBox; 
    Logger.log(e.parameter.min); 
    if (typeof textBoxValue != "number") { 
    var num = parseInt(textBoxValue); 
    app.getElementById('min').setValue(num); 
    Logger.log("textBoxValue is = "+textBoxValue+"\n min value is = "+e.parameter.min); 
    } else { 
    throw "value needs to be set as number"; 
    } 
    return app.close(); 
} 

这是我相信事情正在按计划不会:

function responedToSubmit(e) { 
    var app = UiApp.getActiveApplication(); 
    var textBoxValue = e.parameter.myTextBox; 
    Logger.log(e.parameter.min); 
    if (typeof textBoxValue != "number") { 
    var num = parseInt(textBoxValue); 
    app.getElementById('min').setValue(num); 
    Logger.log("textBoxValue is = "+textBoxValue+"\n min value is = "+e.parameter.min); 
    } else { 
    throw "value needs to be set as number"; 
    } 
    return app.close(); 
} 

我发现,每次我测试.setValue ()不会更新'min'的值,我不明白为什么。你能帮忙吗?

回答

0

您需要将textBox元素添加到clickHandler的callBack元素列表中。 试试这个:

//click handler for setting the value of min to the new value 
var clickHandler = app.createServerClickHandler("responedToSubmit"); 
clickHandler.addCallbackElement(panel); 
clickHandler.addCallbackElement(textBox); 
button.addClickHandler(clickHandler); 
app.add(panel); 
+0

好,我又增加了文本框面板的名称myTextBox和被称为回用 e.parameter.myTextBox目前 日志看起来是这样的: [13 -06-13 11:07:26:032 BST] textBoxValue是= 77 最小值是= 0 有一些关于setValue(),这是不适合我... – user2096568

+0

更新刚刚解决,如果我我们addCallBackElement与min然后我可以更新min的值:e.parameter.min = textBoxValue; 感谢您的帮助br araujo :) – user2096568

+0

编辑:我有另一个问题,如何可能更新另一个变量在单独的frongs respondToSubmit?我想知道,因为我想在我的电子表格的公式中使用更新后的变量。 – user2096568

相关问题