试图从电子表格和数据中获取数据使用UiApp函数显示它们,但我只得到最后一个匹配的条目,如何让GAS等待每个结果的输入?

时间:2014-06-11 18:51:41

标签: google-apps-script google-sheets

GAS和编程一般都是新手。我想做的是:

  1. 如果一行有48 / AW列并且第50列为空,则它应该从该行中的几个单元格中获取数据。
  2. 使用UiApp功能在整齐的摘要/报告中排列和显示收集的数据。
  3. 使用单选按钮和文本输入字段抓取值,并将所述值保存回AW。
  4. 到目前为止,这是我的代码:

    function TestFunction() {
    var ss = SpreadsheetApp.openById("Spreadsheet_ID").getActiveSheet();
    var values = ss.getDataRange().getValues(); // This gets ALL data within the sheet - not very useful if you need only snippets
    var ELTSummarizer = UiApp.createApplication().setHeight(480).setWidth(640).setTitle('Unreviewed Applicant');
    var CandidateInfoGrid = ELTSummarizer.createGrid(3, 2);
    var panel = ELTSummarizer.createVerticalPanel();
    var button = ELTSummarizer.createButton('Press Me');
    
    panel.add(CandidateInfoGrid);
    panel.add(button);
    ELTSummarizer.add(panel);
    
    for(n = 2; n < values.length; ++n){  // Make a finite loop that reads every row found in column AW (the 48th column in the sheet) data will be stored in the variable called Cell
       var cell = values[n][48] ;       //n = row number, 48 = Column AW where comments are saved - x is the index of the column starting from 0
       CandidateInfoGrid.setWidget(0, 0, ELTSummarizer.createLabel('Name:'+ values[n][1]));
       CandidateInfoGrid.setWidget(0, 1, ELTSummarizer.createLabel('Row#:'+ n));
    
      if (values[n][50] == '' && cell == '') {
    
        var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
        spreadsheet.show(ELTSummarizer);
      }   
     }
    }
    

    目前发生的是找到了正确的数据(显然)但是我看到浏览器一次又一次刷新并且它会一直覆盖结果,直到循环到达最后一个匹配的行并且是我看到的那个。所以我的问题是:

    • 有没有办法让循环停在每个结果?
    • 这是否是实现我想要的正确方法?还有另一种方法可以让这些数据显示出能够输入将被写回同一行的数据吗?

    像往常一样,任何帮助将不胜感激,感谢您阅读这篇文章!

    P.S。我尝试过使用Google提供的用户界面服务指南/示例,所有这些指南/示例都以&#34;遇到错误:发生意外错误&#34;消息 - 在某些情况下,他们仍然有效。不知道为什么会那样做。

1 个答案:

答案 0 :(得分:0)

根据您的评论,这是一个版本,在您的用户界面中逐个显示结果,并有一个按钮来继续这个过程...是您的想法? 不确定我到底知道你想要什么。

function TestFunction() {
  var ss = SpreadsheetApp.openById("1aIMzaaHR5czjc2gl0ifHI7SCt8ihw_Y4rwuCgCVhvt8").getSheets()[0];// get first sheet in this SS
  var values = ss.getDataRange().getValues(); // This gets ALL data within the sheet - not very useful if you need only snippets
  var ELTSummarizer = UiApp.createApplication().setHeight(480).setWidth(640).setTitle('Unreviewed Applicant');
  var CandidateInfoGrid = ELTSummarizer.createGrid(3, 2);
  var panel = ELTSummarizer.createVerticalPanel();
  var handler = ELTSummarizer.createServerHandler('continueLoop').addCallbackElement(panel)
  var button = ELTSummarizer.createButton('next match').addClickHandler(handler);
  CandidateInfoGrid.setWidget(0, 0, ELTSummarizer.createLabel('Name:').setId('labelName'));
  CandidateInfoGrid.setWidget(1, 0, ELTSummarizer.createLabel('Row#:').setId('labelRow'));
  var pos = ELTSummarizer.createTextBox().setName('pos').setId('pos');// .setVisible(false); // uncomment this to hide the position counter box
  panel.add(CandidateInfoGrid);
  panel.add(button).add(pos);
  ELTSummarizer.add(panel);
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var position = 2

  for(n = position; n < values.length; ++n){  // Make a finite loop that reads every row found in column AW (the 48th column in the sheet) data will be stored in the variable called Cell
    var cell = values[n][48] ;       //n = row number, 48 = Column AW where comments are saved - x is the index of the column starting from 0    
    if (values[n][50] == '' && cell == '') {
      position = n;
      pos.setText(n);
      ELTSummarizer.getElementById('labelName').setText('Name : '+ values[n][1]);
      ELTSummarizer.getElementById('labelRow').setText('Row# : '+ n);   
      break;
    }   
  }
  spreadsheet.show(ELTSummarizer);
}

function continueLoop(e){
  var ss = SpreadsheetApp.openById("1aIMzaaHR5czjc2gl0ifHI7SCt8ihw_Y4rwuCgCVhvt8").getSheets()[0];// get first sheet in this SS
  var values = ss.getDataRange().getValues(); // This gets ALL data within the sheet - not very useful if you need only snippets
  var app = UiApp.getActiveApplication();
  var labelName = app.getElementById('labelName');
  var labelRow = app.getElementById('labelRow');
  var pos = app.getElementById('pos');
  var position = Number(e.parameter.pos);
  for(n = position; n < values.length; ++n){  // Make a finite loop that reads every row found in column AW (the 48th column in the sheet) data will be stored in the variable called Cell
    var cell = values[n][48] ;       //n = row number, 48 = Column AW where comments are saved - x is the index of the column starting from 0    
    if (values[n][50] == '' && cell == '') {
      app.getElementById('labelName').setText('Name : '+ values[n][1]);
      app.getElementById('labelRow').setText('Row# : '+ n);
      n++;
      pos.setText(n);
      break;
  }   
  }
  return app;
}  

continueLoop函数应该处理文本的输入并将该内容写入工作表...仍然需要对此脚本执行一些操作... 我会在一个使用参数的单独函数中完成它,但如果它只有一个字段,你可以将它集成到同一个函数中。

希望有帮助

enter image description here