javascript Google Apps Script 连接来自电子表格中 2 列的 2 个值 - 显示在列表框中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15688825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:42:18  来源:igfitidea点击:

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

javascriptgoogle-apps-script

提问by RachelW

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!

我有一个电子表格,其中 1 列有名字,第二列有姓氏,我试图加入它们并在列表框中以全名显示。现在,它用水平字母一个字母填充值,第一列值,然后是第二列值。任何帮助,将不胜感激!

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;

}

回答by Henrique G. Abreu

fNameand lNameare matrices of values. You can not sum/concatenate them like this. Here the fix:

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