如何在Google Apps脚本中从网格类中获取值?

时间:2013-08-01 04:02:41

标签: google-apps-script

我正在创建一个带有网格对象的UI实例。我在里面创建了一些项目,例如5个文本框。我通过回调元素传递网格,但我不知道如何使用e.parameter调用值。有什么理由不行吗?

      function first()
        {
         var appheight = 360;
           var appwidth = 500;
            var defcolumns = 8;
           //Get the spreadsheet we are working on and Create a UI
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var app = UiApp.createApplication().setWidth(appwidth).setHeight(appheight).setTitle('Step 1:  Set Information');
        var mainGrid = app.createGrid(5, 1).setId("mainGrid"); 
        for(var r=0;r < 5;r++)
        {
         var columnname = app.createTextBox().setText(r.toString());
        mainGrid.setWidget(r, 0, columnname);
         }

      var steptwohandler =   app.createServerClickHandler("steptwo").addCallbackElement(mainGrid);
    var nextbutton = app.createButton().setId("btnnext").setText("Next ->").addClickHandler(steptwohandler);


}

function steptwo()
{
 var app =UiApp.getActiveApplication();
   //See how many rows exist already
    Logger.log(e.parameter.mainGrid);
  for(var r=0;r <=5; r++)
  {
    //should log 1-5 and the mainGrid object;
    Logger.log(r);
    Logger.log(e.parameter.mainGrid[r]);
  }

}

我已经看到了在doctopus脚本中做类似事情的能力,但无法弄清楚他是如何解决它的。

1 个答案:

答案 0 :(得分:0)

您还必须在每个文本框中设置名称。请参阅下面的修改代码

 function first()
        {
         var appheight = 360;
           var appwidth = 500;
            var defcolumns = 8;
           //Get the spreadsheet we are working on and Create a UI
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var app = UiApp.createApplication().setWidth(appwidth).setHeight(appheight).setTitle('Step 1:  Set Information');
        var mainGrid = app.createGrid(5, 1).setId("mainGrid"); 
        for(var r=0;r < 5;r++)
        {
         /* Set a name for each text box */
         var columnname = app.createTextBox().setText(r.toString())
            .setName('columnname' + r.toString());
        mainGrid.setWidget(r, 0, columnname);
         }

      var steptwohandler =   app.createServerClickHandler("steptwo").addCallbackElement(mainGrid);
    var nextbutton = app.createButton().setId("btnnext").setText("Next ->").addClickHandler(steptwohandler);


}

/* Receive e as a parameter */
function steptwo(e)
{
 var app =UiApp.getActiveApplication();
   //See how many rows exist already
    Logger.log(e.parameter.mainGrid);
  for(var r=0;r <=5; r++)
  {
    //should log 1-5 and the mainGrid object;
    Logger.log(r);
    //Logger.log(e.parameter.mainGrid[r]);
    Logger.log(e.parameter['columnname' + r.toString()]);
  }

}