Created
August 26, 2021 21:53
-
-
Save a11ce/c22495f2784430ec5154866495dbda7c to your computer and use it in GitHub Desktop.
Class Spreadsheet Sorter
This file contains hidden or 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
// https://stackoverflow.com/a/54024653 | |
function hsv2rgb(h,s,v) | |
{ | |
let f= (n,k=(n+h/60)%6) => v - v*s*Math.max( Math.min(k,4-k,1), 0); | |
return [f(5) * 255,f(3) * 255,f(1) * 255]; | |
} | |
// https://stackoverflow.com/a/52171480 | |
function hash(str, seed = 0) { | |
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed; | |
for (let i = 0, ch; i < str.length; i++) { | |
ch = str.charCodeAt(i); | |
h1 = Math.imul(h1 ^ ch, 2654435761); | |
h2 = Math.imul(h2 ^ ch, 1597334677); | |
} | |
h1 = Math.imul(h1 ^ (h1>>>16), 2246822507) ^ Math.imul(h2 ^ (h2>>>13), 3266489909); | |
h2 = Math.imul(h2 ^ (h2>>>16), 2246822507) ^ Math.imul(h1 ^ (h1>>>13), 3266489909); | |
return 4294967296 * (2097151 & h2) + (h1>>>0); | |
} | |
function onEdit(event){ | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Classes Fall 2021"); | |
var editedCell = sheet.getActiveCell(); | |
var columnToSortBy = 1; | |
var tableRange = "A3:C99"; | |
if(editedCell.getColumn() == columnToSortBy){ | |
var range = sheet.getRange(tableRange); | |
range.sort( { column : columnToSortBy } ); | |
} | |
for(var idx = 3; idx < 99; idx++) | |
{ | |
var row = sheet.getRange(idx,1,1,3).getValues()[0]; | |
if(row[0] == "") | |
{ | |
return; | |
} | |
var h = hash(row[0].split(" ")[0]); | |
var hin = h % 359; | |
var sin = (h % 9) + 9; | |
var lin = (h % 10) + 90; | |
Logger.log([hin,sin,lin]); | |
var rgb = hsv2rgb(hin, sin/100, lin/100); | |
Logger.log(rgb) | |
sheet.getRange(idx,1,1,3).setBackgroundRGB(rgb[0],rgb[1],rgb[2]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment