Google Apps Script randomize certain cells function not working

46 Views Asked by At

I am trying to create a simple script activated via button to randomise a game names list. I want to enter 6 names under Rounds 1-13 and have them shuffle into certain cells. I am using the script found of the google developers website: https://developers.google.com/apps-script/reference/spreadsheet/range#randomize() - it works fine until I change .getRange to .getRangeList to select multiple names under their headers.

Googles randomizer:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:C7");

// Randomizes the range
range.randomize();

My randomizer:

function shuffle() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Bike Bash");
const range = sheet.getRangeList(['B5:B10' ,'B12:B17' ,'B19:B24' ,'B26:B31' ,'B33:B38' ,'B40:B45' ,'B47:B52' 'B54:B59']);

// Randomizes the range
range.randomize();
}

Result: Result

1

There are 1 best solutions below

0
On

The range you defined is not a Range but a RangeList, which does not has the method randomize.

Instead you should iterate through each Range.

function shuffle() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Bike Bash");
const ranges = ['B5:B10' ,'B12:B17' ,'B19:B24' ,'B26:B31' ,'B33:B38' ,'B40:B45' ,'B47:B52', 'B54:B59'];

// Randomizes the range
ranges.forEach(range => sheet.getRange(range).randomize());
}