Skip to content

Instantly share code, notes, and snippets.

@sainu
Created September 13, 2016 00:56
Show Gist options
  • Save sainu/13dd56522e0902270ea9eee57d3babb2 to your computer and use it in GitHub Desktop.
Save sainu/13dd56522e0902270ea9eee57d3babb2 to your computer and use it in GitHub Desktop.
var column = {'id':0,'posted_count':1,'ja':2,'en':3};
function slack(message) {
var url = 'https://slack.com/api/chat.postMessage';
var token = 'FILL ME IN';
var channel = 'FILL ME IN';
var text = message;
var username = 'FILL ME IN';
var parse = 'full';
var icon_emoji = ':sunny:';
var method = 'post';
var payload = {
'token' : token,
'channel' : channel,
'text' : text,
'username' : username,
'parse' : parse,
'icon_emoji' : icon_emoji
};
var params = {
'method' : method,
'payload' : payload
};
var response = UrlFetchApp.fetch(url, params);
}
function shuffle(array) {
var n = array.length;
var t, i;
while (n) {
i = Math.floor(Math.random() * n--);
t = array[n];
array[n] = array[i];
array[i] = t;
}
return array;
}
function writeValue(sheet, index, ops) {
var cell = sheet.getRange(index+2, column[ops.column_name] + 1);
switch(ops.column_name) {
case 'id':
cell.setValue(index+1);
break;
case 'posted_count':
switch(ops.type) {
case 'activate':
cell.setValue(0);
break;
case 'increment':
cell.setValue(cell.getValue() + 1);
break;
}
}
return true;
}
function activate(sheet, source) {
for(var i = 0; i < source.length; i++) {
if(source[i][column.posted_count] === '') {
writeValue(sheet, i, {column_name: 'posted_count', type: 'activate'});
}
if(source[i][column.id] === '') {
writeValue(sheet, i, {column_name: 'id'});
}
}
}
function get_today_phrase(source) {
return shuffle(source).sort(function(d1, d2){d1[column.posted_count] < d2[column.posted_count]})[0];
}
function coreFunction() {
var spreadSheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/113lWWLTJ4pQUx673T-aoK9owZIZZ9sHku67DXBnkO1c/edit#gid=0');
var sheet = spreadSheet.getSheets()[0];
var lastRow = sheet.getLastRow();
var startRow = 2;
var startColumn = 1;
var numRows = lastRow - 1;
var numColumns = sheet.getLastColumn();
var source = sheet.getSheetValues(startRow, startColumn, numRows, numColumns);
activate(sheet, source);
var today_phrase = get_today_phrase(source);
writeValue(sheet, today_phrase[column.id], {column_name: 'posted_count', type: 'increment'});
var dt = new Date();
var year = dt.getFullYear(), month = dt.getMonth()+1, date = dt.getDate();
var dateT = ["日","月","火","水","木","金","土"];
var day = dateT[dt.getDay()];
var fulldate = year + '-' + month + '-' + date + ' (' + day + ')';
var line = '\n-------------------------------------------\n';
var message = '【定期】' + fulldate + 'のワンフレーズは、\n' + line + today_phrase[column.en] + '\n( ' + today_phrase[column.ja] + ' )\n' + line + 'です!';
slack(message);
/*Logger.log(message);
Browser.msgBox(Logger.getLog());*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment