Skip to content

Instantly share code, notes, and snippets.

@knowtheory
Created January 13, 2015 14:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save knowtheory/d52fc61f21ce1f047e27 to your computer and use it in GitHub Desktop.
Save knowtheory/d52fc61f21ce1f047e27 to your computer and use it in GitHub Desktop.
Google Drive has an in document javascript API which allows you to do powerful and crazy things like build a web scraper that runs on a timer. When in a google spreadsheet, go to the tools menu and select "script editor".
// Fetch and append the current temperature
function fetchAndAppendWeather() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
// All the actual work is done in the top row.
// We get the link stored in cell A1 (say http://www.wunderground.com/cgi-bin/findweather/getForecast?query=02217 )
var weatherLink = sheet.getRange("A1").getValue();
// we set a formula importing a targeted portion of the web page
// specified by the link we stored in cell A1
var temperature = sheet.getRange("C1").setFormula("importxml(\""+weatherLink+"\",\"//div[@id='curTemp']/*/span[@class='wx-value']\")");
// after we have fetched the current temperature
// insert a new row, where we'll store our new entry
sheet.insertRowAfter(1);
// stick the current time and the value of the temperature
// we've extracted into our newly created row.
sheet.getRange("B2").setValue(new Date());
sheet.getRange("C2").setValue(temperature.getValue());
}
function dummyFunction() {
// This is a function that doesn't do anything
// but can demonstrate how triggers are set up
// under the 'Resources' menu.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment