Skip to content

google apps script for importing json data into spreadsheet

sfsw-brown-solar-charger edited this page Mar 8, 2021 · 2 revisions
function myFunction() {
  var response = UrlFetchApp.fetch("https://brown-solar-charger-default-rtdb.firebaseio.com/data/liveData.json");
  var data=JSON.parse(response.getContentText()); //pull data from the database

  var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("log");
  var lastRow=sheet.getLastRow();

  var dataline=[[
  data["time"],
  data["bat%"],
  data["WGen"],
  data["WUse"],
  data["Available"],
  data["cumulativeWhGen"]]];
  
  if(sheet.getRange(lastRow,1,1,1).getValue()!=data["time"]){
    sheet.getRange(lastRow+1,1,1,6).setValues(dataline);
  }
}