Simple App Script weather data extraction loop API
Project detail
I am using Open Weather Map API to collect data on the locations of the different jobs I currently have open.
I have the script set-up to do it based off one location in the ‘location sheet’ at the moment and then set the extracted data into the ‘weather sheet’, but I need it to loop through all locations in the ‘location sheet’, extract the data for that specific location, then set the extracted data into a its specified range in the ‘weather sheet’.
Here is the script code I currently have:
function getCurrentData() {
//set constants for the API key from currentweather’s API call, the spreadsheet, and the tabs
const key = “99071c9629eded162bb1a2e8b7843888”
const ss = SpreadsheetApp.getActiveSpreadsheet()
const wsLocation = ss.getSheetByName(“Location”)
const wsLiveData = ss.getSheetByName(“Live Data”)
const wsHistory = ss.getSheetByName(“History”)
// Set the values for the API url’s variables which are based on whatever is selected from the google sheet’s range specified – location pulls the city and units pulls the temp type
const location = wsLocation.getRange(“A2”).getValue()
const units = wsLocation.getRange(“B2”).getValue()
// Set the location for where to store the data being pulled each time this function runs – so highcell is for the tempMax
const highCellHa = wsLiveData.getRange(“B2”)
const lowCellHa = wsLiveData.getRange(“C2”)
//Pulls the data in from the API using the variables set above – to add new variables into this URL
let apiURL = `https://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${key}&units=${units}`
//fetch the data from the API called above and convert it into JSON so we can use it in the spreadsheet. Data must be in JSON format to use in spreadsheet
const resText = UrlFetchApp.fetch(apiURL).getContentText()
const resJSON = JSON.parse(resText)
console.log(resJSON[“main”])
//Pull the min temp and max temp from the JSON data from the API and then set the cell values specified above for low/highCell to whatever is pulled
const tempMin = resJSON[“main”][“temp_min”]
const tempMax = resJSON[“main”][“temp_max”]
highCellHa.setValue(tempMax)
lowCellHa.setValue(tempMin)
// save the current data being pulled in by tempMax/tempMin to the History Tab for future reference
wsHistory.appendRow([new Date(),tempMax,tempMin])
}