Weather Google apps script with imported JSON from Openweathermap API

Ratatosk
Participant V

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,000
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