谷歌应用程序脚本TextBox值未传递给e.parameter.TextBoxName

Luc*_*ano 2 google-apps-script

在下面的代码中,我定义了一个名称和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;
}
Run Code Online (Sandbox Code Playgroud)

meg*_*024 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;
}
Run Code Online (Sandbox Code Playgroud)