Weather Data to google spread sheet

  • 1
  • Problem
  • Updated 4 years ago
  • (Edited)
I am trying to use a script from within google spreadsheet to populate weather data that I an use in other apps and my webpage with highcharts. My coding skills are weak and I in the process of educating myself. I wrote this code based on an example I found but it doesn't run. I get the following TypeError: Cannot read property "simpleforecast" from undefined. (line 21, file "Code")
My main goal is to log temp,humid,windspeed,direction.


/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* <a href="https://developers.google.com/apps-script/service_spreadsheet" rel="nofollow">https://developers.google.com/apps-sc...</a>
*/
var cDays = 0, cHigh = 1, cLow = 2, cConditions = 3, cSparkline = 4;
var nClols = 5;

function getTemp() {
var url = '<a href="http://api.wunderground.com/api/************/forecast/q/ON/Belleville.json" rel="nofollow">http://api.wunderground.com/api/*****...</a>&#x27;;
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var response = UrlFetchApp.fetch(url);

var contentText = response.getContentText();
var forecast = Utilities.jsonParse(contentText);
var todaysForecast = forecast.forecast.simpleforecast.forecastday[0];

var high = todaysForecast.high.fahrenheit;
var low = todaysForecast.low.fahrenheit;
var conditions = todaysForecast.conditions;
sheet.insertRowAfter(1);

var range = sheet.getRange(2,1,1, nCols);
var row = range.getValues()[0];
row[cDay] = new Date ();
row[cHigh] = high; row[cLow] = low;
row[cConditions] = conditions;
var nRows= numRows >= 10 ? 10 :numRows;
row[cSparkline] = "=SPARKLINE(R[0]C[-3]:R[" + (nRows-1) + "]C[-3])";
range.setValues([row]);

}
<code>
I figured if I could get this to work I could modify to get exactly what I want.
any advice,example or direction is greatly appreciated!
Thanks
Jeff in Ontario

Photo of cormad

cormad

  • 3 Posts
  • 0 Reply Likes

Posted 4 years ago

  • 1
Photo of Tim Roche

Tim Roche, Official Rep

  • 317 Posts
  • 28 Reply Likes
Hi Jeff,

You'll probably find more help in a forum related to the google spreadsheet API or SDK since it seems like the issue is with the
 UrlFetchApp class.

you can find info on that here
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
Photo of cormad

cormad

  • 3 Posts
  • 0 Reply Likes
Thanks Tim, 
                    I apologize for such bad code. It had a ton of typo's and other mistakes. I was actually able to get it working and thought it would be simple to switch to conditions rather than forecast. Now I get TypeError: Cannot read property "temp_f" from undefined. (line 24, file "Code")

<Code>
var cDay = 0, cTemp = 1, cRH = 2, cWinddir = 3, cWinddegree = 4;var cWindspeed = 5, cWindgust = 6;
var nCols = 6;



function getTemp() {
  var url = 'http://api.wunderground.com/api/**************/conditions/q/44.1,-77.3.json';
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var response = UrlFetchApp.fetch(url);
  
  var contentText = response.getContentText();
  var conditions = JSON.parse(contentText);
  var todaysConditions = conditions.conditions;
  
  var temp = todaysConditions.temp_f;
  var rh = todaysConditions.relative_humidity;
  var winddir = todaysConditions.wind_dir;
  var winddegree = todaysConditions.wind_degrees;
  var windspeed = todaysConditions.wind_mph;
  var windgust = todaysConditions.wind_gust.mph;
  sheet.insertRowAfter(1);
  
  var range = sheet.getRange(2,1,1, nCols);
  var row = range.getValues()[0];
  row[cDay] = new Date ();
  row[cTemp] = temp; 
  row[cRH] = rh;
  row[cWinddir] = winddir;
  row[cWinddegree] = winddegree;
  row[cWindspeed] = windspeed;
  row[cWindgust] = windgust;
  
  range.setValues([row]);
  
}
<Code>
I underlined line 24, I will search your link also. I am so close :).

Jeff 
(Edited)
Photo of Tim Roche

Tim Roche, Official Rep

  • 317 Posts
  • 28 Reply Likes
this line
var todaysConditions = conditions.conditions;
should probably read

 var todaysConditions = conditions.current_observation

there is no conditions object in that API call
Photo of cormad

cormad

  • 3 Posts
  • 0 Reply Likes
Thanks Time I got it working. :) 
Is there a way to call a single data point from one line of code? 
example 
<div>
get*********************************************************temp_f</div>
Excuse my lack of knowledge, I learn as I go