Thursday, January 03, 2019

Pack the Umbrella? Adding Weather Data to Google Sheets

Suppose you're planning an epic two week vacation that has you zipping around the globe. Wouldn't it be ideal to see the weather forecast for each day of your trip in a central location? That would simplify packing, no?

I thought so, and so I built the following custom function in Google Sheets:

/**
 * Look up a weather related attribute in a specific location/date
 *
 * @param {attr} weather attribute to look up
 * @param {location} of the weather to look up
 * @param {date} of the weather forcase
 * @return weather value
 * @customfunction
 */
function weather(attr, location,date) {
  var url = "http://code.benjisimon.com/singular-weather-service/?" +
                                   "loc=" + encodeURIComponent(location) + "&" +
                                   "date=" + encodeURIComponent(date) + "&" +
                                   "attr=" + encodeURIComponent(attr);
 
  if(attr == "debug") {
    return url;
  } else {
    var response = UrlFetchApp.fetch(url);
    return response.getContentText();
  }
}

This function is powered by the Singular-Weather-Service, a super easy to use Weather API.

With this function defined, it's now possible to create a spreadsheet that looks like this:

You can see a live version of this spreadsheet here.

Recall that the Singular-Weather-Service handles future dates by taking a 5 year historic average. This means that as your trip approaches, the weather data turns from historic average into prediction and finally into historic data.

This may all seem a bit excessive. However, I've found that when planning a trip, be it a multi-city extravaganza or a weekend getaway, creating a spreadsheet to record flight, hotel, things-to-do and other trip details is quite useful. Being able to add live weather to this spreadsheet makes it even more so.

No comments:

Post a Comment