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.

image

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

}

}
5 Likes

Nice! And just to make sure…

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

1 Like

Uhm. What is a webhook? :sweat_smile:

I just use google sheets and appscript.

1 Like

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.)

2 Likes

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.

1 Like

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

1 Like