检查文本框是否具有相同的内容

时间:2012-11-07 20:46:14

标签: textbox google-apps-script iteration

我想检查文本框是否像这样创建:

(function(arr) {
    for (var i = 0; i < arr.length; i++) {
        app.add(app.createLabel(arr[i] + " mail"));
        app.add(app.createTextBox().setName(arr[i]));
    }
})(["first", "second", "third"]);

有相同的内容吗?我正在寻找像getElementByTagname或getTextboxes这样的东西,但是没有这样的功能。

那么如果它们全部相等,如何迭代它们并显示标签?

2 个答案:

答案 0 :(得分:0)

创建文本框时,使用setId(id)为每个文本框分配一个ID。 如果您想稍后获得参考,则可以使用getElementById(id)

以下是一个例子:

function doGet() {
  var app = UiApp.createApplication();
  app.add(app.createTextBox().setId("tb1").setText("the original text"));
  app.add(app.createButton().setText("Change textbox").addClickHandler(app.createServerHandler("myHandler")));
  return app;
}

function myHandler() {
  var app = UiApp.getActiveApplication();
  app.getElementById("tb1").setText("new text: in handler");
  return app;
}

答案 1 :(得分:0)

要访问任何窗口小部件值,您需要将它们作为回调元素(或父面板)添加到将处理它们的服务器处理程序。每个窗口小部件的值都填充在传递给处理函数的参数上,并且可以由窗口小部件name(您已设置)引用。

您不需要按照另一个答案的建议setId。除非你想对小部件本身做一些事情(而不是它的价值)。例如改变其文本或隐藏它等等。

var textBoxes = ["first", "second", "third"];

function example() {
  var app = UiApp.createApplication().setTitle('Test');
  var panel = app.createVerticalPanel();
  textBoxes.forEach(function(name){
    panel.add(app.createLabel(name + " mail"));
    panel.add(app.createTextBox().setName(name));
  });
  panel.add(app.createLabel('Example Label').setId('label').setVisible(false));

  var handler = app.createServerHandler('btnHandler').addCallbackElement(panel);
  panel.add(app.createButton('Click').addClickHandler(handler));

  SpreadsheetApp.getActive().show(app.add(panel));
}

function btnHandler(e) {
  var app = UiApp.getActiveApplication(),
      allEqual = true;
  for( var i = 1; i < textBoxes.length; ++i )
    if( e.parameter[textBoxes[i-1]] !== e.parameter[textBoxes[i]] ) {
      allEqual = false; break;
    }
  app.getElementById('label').setVisible(allEqual);
  return app;
}

请注意ServerHandlers不会立即运行,因此单击按钮后标签显示或隐藏可能需要几秒钟。