Hey guys,
has someone experience with the openweathermap API?
I’ve already a script in google that gives me the weather data from the city I select in my app.
This workarround unfortunately is not key because I get the weather just after I added a new row.
So my questions is: how can I get the weather directly in the app by just selecting the City? Any Ideas?
This is the code i already produced for google:
When you wann use it, just change [KEY] to your API-Key and change the names of the spreadsheets:
function appendWeather() {
var Wetter = getWeatherData() // 1) this will execute the function getWeatherData and put the result in weatherData
saveRecordInSheet(Wetter) // 11) We use the result to save it using saveRecordInSheet function.
}
//This gets the date from the openweathermap api and parses it to a record.
function getWeatherData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var Stadt = sheet.getSheetValues(1,4,1,1); //cell with the value of the city
Logger.log(Stadt);
var url = “http://api.openweathermap.org/data/2.5/weather?q=” + Stadt + “,de&appid=[KEY]&units=metric”;
var response = UrlFetchApp.fetch(url, { // 4) call the URL
headers: {
‘Content-Type’: ‘application/json’, // 5) Make sure we use JSON as transport
‘Accept’: ‘application/json’
}
});
var json = response.getContentText(); // 6) Get the content of the API call (this gets the response as TEXT(string)
var Datum = JSON.parse(json); // 7) Parse the TEXT to JSON (make objects out of it)
var Wetter = Datum.weather[0].main // 😎 Get the weather variable
var Temperatur = Datum.main.temp // 9) get the temperature variable
return [new Date(), Wetter, Temperatur] // 10) add the date and Return them (as a record)
}
// Save a record in the spreadsheet
function saveRecordInSheet(record) {
var currentSheet = SpreadsheetApp.getActiveSheet(); // 12) This gets the current Sheet in
currentSheet.appendRow(record); // 13) Append the record at the bottom of the current Sheet
}
User | Count |
---|---|
16 | |
8 | |
6 | |
3 | |
2 |