0

我的代码应该每次循环并获得一个新行。它做的一切都是正确的,只是它一直走到第一行。它显然循环正确并且计数在一段时间内工作,但是当我尝试递增时nn,它没有这样做,所以它不是在它刚刚离开的单元格之后开始,而是回到它开始的地方。任何帮助将不胜感激,谢谢。

function doGet() { 
var app = UiApp.createApplication().setTitle('When Am I Eligible?');
//var panel = app.createVerticalPanel()/*.setHeight("400px;")*/.setWidth("800px");
//var submitButton = app.createButton('Check');


var key= ''
var ss = SpreadsheetApp.openById(key);
  var sh = ss.getSheetByName('SHEET');
   var last=ss.getLastRow();
  var data=sh.getRange(1,1,last,5).getValues();
  var valB= Session.getEffectiveUser();
 var nn = 0;

   for (cnt=0; cnt<8; ++cnt){
  for(nn;nn<data.length;++nn){
    if (data[nn][1]==valB){
      var final = data[nn][0];
      var final1 = data[nn][1];
      var final2 = data[nn][2];
      var final3 = data[nn][3];
      var final4 = data[nn][4];


      break} ;// if a match in column B is found, break the loop
      }


 var app = UiApp.getActiveApplication();
 var panel = app.createVerticalPanel()/*.setHeight("400px;")*/.setWidth("800px");
 var answer = app.createLabel("BRO").setVisible(true).setText(final)
 var answer1 = app.createLabel("BRO").setVisible(true).setText(final1)
 var answer2 = app.createLabel("BRO").setVisible(true).setText(final2)
 var answer3 = app.createLabel("BRO").setVisible(true).setText(final3)
 var answer4 = app.createLabel("BRO").setVisible(true).setText(final4)
 panel.add(answer).add(answer1).add(answer2).add(answer3).add(answer4);
 app.add(panel);

nn++



     if (cnt == 7) {
       break}
   }
  return app;
}

问题似乎是那个data[nn]nn而不是细胞位置。我的日志数据显示了这一点

  • [13-06-14 09:04:58:671 EDT] 6.0 这是 nn 当它找到它正在寻找的东西时
  • [13-06-14 09:04:58:676 EDT] 7.0 这是我在增加它。
  • [13-06-14 09:04:58:676 EDT] 1.0 cnt
  • [13-06-14 09:04:58:680 EDT] 25.0 然后当它再次查看时,这又是 nn 值。
  • [13-06-14 09:04:58:680 美国东部时间] 2.0

....ETC

奇怪的是,它的发现值是它的第一个单元格和列。它得到所有这些奇数值,但它从第一列和第一行返回值。

4

2 回答 2

0

Why should your code something not starting at the first line? You're initializing nn to zero at the beginning of your code. Your nn++statement is obsolete as all the variables aren't persistent.

You should try something like:

function doGet() { 
  var app = UiApp.createApplication()
                .setTitle('When Am I Eligible?').setWidth("800px");
  app.add(app.createFlowPanel().setId('list'))
     .add(app.createHidden('firstRow', 0).setId('firstRow'))
     .add(app.createButton('Check')
       .addClickHandler(app.createServerHandler('buttonClick')
         .addCallbackElement(app.getElementById('list'))
         .addCallbackElement(app.getElementById('firstRow'))));
  return app;
}

function buttonClick(e) {
  var sh = SpreadsheetApp.openById(key).getSheetByName('SHEET');
  var data = sh.getRange(1,1,sh.getLastRow(),5).getValues();
  var valB = Session.getEffectiveUser().getEmail();
  var app = UiApp.getActiveApplication();
  var list = app.getElementById('list').clear();
  var lines = 0;
  for (nn = Number(e.parameters.firstRow); nn < data.length; nn++) {
    if (data[nn][1]==valB){
      var = app.createHorizontalPanel();
      for (var i = 0; i < data[nn].length; i++)
        panel.add(app.createLabel(data[nn][i]));
      list.add(panel);
      if (++lines > 7) {
        nn++
        break;
      }
    }
  }
  app.getElementById('firstRow').setValue(nn);
  return app;
}

---> Remember that this is untested code I just typed into the browser window, don't complayn about typos :))

于 2013-06-14T13:33:01.293 回答
0

乍一看,这可能不是您的主要问题,但您没有在任何地方声明您的cnt变量。尝试 (var cnt=0; cnt<8; ++cnt)

于 2013-06-14T10:57:39.307 回答