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

时间:2018-05-25 01:18:11

标签: google-apps-script google-sheets google-form

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

这一切都很完美。

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

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

以下是适用的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 个答案:

答案 0 :(得分:0)

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

namesValues是一个数组,javascript数组带有内置的sort函数

sortedNamesValues = namesValues.sort();