Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
Can you find what I am doing wrong in this Google App Script for pulling weather data based on location in sheet and placing it in other sheet?

I am working on a project overview/scheduling sheet that I can keep track of all my jobs and make scheduling changes based off weather forecasts in the job-specific location.

I have successfully been able to pull in the weather data through an API from my location sheet, with one location, and place that pulled data into my report sheet.

 
 const key = "99071c9629eded162bb1a2e8b7843888"
 const ss = SpreadsheetApp.getActiveSpreadsheet()
 const wsLocation = ss.getSheetByName("Location")
 const wsLiveData = ss.getSheetByName("Live Data")
 const wsHistory = ss.getSheetByName("History")
 
 const location = wsLocation.getRange("A2").getValue()
 const units = wsLocation.getRange("B2").getValue()
 
 const highCellHa = wsLiveData.getRange("B2")
 const lowCellHa = wsLiveData.getRange("C2")
 
 let apiURL = "https://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${key}&units=${units}"
 
 const resText = UrlFetchApp.fetch(apiURL).getContentText()
 const resJSON = JSON.parse(resText)
    console.log(resJSON["main"])
 
 const tempMin = resJSON["main"]["temp_min"]
 const tempMax = resJSON["main"]["temp_max"]
 highCell.setValue(tempMax)
 lowCell.setValue(tempMin)
 
 wsHistory.appendRow([new Date(),tempMax,tempMin]) 
 }
}

I need to be able to have it pull the weather data from multiple locations however and then place each respective locations data into the report sheet in its own spot. I have tried to use a for loop, but to no avail. Any ideas are welcomed and appreciated!!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In your script, how about the following modification?

Modified script:

function myFunction() {
  const key = "###"; // Please set your key.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const wsLocation = ss.getSheetByName("Location");
  const wsHistory = ss.getSheetByName("History")
  const location = wsLocation.getRange("A2:A" + wsLocation.getLastRow()).getValues();
  const units = wsLocation.getRange("B2").getValue();
  const date = new Date();
  const reqs = location.map(([a]) => ({url: `https://api.openweathermap.org/data/2.5/weather?q=${a}&appid=${key}&units=${units}`, muteHttpExceptions: true}));
  const res = UrlFetchApp.fetchAll(reqs);
  const values = res.map(r => {
    if (r.getResponseCode() == 200) {
      const temp = JSON.parse(r.getContentText());
      return [date, temp.main.temp_min, temp.main.temp_max];
    }
    return [date, null, null];
  });
  wsHistory.getRange(wsHistory.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
  • In this modification, it supposes that the values of location are put to the column "A" of "Location" sheet. Please be careful about this. And, the retrieved values are put to the History sheet.

  • In your script, highCell and lowCell are not declared. Please be careful about this.

References:

  • fetchAll(requests)
  • map()
about 4 years ago · Juan Pablo Isaza Relatório

0

The apiURL has to be enclosed in back ticks to use template literals ${}

Try this:

function lfunko() {
  const key = "99071c9629eded162bb1a2e8b7843888"
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const wsLocation = ss.getSheetByName("Location")
  const wsLiveData = ss.getSheetByName("Live Data")
  const wsHistory = ss.getSheetByName("History")
  const locations = wsLocation.getRange(2, 1, wslocation.getLastRow() - 1).getValues()
  const units = wsLocation.getRange("B2").getValue()
  const highCellHa = wsLiveData.getRange("B2")
  const lowCellHa = wsLiveData.getRange("C2");
  locations.forEach(l => {
    let apiURL = `https://api.openweathermap.org/data/2.5/weather?q=${l}&appid=${key}&units=${units}`;
    let resText = UrlFetchApp.fetch(apiURL).getContentText()
    let resJSON = JSON.parse(resText)
    let tempMin = resJSON["main"]["temp_min"]
    let tempMax = resJSON["main"]["temp_max"]
    highCell.setValue(tempMax);
    lowCell.setValue(tempMin);
    wsHistory.appendRow([l, `Max: ${tempMax} Min: ${tempMin}`]);
  });
}

There may need to be other data in locations but since you have not provided your data structure there is no way to know that

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda