0

我需要在 Google Script 中创建一个应用程序,它可以从电子表格中读取值并根据第一列的值将这些值加载到文本区域中。例如,在第一列中,我在第二个名字、第三个姓氏、第四个停止日期有昵称。我已经设法使用 getRange 将昵称加载到列表框中。但我需要知道如何根据 NickName 将相邻的值(第一个、最后一个、停止日期)加载到文本区域中。单击按钮后,这些值将被更新而不创建新记录。让我知道。这会有很大帮助。

这是我到目前为止的代码。我需要根据从下拉框中选择的昵称将值加载到相应的文本字段中:

function doGet() {
var app = UiApp.createApplication();
var abspanel= app.createAbsolutePanel();
var stackpanel=app.createStackPanel();
var grid3=app.createGrid(12,12).setId('grd3');
var loadbtn= app.createButton('Load Employee Data');
var selectlbl=app.createLabel('Select Employee');
var active= SpreadsheetApp.openById('0Ai21cbl').getSheetByName('testing');
var endrow= active.getLastRow()-1;
var emplist=app.createListBox().setId('emps').setName('elist');

var emprange= active.getRange('A2:F100').getValues().sort();
 for(var i=0; i<emprange.length; i++){
   emplist.addItem(emprange[i][0])
 }

var efirstlabel=app.createLabel('First').setId('elabel');
var efname=app.createTextArea().setId('efirst').setHeight('20px').setName('efst');
var elastlabel=app.createLabel('Last').setId('ellabel');
var elname=app.createTextArea().setId('elast').setHeight('20px').setName('elst');
var enicklabel=app.createLabel('NickName').setId('enlabel');
var enname= app.createTextArea().setId('enick').setHeight('20px').setName('enk');
var eattlabel= app.createLabel('Employee Attribute').setId('ealabel').setHeight('20px');
var eatt1='TLYDYE'
var eatt2='TLYDNE'
var eatt3='TLNDYE'
var eatt4='TLNDNE'
var eatt5='CAYDYE'
var eatt6='CAYDNE'
var eatt7='CANDYE'
var eatt8='CANDNE'
var eattlist=app.createListBox().setWidth('120px').setId('eattribute').setName('eattbt')
eattlist.addItem(eatt1);
eattlist.addItem(eatt2);
eattlist.addItem(eatt3);
eattlist.addItem(eatt4);
eattlist.addItem(eatt5);
eattlist.addItem(eatt6);
eattlist.addItem(eatt7);
eattlist.addItem(eatt8);

var estartlabel=app.createLabel('Start Date:');
var estartdate=app.createDateBox().setId('edate');
var estoplabel=app.createLabel('Stop Date:');
var estopdate=app.createDateBox().setId('sdate');
var inlabel=app.createLabel().setId('indexlabel');

//Edit Employee Handler

var loadhandler=app.createServerClickHandler('loadEmp');
loadhandler.addCallbackElement(stackpanel);
loadbtn.addClickHandler(loadhandler);

grid3
.setWidget(1,1,selectlbl)
.setWidget(1,2,emplist)
.setWidget(1,3,loadbtn)
.setWidget(2,1, efirstlabel)
.setWidget(2,2, efname)
.setWidget(2,3, elastlabel)
.setWidget(2,4, elname)
.setWidget(3,1, enicklabel)
.setWidget(3,2, enname)
.setWidget(4,1, eattlabel)
.setWidget(5,1,eattlist)
.setWidget(6,1,estartlabel)
.setWidget(7,1,estartdate)
.setWidget(8,1,estoplabel)
.setWidget(9,1,estopdate)
.setWidget(10,1,inlabel);

stackpanel.add(grid3, 'Edit Employee').setStyleAttribute("text-align", "center")
app.add(abspanel)
app.add(stackpanel)  
return app;
}

function loadEmp(e){
var app=UiApp.getActiveApplication();

app.getElementById('efirst').setValue(e.parameter.elist);

return app;
}
4

1 回答 1

0

好吧,我想如果在将每个名称添加到列表时添加为返回行号的值而不是省略并只有名称会更容易。这样,在loadEmp处理程序上重新加载值会更容易(即您不需要搜索它)。例如

function doGet() {
  //... the starting code your already have
  var emprange = active.getDataRange().getValues(); //sorting a matrix doesn't make sense
  //also, it's easier if you sort by the desired column the on the spreadsheet before hand
  for( var i = 1; i < emprange.length; ++i )
    emplist.addItem(emprange[i][0], i); //this 2nd parameter "i" is the return value

  //... rest of your code
}

function loadEmp(e) {
  var app = UiApp.getActiveApplication();
  var active = SpreadsheetApp.openById('0Ai21cbl').getSheetByName('testing');
  var index = +e.parameter.elist;
  var empdata = active.getRange(index+1, 1, 1, 6).getValues()[0];
  var cols = ['efirst','elast','enick']; //in the same order as in the spreadsheet
  for( var i = 0; i < cols.length; ++i )
    app.getElementById(cols[i]).setValue(empdata[i]);
  return app;
}

保存按钮也是一样的,你只会setValues得到而不是得到。

于 2013-06-06T03:04:59.293 回答