Script for Selecting random data from a list on button press

I'm creating a spreadsheet which can automatically select random cell data from a particular column when I press a button. However, I cannot figure out the script.

Thanks to some friends I've tried a few variations that involve using add-ons but ideally I don't want to use anything like that as it needs to be usable if people wanted to make a copy without the addons.

What I'm looking to do is click a particular button and then the adjacent cell displays a random value from the data set given in the list.

I have two sheets. "Interface" and "Under the Hood" - Pretty self-explanatory, Interface has the buttons and Under the hood contains the data.

Here is some code that I've found elsewhere online, trying to use this as a base.

  function random() {
      var ss=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
      var val=ss.getRange("A1").getValue()
      var clr =ss.getRange("A1").clearContent()

To throw an example out there -

A2-A33 is our data setlist (Under the Hood sheet)

I Have drawn a button and I would like the random selection to appear in cell D17 (interface).

Dummy Data:

Can anybody walk me through how to do this?

1 answer

  • answered 2019-09-15 19:16 Cooper

    Try this:

    function getRandomValue(searchColumn,resultCellRow,resultCellColumn) {
      var ss=SpreadsheetApp.getActive();
      var dsh=ss.getSheetByName('Interface');
      var ssh=ss.getSheetByName('UnderTheHood')
      var rg=ssh.getRange(2,searchColumn,ssh.getLastRow()-1,1);
      var vA=rg.getValues().map(function(r){return r[0]});
      while(vA[vA.length-1].length==0) {
      dsh.getRange(resultCellRow,resultCellColumn).setValue( vA[Math.floor(Math.random()*vA.length)]);
    function generateCoach() {
    function coachBackStory() {
    function contracts() {

    I presume that you can figure out the rest of the functions for the various buttons. All you need to do is give it the column to search in "UnderTheHood" and the cell to put answer in "Interface"