提问者:小点点

Google电子表格值使用Google Apps脚本推送到表单,但按字母顺序排序


我有一个Google Apps脚本,它可以获取电子表格中特定列中的数据,并将其推送到Google表单的下拉菜单中。

这一切工作完美。

但是,我正在尝试通过脚本对数据进行排序,以便在将其推送到表单之前,它会按字母顺序对其进行排序(A-

电子表格中的数据没有排序,我不能在电子表格中对其进行排序,因为数据不断添加。

这是有效的Google Apps脚本,除了排序:

function updateClientForm(){
  // call your form and connect to the drop-down item
  var form = FormApp.openById("1qszM48QILtnf-2QZa078ypqhjYHdB-VK2c0VMJDrsXo");

  //Inspect the element on the form to find the ID value
  var clientnamesList = form.getItemById("449574637").asListItem();


// identify the sheet where the data resides needed to populate the drop-down
  var ss = SpreadsheetApp.getActive();
  var clientnames = ss.getSheetByName("Client Names with ID");

  // grab the values in the first column of the sheet - use 2 to skip header row 
  var namesValues = clientnames.getRange(2, 1, clientnames.getMaxRows() - 1).getValues();

  var customerNames = [];

  // convert the array ignoring empty cells
  for(var i = 0; i < namesValues.length; i++)    
    if(namesValues[i][0] != "")
      customerNames[i] = namesValues[i][0];

  // populate the drop-down with the array data
  clientnamesList.setChoiceValues(customerNames);

}

有什么想法吗?提前感谢!


共1个答案

匿名用户

Google Apps Script基于javascript,因此您几乎可以访问您可以在javascript中执行的所有操作。

namesValue是一个数组,javascript数组带有内置的排序函数

sortedNamesValues = namesValues.sort();