2

在下面的代码中,我定义了一个带有名称和 id 的 TextBox。按钮处理程序工作正常,但我无法获得在 TextBox 上输入的值。msgBox 显示,但e.parameter.matchValue显示为undefined.

在应用程序的其他部分,我有相同的逻辑,但使用 ListBox 并且工作正常。

我究竟做错了什么?

function chooseColumnValueToMatch() {
  var app = UiApp.createApplication().setHeight(150).setWidth(250);
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var columnName = ScriptProperties.getProperty("columnNameToMatch");

  var h1Line = app.createHTML("<h2>Value to match "+columnName+":</h2>");
  var textPara = app.createHTML("<p>Type the VALUE that the <b>"+columnName+"</b> column must match EXACTLY to send the message.</p>");
  var selectionHandler = app.createServerHandler('chooseColumnValueToMatchSelectionHandler');
  var valueInputBox = app.createTextBox()
      .setTitle("Match value for "+columnName)
      .setName("matchValue")
      .setId("matchValue");
  var submitBtn = app.createButton("Submit", selectionHandler);

  app.add(h1Line)
    .add(textPara)
    .add(valueInputBox)
    .add(submitBtn);
  ss.show(app);
}
function chooseColumnValueToMatchSelectionHandler(e){
  var app = UiApp.getActiveApplication();
  var columnName = ScriptProperties.getProperty("columnNameToMatch");

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var msg = e.parameter.matchValue;

  Browser.msgBox("Value to match "+columnName+" column is:", msg, Browser.Buttons.OK);

  return app;
}
4

2 回答 2

6

您必须使用ServerHandler.addCallbackElement方法。下面的代码演示了它。方法调用“告诉”GAS 内部,作为参数指向的小部件的值应该传递给处理程序。

如果您有多个小部件,应由处理程序处理,则addCallbackElement可以将所有控件放置到面板并仅将面板作为addCallbackElement方法参数,而不是调用每个小部件。本教程第 4.4 点中的代码显示了这种方式。

function doGet(e) {
  var app = UiApp.createApplication();
  var valueInputBox = app.createTextBox()
      .setTitle("Match value for XXX")
      .setName("matchValue")
      .setId("matchValue");
  var selectionHandler = app.createServerHandler('chooseColumnValueToMatchSelectionHandler');
  selectionHandler.addCallbackElement(valueInputBox);
  var submitBtn = app.createButton("Submit", selectionHandler);
  var output = app.createLabel().setId("output"); 
  app.add(valueInputBox).add(submitBtn).add(output);  
  return app;
}

function chooseColumnValueToMatchSelectionHandler(e){
  var app = UiApp.getActiveApplication();
  var output = app.getElementById("output");

  var msg = e.parameter.matchValue;

  output.setText("Value to match XXXX column is: " + msg);

  return app;
}
于 2012-10-11T05:46:14.047 回答
2

您似乎忘记在处理程序中添加一个 callbackElement

你可以这样尝试:

selectionHandler.addCallbackElement(valueInputBox)

在 submitBtn 定义之后

于 2012-10-11T05:22:55.127 回答