简体   繁体   English

如何在Google Apps脚本中使用CheckBox小部件?

[英]How to use checkBox widget in Google apps script?

I am trying to figure out how to create a function that turns the color of the "productInfoButton" lime green when checked and grey when unchecked? 我试图弄清楚如何创建一个函数,该函数在选中时将“ productInfoButton”的颜色变成绿色,而在未选中时变成灰色? Could someone please help? 有人可以帮忙吗?

function doGet(e) {
  var app = UiApp.createApplication();

  var productInfoButton = app.createButton("Products").setId('productInfoButton');

  var handlerL = app.createServerClickHandler('prodCompleteHandlerL');
  var productCompleteCheckBox = app.createCheckBox().setId("productCompleteCheckBox")
  .setName("productCompleteCheckBox");
  productCompleteCheckBox.addClickHandler(handlerL);
  handlerL.addCallbackElement(productInfoButton);

  app.add(productInfoButton);
  app.add(productCompleteCheckBox);

  return app;
}

function prodCompleteHandlerL(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById('productInfoButton').setStyleAttribute('background', 'lime')
  return app;
}

The state of the checkBox is available in the event info parameter using the name of the widget. 使用小部件名称的事件信息参数中提供了checkBox的状态。 In your code it would be : 在您的代码中它将是:

function prodCompleteHandlerL(e) {
  var app = UiApp.getActiveApplication();
if(e.parameter.productCompleteCheckBox == 'true'){
  app.getElementById('productInfoButton').setStyleAttribute('background', 'lime')
}else{
  app.getElementById('productInfoButton').setStyleAttribute('background', '#dddddd') // is light grey
}
  return app;
}

EDIT : I didn't notice another problem (sorry about that) : the CallbackElement should include the productCompleteCheckBox .... so you should use productCompleteCheckBox as callBackElement in the main function (or use the parent widget that holds this wjidget (if any)) 编辑:我没有注意到另一个问题(对此感到抱歉): CallbackElement应该包括productCompleteCheckBox ....因此,您应该在主函数productCompleteCheckBox用作callBackElement (或者使用保存此wjidget的父窗口小部件(如果有)) )

EDIT2 : following your comment : You can find documentation on colors and styles by looking for CSS documentation since it is what is used here... or you can have a look at the options available in the GUI builder to get inspiration and to see what happens in a WYSWYG environment. EDIT2跟随您的评论:您可以通过查找CSS文档来找到有关颜色和样式的文档,因为此处使用的是它...或者您可以查看GUI生成器中的可用选项,以获取灵感并查看内容。发生在所见即所得的环境中。 Everything that can be done with the GUI builder can of course be done using script (in fact script is even more powerful but it's nice to have an idea). GUI生成器可以完成的所有操作当然都可以使用脚本来完成(实际上,脚本功能更强大,但是有一个想法很不错)。

If you want to get a widget transparent simply use .setStyleAttribute('background','transparent') 如果您想使小部件透明,只需使用.setStyleAttribute('background','transparent')

EDIT 3 : getting the 'default look' for buttons: 编辑3:获得按钮的“默认外观”:

Examinig the code from a page I noticed that it uses a .png file in a cache... since I couldn't find a way to use that I copied the file, put it on a public folder, get the url and used it in the style... quite a surprise : it's working. 检查页面中的代码,我发现它在缓存中使用了.png文件...因为我找不到使用该文件的方法,所以我将文件复制,放置到公共文件夹中,获取了网址并使用了它风格……很令人惊讶:它正在工作。 (the url found in the page seems to be specific to the script, I don't know if I could use it in another script) (页面中找到的url似乎是特定于脚本的,我不知道是否可以在其他脚本中使用它)

Maybe someone smarter than me will find a more easy way to achieve the same goal, feel free to comment and/or suggest ;-) 也许比我聪明的人会找到一种更简单的方法来实现相同的目标,随时发表评论和/或提出建议;-)

Also : I copied all the parameters, some of them are useless in this use case, I know ^^ 另外:我复制了所有参数,其中一些在此用例中无用,我知道^^

  var btn = app.getElementById('btn').setStyleAttributes({ 
  'margin': '5px',
  'padding': '3px 5px',
  'text-decoration': 'none',
  'font-size': 'small',
  'cursor': 'pointer',
  'cursor': 'hand',
  'background': 'url("https://dl.dropbox.com/u/211279/hborder.png") repeat-x 0px -27px',
  'border': '1px outset #ccc'
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM