Weather Google apps script with imported JSON from Openweathermap API

Learned a lot about importing JSON today to apps script, and thought Iโ€™d share my progress so far. Still a lot to do. My goal is to make a forecast of 5 regions in my city.

3X_a_9_a9f8ca0ae62087a00f217709e1e052cf26a474c5.png

Hereโ€™s the code:

//Made by Ratatosk (Appsheet user) Feel free to use it as you wish!

function getWeather() { getBergen3hData() }

function  getBergen3hData() {
  
  var url = "api.openweathermap.org/data/2.5/forecast?q=Bergen,NO&lang=no&appid=ENTERYOURKEYHERE&units=metric" // URL for weather. API key needed(free)
  var response = UrlFetchApp.fetch(url, {method: 'GET',headers: {'Content-Type': 'application/json','Accept': 'application/json'} });
   
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  var weather = SpreadsheetApp.getActive().getSheetByName('BergenVรฆr');
  var hoursperiods = 12; //how many 3 hour periods you extract from the forecast
  weather.getRange("A2:G1000").clearContent(); //deletes old data in sheet
   
 for (var i = 1; i < hoursperiods; i++) {
 
  weather.getRange('A'+(i+1)).setValue(data.list[i].dt_txt); //times
  weather.getRange('E'+(i+1)).setValue(data.city.name); //City name
  weather.getRange('B'+(i+1)).setValue(data.list[i].weather[0].main); //one word description
  weather.getRange('C'+(i+1)).setValue(data.list[i].weather[0].description); //description
  weather.getRange('D'+(i+1)).setValue("http://openweathermap.org/img/wn/"+data.list[i].weather[0].icon+"@4x.png"); //icon
  weather.getRange('F'+(i+1)).setValue(data.list[i].main.temp+"ยฐC"); //temp
//weather.getRange('B'+(i+1)).setValue(LanguageApp.translate(JSON.stringify(data.list[i].weather[0].main), 'en', 'no')); //How to translate language if needed

}

}
8 6 2,051
6 REPLIES 6

Nice! And just to make sureโ€ฆ

The code you presented goes into the Body template of a Webhook Workflow? Is that right?

Uhm. What is a webhook?

I just use google sheets and appscript.

Oh! LOL! Ok! I knew is wasnโ€™t JSON code (its actually Javascript parsing JSON data) but thought you had found a clever way to use it in AppSheet.

Webhooks are a way to interact with other services/APIโ€™s (application programming interface). They can be used to send and retrieve data from other services, such as weather information. (Though I am not sure we can retrieve data just yet. I have heard it is in the works to be available soon.)

Haha! Yeah my title is not very good.

Would be great if the app could receive data โ€œliveโ€.

Now Apigee as data source is in place although it is beta . We donโ€™t need to type any script any longer to do the same stuffs.

Iโ€™m aiming at the premium subscription plan when Iโ€™m ready to deploy my first app, so no apigee for me.

Top Labels in this Space