Last active
August 8, 2020 08:25
-
-
Save jagedn/05cbf22d3cd97afc03f5b9982653de65 to your computer and use it in GitHub Desktop.
A Raffle in Google Sheet v0.1, see https://dev.to/jagedn/build-a-raffle-with-google-sheet-5da
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
function onOpen(e) { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createAddonMenu().addItem('Raffle', 'raffleUI').addToUi(); | |
} | |
function getRemains(){ | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getActiveSheet(); | |
var ret = []; | |
for(var i=3; i<ss.getLastRow()+1;){ | |
var from = i; | |
var to = from+100; //google has a max of 100 rows per read | |
var rows = ss.getRange("A"+from+":D"+to).getValues(); | |
for( r in rows ){ | |
if( !rows[r][0] ){ | |
break | |
} | |
if( !rows[r][3] ){ | |
// rowIndex, name and surname | |
ret.push( [i,rows[r][0],rows[r][1]] ) | |
} | |
} | |
i=to+1 | |
} | |
return ret; | |
} | |
function yepes(idx){ | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getActiveSheet(); | |
var cell = sheet.getRange("C"+idx); | |
cell.setValue("yepes"); | |
raffleUI(); | |
} | |
function nopes(idx){ | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getActiveSheet(); | |
var cell = sheet.getRange("C"+idx); | |
cell.setValue("nopes"); | |
raffleUI(); | |
} | |
function raffle(){ | |
var remains = getRemains(); | |
var suertudoIndex = Math.floor(Math.random()*remains.length); | |
var suertudo = remains[suertudoIndex]; | |
Logger.log(suertudo) | |
var htmlOutput = HtmlService | |
.createHtmlOutput('<p>'+ | |
'<image src="https://media.giphy.com/media/11sBLVxNs7v6WA/giphy.gif">'+ | |
'</p>'+ | |
'<p>'+ | |
'<div>'+ | |
'<input type="button" value="Yepes" onclick="google.script.run.withSuccessHandler(google.script.host.close).yepes('+suertudo[0]+')" /> '+ | |
'<input type="button" value="Nopes" onclick="google.script.run.withSuccessHandler(google.script.host.close).nopes('+suertudo[0]+')" />'+ | |
'</div>'+ | |
'</p>' | |
) | |
.setWidth(550) | |
.setHeight(350); | |
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Congratulations '+suertudo[2]+','+suertudo[1]); | |
} | |
function raffleUI(){ | |
var remains = getRemains(); | |
var html = '<!DOCTYPE html>'+ | |
'<html>'+ | |
'<head>'+ | |
'<base target="_top">'+ | |
'<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> '+ | |
'<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>'+ | |
'</head>'+ | |
'<body>'+ | |
'<div> There are ' + remains.length + ' participants</div>'+ | |
'<p>'+ | |
'<div><input type="button" value="Sortear" onclick="google.script.run.raffle()" />'+ | |
'</p>'+ | |
'</body>'+ | |
'</html>' | |
var htmlOutput = HtmlService | |
.createHtmlOutput(html) | |
.setTitle('Raffle'); | |
SpreadsheetApp.getUi().showSidebar(htmlOutput); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment