Skip to content

Instantly share code, notes, and snippets.

@ilkovich
Last active March 11, 2016 20:08
Show Gist options
  • Select an option

  • Save ilkovich/ff5bb3ccc991789ac4fb to your computer and use it in GitHub Desktop.

Select an option

Save ilkovich/ff5bb3ccc991789ac4fb to your computer and use it in GitHub Desktop.
Apps Script that powers Botwick - https://rundexter.com/app/botwick-slackbot
function lookupAndCompute(spreadsheet_id, text, user_id, channel_id) {
var ss = SpreadsheetApp.openById(spreadsheet_id)
, matches = ss.getSheetByName("Matches")
, logs = ss.getSheetByName("Logs")
, rows = matches.getRange(2, 1, matches.getLastRow(), matches.getLastColumn())
, conv = SheetConverter.init(ss.getSpreadsheetTimeZone(), ss.getSpreadsheetLocale())
, re
, reMatches
, row
;
for(var i=1; i<=rows.getNumRows(); i++) {
re = rows.getCell(i, 1).getValue();
re = new RegExp(re, "i");
reMatches = text.match(re);
if(reMatches) {
row = conv.convertRange(rows)[i-1];
for(var i=0; i<reMatches.length; i++) {
match = reMatches[i]
.replace(/[\u2018\u2019]/g, "'")
.replace(/[\u201C\u201D]/g, '"')
.replace(/"/g, '""')
;
row[1] = row[1].replace(new RegExp('\\\\'+i,'g'), match);
}
row.unshift(text);
row.unshift((new Date()).toUTCString());
idx = logs.appendRow(row).getLastRow();
row = conv.convertRange(logs.getRange(idx, 1, 1, logs.getLastColumn()));
return { row: row[0] };
}
}
return "done";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment