I made an Excel document in Google Sheets. I have it set up to automatically pull the weather from the internet using =IMPORTXML. We work in 2 different towns and I would like the weather to pull for the town we are in at the moment. So basically if I choose โSitkaโ it populates the Sitka weather, if I choose โKetchikanโ I get Ketchikan weather. Is this possible?
There are a bunch of free weather REST APIs on the internet that you can use. I may advise to construct a time triggered script which may run in hourly intervals and update weather data in a gSheet. You can do that for multiple cities and use table grouping to present that data in the app.
This is the one that Iโm using mostly.
GOOGLE SPREADSHEET SETUP
Sheet could be like this:
Column Name | Data Type | Key? | Initial Value | AppFormula |
---|---|---|---|---|
ID | Text | - | - | |
TIMESTAMP | Datetime | - | - | - |
CITY | Text | - | - | - |
ICON | Image | - | - | - |
DESC | Text | - | - | - |
TEMP | Number | - | - | - |
FEELS_LIKE | Number | - | - | - |
WIND_SPEED | Number | - | - | - |
WIND_DEGREE | Number | - | - | - |
WIND_DIR | Text | - | - | - |
PRESSURE | Number (thousands off) | - | - | - |
PRECIPITATION | Number | - | - | - |
HUMIDITY | Number | - | - | - |
UV_INDEX | Number | - | - | - |
VISIBILITY | Number | - | - | - |
GOOGLE APPS SCRIPT
const WEATHER_API_KEY = "YOUR_API_KEY_HERE";
const SHEETID = "YOUR_SPREADSHEET_ID_HERE";
const SHEETNAME = "YOUR_SHEET_NAME_HERE";
const myCities = ["Sitka" , "Ketchikan"]; //You can add more cities in this array if needed
function getWeatherData(queryCity) {
var baseURL = "http://api.weatherstack.com/current?access_key="+WEATHER_API_KEY+"&query="+encodeURIComponent(queryCity);
var data = UrlFetchApp.fetch(baseURL);
if (data.getResponseCode() === 200) {
var response = JSON.parse(data.getContentText())
var weatherArray = [UUID(),response.location.localtime,response.location.name,response.current.weather_icons[0],response.current.weather_descriptions[0],response.current.temperature,
response.current.feelslike,response.current.wind_speed,response.current.wind_degree,response.current.wind_dir,response.current.pressure,response.current.precip,
response.current.humidity,response.current.uv_index,response.current.visibility];
SpreadsheetApp.openById(SHEETID).getSheetByName(SHEETNAME).appendRow(weatherArray)
}
}
function UUID(){
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
var uuid = [], rnd = Math.random, r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4'; // version 4
for (var i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | rnd()*16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
}
}
return uuid.join('').split("-")[0];
}
function getMyWeather(){
myCities.forEach(function(city){
getWeatherData(city)
})
}
You can then set an hourly (or any desired interval) time trigger for the getMyWeather() function. On how to set a script trigger, you can check below post.
DEMO APP
Thanks @LeventK ! This solution is excellent! Works great! This will certainly save lots of Zaps on my Zapier account!
Youโre welcome.
I need some help utilizing this new weather table within my app. My weather table is built exactly as @LeventK describes in this great post. I set the App Script trigger to run hourly rather than on a spreadsheet update/edit. This is because there are many changes and updates in my app which would result in many similar unnecessary weather rows. I have added the table (โweatherโ) to my app and set the column types appropriately. My question is, how do you set up references to tables that are updated outside of Appsheet input?
In the fishing app Iโm working on I have a table called โTripsโ This table includes a column TripNumber which is a unique primary key, The table also has StartDate, StartTime, StopDate, StopTime and Status. Status is a virtual column that changes from โFishingโ to โCompleteโ once the trip ends. I also concatenated the StartDate/Time and StopDate/Time in virtual columns. I would like to set up a 1 to many relationship between โTripsโ and the โWeatherโ table. I canโt figure out how to create a reference between these tables. I would like to pass the TripNumber to the weather table when the LocalTime field within the โweatherโ table is between the StartDate/Time and StopDate/Time and the trip status is โFishingโ. Since I am new to appsheet and app script I was hoping to do this within appsheet where I have slightly more knowledge. Any help would be great. Thank you!
@GW375
As your TripNumber is the key column to your table, you cannot convert that column to a REF type. However, you can create a List type VC and name it i.e. TripWeather and construct a FILTER() expression like:
FILTER(
"WeatherTableName",
(AND(
[TIMESTAMP] >= [_THISROW].[START DATETIME], //StartDate/Time VC
[TIMESTAMP] <= [_THISROW].[STOP DATETIME] //StopDate/Time VC
[CITY] = [_THISROW].[LOCATION] // Location Column
))
)
And you can set a SHOW_IF expression for this VC:
โFishingโ = [STATUS]
Check below update in my demo app. I have also set an Editable_If for the Start/End Datetime columns (so that they canโt be edited if the [Status] is Complete) and assign a Quick Edit mode on them in the Detail View, so that the Virtual Weather Data list became dynamic directly from the detail view. If you change the dates (as per mock data), the list will re-arrange itself.
REMINDER
The WeatherStack API is free only for 1000 API calls per month. With a timely trigger itโs 24 calls per day and 741 calls per month. However even bad API calls or null returns are also counted as a legit call, I may advise following your API usage from their dashboard > https://weatherstack.com/usage
Thanks @LeventK!! Your solution worked perfectly and your example app was extremely useful! I also understand the filter expression much better now!
Thanks for the reminder about the 1000 API calls with WeatherStack. So far I have been very impressed with the reliability. I havenโt had any null returns yet.
Youโre welcome @GW375, my pleasure to be helped of. Happy New Year!
Hello! Great contribution LeventK! I will apply it in an app! Today and for this specific case of general or non-specific api queries, you can add the extension or plugin to the google spreadsheet, called API Connector. There configure the api queries and schedule them. The most important thing is that you don't need to write a code! Greetings ๐
User | Count |
---|---|
35 | |
30 | |
30 | |
20 | |
18 |