如何使用GAS将输入值传递给函数

时间:2013-06-12 15:18:59

标签: user-interface google-apps-script google-sheets

我会尽量保持这个简短。我试图在谷歌电子表格中制​​作一个谷歌网络应用程序,允许我输入最小值和最大值。

我已经能够创建GUI并将其添加到面板中。但我似乎无法将输入的整数传递给另一个函数。我已经尝试了所有的东西,我对创建Google Script相对较新,所以如果这有点像是一个无聊的问题,我很抱歉。

到目前为止,以下是所有代码:

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'的值,我看不出原因。你能帮忙吗?

1 个答案:

答案 0 :(得分: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);