Creating a Button that will run a script to select a random number from a group of cells.

Skip to first unread message

Bill Roberson

Aug 25, 2022, 11:34:47 AMAug 25
to Google Apps Script Community
I have a Google Spreadsheet where I have created a button "Click to Select Random Room Number" and a cell beside the button where I would like this room number to be displayed ("D1"). I have recorded a list of room numbers in cells G2 through G155 that I would like the script to refer to when selecting a random number. 

I have figured out how to generate a random number from this selection but I cannot figure out how to make this work with the use of App Scripts. Ideally, each time I click the button that I've created, I'd like the script to run and select a new/random number each time the button is clicked. Where do I start?

Ben Ronkin

Aug 26, 2022, 8:17:50 PMAug 26
to Google Apps Script Community
Use this code:

const pickRoomNumber = () => {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  const options = sh
    .filter((x) => x.toString().length > 0);
  const pos = Math.floor(Math.random() * options.length);

Bill Roberson

Sep 26, 2022, 6:38:05 PMSep 26
to Google Apps Script Community
Thank you.
Reply all
Reply to author
0 new messages