电子表格中2列的Google Apps Script Concatenate 2值 - 显示在列表框中

时间:2013-03-28 17:53:31

标签: javascript google-apps-script

寻找一点帮助我几乎在那里,但我知道我只是缺少一件事而且我一直在寻找答案。

我有一张电子表格,其中一列有第一个名字,第二列有姓氏,我正在尝试加入它们并在列表框中以全名显示。现在,它会逐字逐字地填充值,第一列为值,第二列为值。任何帮助将不胜感激!

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

  var dateSelect = e.parameter.dateList;

  var ss = SpreadsheetApp.openById('spreadsheetID');
  var sheet = ss.getSheetByName(dateSelect);

  var studentList = app.createListBox().setName('studentList').setId('studentList');
  var cellList = sheet.getLastRow();
  var fName = sheet.getRange(1,1,cellList,1).getValues();
  var lName = sheet.getRange(1,2,cellList,1).getValues();
  var fullName = (fName+ " " +lName);


  //Add items to the list box
  for(var i=0; i<fullName.length; i++){
    studentList.addItem(fullName[i]);
  }

  app.getElementById('grid1').setWidget(1,1,studentList);




  return app;

}

1 个答案:

答案 0 :(得分:5)

fNamelName是值的矩阵。你不能像这样对它们进行求和/连接。这里修复:

function studentBox(e) {
  var app = UiApp.getActiveApplication();
  var dateSelect = e.parameter.dateList;

  var ss = SpreadsheetApp.openById('spreadsheetID');
  var sheet = ss.getSheetByName(dateSelect);

  var studentList = app.createListBox().setName('studentList').setId('studentList');
  var cellList = sheet.getLastRow();
  var names = sheet.getRange(1,1,cellList,2).getValues(); //getting both first and last names at once

  //Add items to the list box
  for(var i=0; i<names.length; i++){
    studentList.addItem(names[i][0]+' '+names[i][1]);
  }

  app.getElementById('grid1').setWidget(1,1,studentList);
  return app;
}