在Google Script中将文本框更改为按钮

时间:2014-10-03 17:11:44

标签: google-apps-script

我正在制作谷歌脚本中的待办事项列表程序。我拥有它,以便人们可以在文本框中键入他们的任务,但我想要它,一旦他们按下回车键,文本框变成一个按钮,说明任务是什么,他们将在以后按下它们完成该任务后,它将返回文本框。我无法弄清楚如何从另一个函数内部对应用程序进行任何更改。

到目前为止我所拥有的是:

function doGet(e) {
  var app = UiApp.createApplication();
  var panel = app.createAbsolutePanel().setHeight("750px").setWidth("1600px");
  var button = app.createButton("swag");
  var enterHandler = app.createServerKeyHandler('enterPress');
  enterHandler.addCallbackElement(panel);

  var box = app.createTextBox().addKeyUpHandler(enterHandler);

  panel.add(box,0,0);
  app.add(panel);
  return app;
}

function enterPress(e){
  var app = UiApp.getActiveApplication();
  if (e.parameter.keyCode==13){
    var button2 =app.createButton('swag');
    var panel = app.createAbsolutePanel().setHeight('750px').setWidth('1600px');
    panel.add(button2,0,0);
    app.add(panel);
  }
  return app;
}

它识别输入按下,但更改不会返回主应用程序。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

注意:我没有“放置”按钮,我想您对如何让它们显示出来有所了解......

您也可以根据需要使用网格放置它们。

function doGet(e) {
  var app = UiApp.createApplication();
  var panel = app.createAbsolutePanel().setHeight("750px").setWidth("1600px");
  var enterHandler = app.createServerKeyHandler('enterPress').addCallbackElement(panel);
  var button = app.createButton("confirm",enterHandler).setId('swag');
  var tBox = app.createTextBox().setName('tBox').setId('tBox').setWidth(300).setStyleAttributes({'textAlign':'center'});
  var bBox = app.createButton("no Text",enterHandler).setVisible(false).setId('bBox').setWidth(300);
  panel.add(tBox,20,20).add(bBox,20,20).add(button,140,50);
  app.add(panel);
  return app;
}

function enterPress(e){
  var app = UiApp.getActiveApplication();
  if (e.parameter.source=='bBox'){
    app.getElementById('bBox').setVisible(false);
    app.getElementById('tBox').setVisible(true);
  }
  if (e.parameter.source=='swag'){
    app.getElementById('bBox').setVisible(true).setHTML(e.parameter.tBox);
    app.getElementById('tBox').setVisible(false);
  }
  return app;
}

Test here

修改:使用展示位置修改的代码。

EDIT2:你也可以隐藏输入按钮,我发现它更好。

function enterPress(e){
  var app = UiApp.getActiveApplication();
  if (e.parameter.source=='bBox'){
    app.getElementById('bBox').setVisible(false);
    app.getElementById('tBox').setVisible(true);
    app.getElementById('swag').setVisible(true);
  }
  if (e.parameter.source=='swag'){
    app.getElementById('bBox').setVisible(true).setHTML(e.parameter.tBox);
    app.getElementById('tBox').setVisible(false);
    app.getElementById('swag').setVisible(false);
  }
  return app;
}