Sorting Script in Sheets...What Am I Doing Wrong?

63 views
Skip to first unread message

Jennifer Preissel

unread,
Oct 2, 2023, 12:00:26 PM10/2/23
to Google Apps Script Community
Complete novice here! I'm a high school math teacher and I made a spreadsheetĀ of seating arrangementsĀ which I randomize daily. But rather than doing the data sort every day, I want to just push a button in the document.Ā 

Sharing screengrabs of the spreadsheet and of the script I started, which was meant to randomize the tab for the Green Section. But when I run it, I get an error. Any thoughts as to why?Screen Shot 2023-10-02 at 11.59.12 AM.png
script_screenshot.png

Jon Couch

unread,
Oct 2, 2023, 12:29:03 PM10/2/23
to google-apps-sc...@googlegroups.com
Here's a quick ChatGPT script that seems to work for me. I'm assuming first and last names are in columns B & C from Row 13 to Row 30

function myFunction() {
Ā  const ss=SpreadsheetApp.getActiveSpreadsheet();
Ā  const sheet = ss.getSheetByName("Green S1");
Ā  const range = sheet.getRange("B13:C30");

Ā  // Get the values in Columns B and C
Ā  var values = range.getValues();

Ā  // Create an array to store the shuffled rows
Ā  var shuffledRows = [];

Ā  // Randomly shuffle the rows
Ā  while (values.length > 0) {
Ā  Ā  var randomIndex = Math.floor(Math.random() * values.length);
Ā  Ā  var randomRow = values.splice(randomIndex, 1)[0];
Ā  Ā  shuffledRows.push(randomRow);
Ā  }

Ā  // Set the shuffled rows back in Columns B&C
Ā  range.setValues(shuffledRows);
}

Good luck,
Jon

--
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/feefdff9-22b0-44d4-b290-e43d30ae00c0n%40googlegroups.com.

Keith Andersen

unread,
Oct 2, 2023, 4:42:36 PM10/2/23
to google-apps-sc...@googlegroups.com
Jennifer
Make a copy of this and you will become sole owner with editor privileges. I hope it meets your needs.


Cheers
Keith

Keith Andersen

unread,
Oct 3, 2023, 3:53:39 PM10/3/23
to google-apps-sc...@googlegroups.com
Jennifer,
If you haven't already downloaded it, I revised it yesterday and made it so that you can copy the sheet,Ā  rename it to another class, and then put in that class's children and it will work for any and all classes you need to make sheets for.

šŸ‘ KeithĀ 
Reply all
Reply to author
Forward
0 new messages