Skip to content

Instantly share code, notes, and snippets.

@angelwong
Created March 8, 2017 18:16
Show Gist options
  • Save angelwong/9b36edbb23375fe2efae98859a58dbe9 to your computer and use it in GitHub Desktop.
Save angelwong/9b36edbb23375fe2efae98859a58dbe9 to your computer and use it in GitHub Desktop.
Include these methods in the "Code.gs" file of the "Script editor" of your Google Spreadsheet Bot
function getWeather(zip, additionalText) {
var response = UrlFetchApp.fetch("http://api.poncho.is/weather/forecast/"+zip)
, obj = JSON.parse(response.getContentText())
;
return [ [obj.data.body+additionalText, obj.data.media] ]
}
/*
* Retrieves the URL to the active spreadsheet
*
* Note: Cannot be called from HTTP context as SpreadsheetApp.getActiveSpreadsheet() == null
*/
function getUrl() {
return "http://docs.google.com/spreadsheets/d/" + SpreadsheetApp.getActiveSpreadsheet().getId();
}
/*
* Called in template spreadsheet to make the sheet id available to http requests
*/
function setSpreadsheetId() {
var sheetId= SpreadsheetApp.getActiveSpreadsheet().getId()
PropertiesService.getScriptProperties().setProperties({ spreadsheet_id: sheetId });
return sheetId;
}
/*
* Returns the spreadsheet ID as defined in script properties
*/
function getSpreadsheetId() {
return PropertiesService.getScriptProperties().getProperty('spreadsheet_id');
}
/*
* Lookup a text value by the regular expression column and compute the response value
*
* @param { String } spreadsheet_id - pass null to use active sheet or id from properties.
* @param { String } text - The text to use for computation.
* @param { Object } vars - Substitution variables ( e.g. { key: "subthis" } would
* replace occurrences of %key% with subthis)
*/
function lookupAndCompute(spreadsheet_id, text, vars) {
var ss = spreadsheet_id
? SpreadsheetApp.openById(spreadsheet_id) //if we pass it in just use it
: (
//get the active sheet or the one defined in script properties
SpreadsheetApp.getActiveSpreadsheet() || SpreadsheetApp.openById(getSpreadsheetId())
)
, 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, key
;
for(var i=1; i<=rows.getNumRows(); i++) {
re = rows.getCell(i, 1).getValue();
//skip empty rows
if(!re) {
continue;
}
//ignore regex errors
try {
re = new RegExp(re, "i");
reMatches = text.match(re);
} catch(e) {
continue;
}
if(reMatches) {
//make sure formatting is right
row = conv.convertRange(rows)[i-1];
for(var i=0; i<reMatches.length; i++) {
//handle unicode quotation marks
match = reMatches[i]
.replace(/[\u2018\u2019]/g, "'")
.replace(/[\u201C\u201D]/g, '"')
.replace(/"/g, '""')
;
row[1] = row[1].replace(new RegExp('\\\\'+i,'g'), match);
}
//replace dynamic variables
for(var key in vars) {
if(vars.hasOwnProperty(key)) {
val = vars[key];
for(var j=0; j<row.length; j++) {
if(row[j])
row[j] = row[j].replace(new RegExp('%'+key+'%', 'ig'), val);
}
}
}
//add some relevant details text, datetime
row.unshift(text);
row.unshift((new Date()).toUTCString());
idx = logs.appendRow(row).getLastRow();
//again make sure formatting is right
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