Apps Script for multiple selections from list, auto-check of prior selections: Selections are populating in random order

40 views
Skip to first unread message

Jon S

unread,
Sep 30, 2022, 8:59:29 AMSep 30
to Google Apps Script Community
Newbie here.

These are the two app script codes I'm using. Top one is saved as multi-select.gs. Second one is saved as dialog.html

The issue is that that the order in which the values selected populating the cell appears to be completely random whenever they are set. So if I click "set," it can populate in one order (.e.g. A, B.C) and then I click "set" a few more times and it keeps changing each time (e.g., B, C, A). There seems to be no rhyme or reason to it, as far as I can tell.

I would love to be able to do it in the order in which they are selected, but if that is not possible, then at least for it to populate accoding to the order in the list reflected in the apps script menu.

See below for what happens right now though:First click of set in menu.pngSecond click of set in menu.png

(Background: I created it based off the script created here, which creates a dropdown for me to be able to select from multiple options to populate a cell. My validation lists are taken from ranges in other worksheets. And the issue doesn't appear to be limited to just me -  another user also comment about it)

Any help would be much appreciated!

cbmserv...@gmail.com

unread,
Oct 2, 2022, 4:19:38 PMOct 2
to google-apps-sc...@googlegroups.com

Hi Jon,

 

I looked through the code you have and I think the order is dependent on how Google grabs the info. I am not sure if there is an easy way to make Google grab it in a certain order.

 

But what you can do is sort both the form items and sort the entries as they are updated so that the list is always in a predictable order.

 

Change this function:

 

function getValidationData(){

  try {

    var list =  SpreadsheetApp.getActiveRange().getDataValidation().getCriteriaValues()[0].getValues();

    list.sort();

    return list;

  } catch(e) {

    return null

  }

}

 

And this one:

 

 

 

function setValues_(eupdate) {

  var selectedValues = [];

  

  for (var i in e) {

    selectedValues.push(i);

    selectedValues.sort();

  }

 

That should get the list in a sorted fashion for both the form and any entries updated.



 

From: google-apps-sc...@googlegroups.com <google-apps-sc...@googlegroups.com> On Behalf Of Jon S
Sent: September 30, 2022 5:59 AM
To: Google Apps Script Community <google-apps-sc...@googlegroups.com>
Subject: [Apps-Script] Apps Script for multiple selections from list, auto-check of prior selections: Selections are populating in random order

 

Newbie here.

 

These are the two app script codes I'm using. Top one is saved as multi-select.gs. Second one is saved as dialog.html

 

The issue is that that the order in which the values selected populating the cell appears to be completely random whenever they are set. So if I click "set," it can populate in one order (.e.g. A, B.C) and then I click "set" a few more times and it keeps changing each time (e.g., B, C, A). There seems to be no rhyme or reason to it, as far as I can tell.

 

I would love to be able to do it in the order in which they are selected, but if that is not possible, then at least for it to populate accoding to the order in the list reflected in the apps script menu.

 

See below for what happens right now though:

 

(Background: I created it based off the script created here, which creates a dropdown for me to be able to select from multiple options to populate a cell. My validation lists are taken from ranges in other worksheets. And the issue doesn't appear to be limited to just me -  another user also comment about it)

 

Any help would be much appreciated!

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/da3d7537-0009-4fad-a1aa-eaed7ec62f11n%40googlegroups.com.

image001.png
image002.png

Jon S

unread,
Oct 4, 2022, 7:02:19 PMOct 4
to Google Apps Script Community
Thanks very much. Would there by anyone to sort this differently than alphabetically? For purposes of this specific list, it woud be easier for the person using the  sidebar menu if the terms are sorted by association with a certain class. Which is why I  entered into the validation list accortding to that  different order.

cbmserv...@gmail.com

unread,
Oct 4, 2022, 7:22:12 PMOct 4
to google-apps-sc...@googlegroups.com

You can sort it easily by reverse alphabet order as well. But what you are asking for is a special sort order based on some other data than the list itself. You can certainly write your own sort algorithm, but you will need to define how you want to do it and have at least intermediate coding skills to do it.

 

Google did not sort validation lists, they just take them in the order they are entered. But as you saw, the tagging was not in any specified order that you wanted..

Reply all
Reply to author
Forward
0 new messages