从Google脚本中的flextable中选择一行

时间:2014-09-12 23:43:35

标签: google-apps-script

我是谷歌脚本的新手。我有一个数据源,我需要在表中显示,并允许用户选择一行并选择打开按钮。打开按钮的事件处理程序应该能够找到行号,以便我能够发送一个名为user的请求。

我想添加一个复选框,并在click事件上,只需读取具有行号的复选框的名称。

请让我知道如何做到这一点。谢谢

   var panel = app.createVerticalPanel();
   var scroll = app.createScrollPanel().setPixelSize(500, 300);
   scroll.add(panel);

   var flexTable = app.createFlexTable().setStyleAttribute('border', '1px solid black')
      .setStyleAttribute('borderCollapse','collapse')
      .setBorderWidth(1); 
   flexTable.setText(0, 1, 'Application Name')
   flexTable.setRowStyleAttribute(0,'color', 'blue')
   flexTable.setText(0, 2, 'Owner')
   flexTable.setText(0, 3, 'Date')

   var handler = app.createServerClickHandler('clickTable');
   var counter = 1;
   for (var i in queryResult.records) {
      var chkBox = app.createCheckBox().setName('chk' + counter).setId('chk');

      var owner  = queryResult.records[i]["Owner"]["Name"];
      var AppName = queryResult.records[i]["Name"];
      var ModifiedDate =  queryResult.records[i]["LastModifiedDate"]
      var UniqId =  queryResult.records[i]["Apttus_XApps__UniqueId__c"]
      flexTable.setWidget(counter, 0 , chkBox)
      flexTable.setText(counter, 1, AppName.toString());
      flexTable.setText(counter, 2, owner.toString());
      flexTable.setText(counter, 3, ModifiedDate.toString());
      counter ++; 
    }

  panel.add(app.createButton('Open', handler));
  panel.add(flexTable);
  app.add(scroll);

2 个答案:

答案 0 :(得分:0)

您只创建了服务器处理程序,但没有附加回调元素。请查看此page

所以你的代码就像。

var handler = app.createServerClickHandler('clickTable');
handler.addCallbackElement(panel)

下一步:在您的服务器功能clickTable

function clickTable(e)
{
for (var i in e.parameter) {
   Logger.log('parameter ' + i+1 + ' is ' + e.parameter[i]);
  } 

//尝试记录传递的所有值并相应地使用它,     }

答案 1 :(得分:0)

以下是我在上面的评论中使用radioButtons建议的实现示例。

如果选择了一个选项,用户只能提交

test online here

代码:

function doGet(){
  var app = UiApp.createApplication();
  var panel = app.createVerticalPanel().setStyleAttributes({'padding':'15px'});
  var handler = app.createServerClickHandler('clickTable').addCallbackElement(panel);
  var btn = app.createButton('Open', handler).setEnabled(false);
  panel.add(btn);
  var scroll = app.createScrollPanel().setPixelSize(500, 400);
  scroll.add(panel);
  var flexTable = app.createFlexTable().setStyleAttribute('border', '1px solid black')
  .setStyleAttributes({'borderCollapse':'collapse'})
  .setBorderWidth(1).setCellPadding(2); 
  flexTable.setText(0, 1, 'Application Name')
  flexTable.setRowStyleAttribute(0,'color', 'blue')
  flexTable.setText(0, 2, 'Owner')
  flexTable.setText(0, 3, 'Date')
  var radioValue = app.createTextBox().setName('radioValue').setVisible(false);// set it to visible if you want to see what happens in tests
  panel.add(radioValue);  
  var counter = 1;
  for (var i =0;i<10;i++) {
    var name = counter;
    var radio = app.createRadioButton('radioBtn','select');
    var CliHandler = app.createClientHandler().forTargets(radioValue).setText(name).forTargets(btn).setEnabled(true);
    radio.addClickHandler(CliHandler);
//    var owner  = queryResult.records[i]["Owner"]["Name"];
//    var AppName = queryResult.records[i]["Name"];
//    var ModifiedDate =  queryResult.records[i]["LastModifiedDate"]
//    var UniqId =  queryResult.records[i]["Apttus_XApps__UniqueId__c"]
//  temp var definitions for test
var owner  = 'test';
var AppName = 'test';
var ModifiedDate =  'test';
var UniqId =  'test';
// delete in production code
    flexTable.setWidget(counter, 0 , radio)
    flexTable.setText(counter, 1, AppName.toString());
    flexTable.setText(counter, 2, owner.toString());
    flexTable.setText(counter, 3, ModifiedDate.toString());
    counter ++; 
  }

  panel.add(flexTable);
  app.add(scroll);
  return app;
}

function clickTable(e){
  var app = UiApp.getActiveApplication();
  app.add(app.createLabel('you have selected item nr '+e.parameter.radioValue));
  return app
}