Created
March 17, 2019 14:17
-
-
Save takatoshiono/0024c85298c17d4af3fede98899f948c to your computer and use it in GitHub Desktop.
Bingo numbers for Google Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var minNum = 1 // The minimum bingo number | |
var maxNum = 15 // The maximum bingo number | |
var numRows = 3 // The number of bingo sheet rows | |
var numCols = 3 // The number of bingo sheet columns | |
// The list of left top point of a bingo sheet in format of spreadsheet rows and columns number | |
var startingPoints = [ | |
{"row":4,"col":2}, | |
{"row":4,"col":6}, | |
{"row":11,"col":2}, | |
{"row":11,"col":6}, | |
{"row":18,"col":2}, | |
{"row":18,"col":6}, | |
] | |
// refs: | |
// https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app | |
// https://developers.google.com/apps-script/reference/spreadsheet/sheet | |
// https://developers.google.com/apps-script/reference/spreadsheet/range | |
function main() { | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
startingPoints.forEach(function(p){ | |
values = getValues() | |
range = sheet.getRange(p.row, p.col, numRows, numCols) | |
range.setValues(values) | |
}) | |
} | |
function getValues() { | |
var values = [] | |
var exists = {} | |
for (i = 0; i < numRows; i++) { | |
values[i] = [] | |
for (j = 0; j < numCols; j++) { | |
while (true) { | |
n = getRandomInt(minNum, maxNum) | |
if (!exists[n]) { | |
values[i][j] = n | |
exists[n] = true | |
break; | |
} | |
} | |
} | |
} | |
return values | |
} | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; //The minimum and maximum are inclusive | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment