简体   繁体   中英

Google Apps Script Concatenate 2 values from 2 columns in Spreadsheet - display in listbox

Looking for a little help I'm almost there but I know I'm just missing one thing and I have been searching for the answer for awhile.

I have a spread sheet with 1 column that has the first name and the 2nd has the last name, I'm trying to join them and display in full name in the listbox. Right now it populates with the values horizontally letter by letter, with the first columns values and then the second column values. Any help would be appreciated!

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;

}

fName and lName are matrices of values. You can not sum/concatenate them like this. Here the fix:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM