Google Apps Scrollpanel未随信息更新

时间:2013-02-18 08:18:32

标签: google-apps-script

我的脚本正在迭代一些电子表格信息。我阅读了文档,滚动面板只允许一个孩子。因此,我将水平面板信息包装在滚动面板中,但由于某种原因,滚动面板从不显示内容只是彩色背景。任何想法为什么会这样?

 var myscrollpanel = app.createScrollPanel().setPixelSize(100, 100);
  myscrollpanel.setWidth("100%");
  myscrollpanel.setStyleAttribute("background", "silver");
  var vpanel = app.createVerticalPanel();


 for (i=1; i <= mylastrow;++i)
{

 var cellsname= mydatarange.getCell(i,1).getValue().toLowerCase();
// Browser.msgBox(cellsname );
// Browser.msgBox(searchstr.toString());
 if (cellsname.toString() == searchstr.toString()) 
 {

   var panelrow = app.createHorizontalPanel();
   var ddate = app.createTextBox();
   var sbehavior = app.createTextBox();
   var sconsequence = app.createTextBox();
   var scomment = app.createLabel();
   var steacher = app.createTextBox();
   ddate.setText(mydatarange.getCell(i,5).getValue());
   sbehavior.setText(mydatarange.getCell(i,8).getValue());
   sconsequence.setText(mydatarange.getCell(i,6).getValue());
   scomment.setText(mydatarange.getCell(i,10).getValue());
   steacher.setText(mydatarange.getCell(i,7).getValue());
   panelrow.add(ddate);
   panelrow.add(sbehavior);
   panelrow.add(sconsequence);
   panelrow.add(steacher);
   panelrow.add(scomment);
   vpanel.add(panelrow);
   app.add(panelrow);


   cnt = ++cnt;

 }

 }  

//  Browser.msgBox(cnt);     

 myscrollpanel.add(vpanel);
app.add(myscrollpanel);
// return myscrollpanel;

return app    ;// update UI

1 个答案:

答案 0 :(得分:1)

有一些&#34;异常&#34;在您的代码中可能会导致一些问题,我建议进行一些更改(请参阅下面的代码)。

另一点是,在循环中读取电子表格不是一个好主意,一次读取整个范围然后使用数组值进行迭代会更加高效。 这是一个代码提案,我没有测试它(它可能需要一些调试),但提出一些注释来解释这些更改。

  var myscrollpanel = app.createScrollPanel()//.setPixelSize(100, 100);// 100 pixel is quite small to put all the items you add !!
  myscrollpanel.setWidth("100%");// 
  myscrollpanel.setStyleAttribute("background", "silver");// this will not affect the widgets you add to the panel...
  var vpanel = app.createVerticalPanel();

  var data = mydatarange.getValues();// get all data in an array

 for (i=0; i <data.length;++i)  // and work directly with array values (indexed to 0 instead of cells indexed to 1)
{

 var cellsname= data[i][0].toString().toLowerCase();
 if (cellsname == searchstr.toString()){
   var panelrow = app.createHorizontalPanel();
   var ddate = app.createTextBox();
   var sbehavior = app.createTextBox();
   var sconsequence = app.createTextBox();
   var scomment = app.createLabel();
   var steacher = app.createTextBox();
   ddate.setText(data[i][4].getValue());
   sbehavior.setText(data[i][7].getValue());
   sconsequence.setText(data[i][5].getValue());
   scomment.setText(data[i][9].getValue());
   steacher.setText(data[i][6].getValue());
   panelrow.add(ddate);
   panelrow.add(sbehavior);
   panelrow.add(sconsequence);
   panelrow.add(steacher);
   panelrow.add(scomment);
   vpanel.add(panelrow);// add only to the panel, not to the app or the row will be outside the panel
   cnt = ++cnt;
   }
 }  
  myscrollpanel.add(vpanel);
  app.add(myscrollpanel);
  return app    ;// update UI
}