使用GAS UI中的新值更新Panel

时间:2014-08-06 10:00:03

标签: user-interface google-apps-script

我有一个巨大的电子表格矩阵,我从中创建了一个很长的复选框列表。然后用户选择不同的能力,按搜索。代码与数据库电子表格交叉检查,返回具有这些能力的人员的姓名。

我需要使用搜索结果更新“rightPanel”。但我简单无法弄清楚如何 - 如果完全可行 - 更新我的UI中的面板..

var dataSSkey = 'sheetID'; //datasheet ID
var dataSheet = SpreadsheetApp.openById(dataSSkey).getSheetByName('Ansatte');
var groupsArray = [[],[],[],[]]; 
var lastRow = dataSheet.getLastRow();
var lastColumn = dataSheet.getLastColumn();
var dataArray  = dataSheet.getRange(1,1,lastRow,lastColumn).getValues();
var numberGroups
var app = UiApp.createApplication().setTitle('Find Consultant');
var panel = app.createVerticalPanel();
var leftPanel = app.createVerticalPanel().setWidth(450);
var rightPanel = app.createVerticalPanel().setWidth(450);
var grid = app.createGrid(1, 2).setId('myGrid')

var outputArray = []; //to store output from search

var positiveList = [[],[]]; //array to store name and folder-ID of consultants matching
var numberPositive = 0; //number of consultants matching

function doGet() {
  buildGroupsArray()

  addCheckBoxesToUI()

  var scrollPanel = app.createScrollPanel().setHeight(460);

  //Search button
  var searchButton = app.createButton('Search'); 
  var clickHandler = app.createServerClickHandler("respondToSearch");
  searchButton.addClickHandler(clickHandler);
  clickHandler.addCallbackElement(panel);

  var spacerImage = app.createImage("http://www.bi..ge.jpg").setHeight(3);

  scrollPanel.add(panel);
  rightPanel.add(app.createLabel('resultat her'));


  leftPanel.add(scrollPanel);
  leftPanel.add(spacerImage);
  leftPanel.add(searchButton);

  grid.setWidget(0, 0, leftPanel)
  grid.setWidget(0, 1, rightPanel);

  app.add(grid);

  return app;
}
function respondToSearch(e){
var numberLogged = 0;

//define firstEmpty
var firstEmpty = "A"+lastRow;
if(lastRow !== 1){
  firstEmpty = "A"+(lastRow+1);
};

//find selected competencies --> store in array + count competencies 
for(i = 1; i <= lastRow; i++){
if (e.parameter["Checkbox"+i] == "true") {
  var value = e.parameter["CheckboxValue"+i];
  outputArray[numberLogged] = value;
  numberLogged++;
  }
}

for(i = 2; i <= lastColumn; i++){
   var numberCorrect = 0;

//Run through rows according to content of output from selection
for(j in outputArray){

   //Check if consultant own selected competency
   if(dataArray[outputArray[j]][i] == "x"){
     numberCorrect++; //if consultant owns selected competency then count
     }
   }

  //if consultant owns all competencies, then add name and folder-id to array
  if(numberCorrect == numberLogged){
    positiveList[0][numberPositive] = dataArray[1][i]; //Add consultant name
    positiveList[1][numberPositive] = dataArray[2][i]; //Add consultant-folder ID
    numberPositive++ //count the number of consultants that own all competencies
    }
  }

for(j in positiveList[0]){
   var name = positiveList[0][j];
   var id = positiveList[1][j];
   Logger.log(name);
   Logger.log(id)
   var anchor = app.createAnchor(name,'https://ww......folderviewid='+id);
rightPanel.add(anchor)
}
return app;
}

1 个答案:

答案 0 :(得分:0)

我真的不明白你的问题......

在您的处理程序函数中,您只需使用app=UiApp.getActiveApplication()并从那里填充面板的方式与doGet()函数中的方式完全相同,以实际return app;结尾更新当前的Ui。

周围有几十个例子......我误解了你问题中的某些内容吗?


编辑:发表评论。

我想你在doGet函数之外定义了你的变量,希望它们变得全局,因此脚本中的所有函数都可以使用它们,但这不会起作用。无法通过功能更新Google Apps脚本中的全局变量。 我强烈建议您在doGet函数中创建应用程序和面板,并为它们提供一个ID,以便您可以将它们取回并从处理程序函数更新它们的值(或内容)。

这是您的代码的重写版本(未测试) :(某些部分未被复制(见//...)

var dataSSkey = 'sheetID'; //datasheet ID
var dataSheet = SpreadsheetApp.openById(dataSSkey).getSheetByName('Ansatte');
var groupsArray = [[],[],[],[]]; 
var lastRow = dataSheet.getLastRow();
var lastColumn = dataSheet.getLastColumn();
var dataArray  = dataSheet.getRange(1,1,lastRow,lastColumn).getValues();
var numberGroups

var outputArray = []; //to store output from search

var positiveList = [[],[]]; //array to store name and folder-ID of consultants matching
var numberPositive = 0; //number of consultants matching

function doGet() {
  var app = UiApp.createApplication().setTitle('Find Consultant');
  var panel = app.createVerticalPanel();
  var leftPanel = app.createVerticalPanel().setWidth(450).setId('leftPanel');
  var rightPanel = app.createVerticalPanel().setWidth(450).setId('rightPanel');;
  var grid = app.createGrid(1, 2).setId('myGrid')
  buildGroupsArray(app); // in this function get app as parameter or use UiApp.getActiveApplication();

  addCheckBoxesToUI(app);// in this function get app as parameter or use UiApp.getActiveApplication();

  var scrollPanel = app.createScrollPanel().setHeight(460);
//...
//...  
  return app;
}
function respondToSearch(e){
//...
//...
  var app = UiApp.getActiveApplication();
  var rightPanel = app.getElementById('rightPanel');

  for(j in positiveList[0]){
    var name = positiveList[0][j];
    var id = positiveList[1][j];
    Logger.log(name);
    Logger.log(id)
    var anchor = app.createAnchor(name,'https://ww......folderviewid='+id);
    rightPanel.add(anchor)
  }
  return app;
}