Created
May 29, 2019 04:15
-
-
Save jctosta/454db8e6aa1b2609fdd8f4e219fc8b42 to your computer and use it in GitHub Desktop.
A google script to log web service data in a drive sheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deg2rad(deg) { | |
return deg * (Math.PI / 180); | |
} | |
function getDistanceFromLatLongInKm(lat1, lon1, lat2, lon2) { | |
var R = 6371; // Raio do Planeta Terra em KM | |
var dLat = deg2rad(lat2 - lat1); | |
var dLon = deg2rad(lon2 - lon1); | |
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); | |
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
var d = R * c; | |
return d; | |
} | |
function myFunction() { | |
// Configurando os horários de funcionamento do script | |
var currentDate = new Date(); | |
var currentTime = parseInt(currentDate.getHours()); | |
var sleepingTime = currentTime > 21 || currentTime < 6; | |
var LATITUDE_A = -22.9686843; | |
var LONGITUDE_A = -43.3650985006356; | |
var LATITUDE_B = -23.0073558; | |
var LONGITUDE_B = -43.3129786 | |
// Obtendo a instância do script properties | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
// Inicializando o ID da base de dados | |
var CURRENT_ID = scriptProperties.getProperty("CURRENT_ID"); | |
if (CURRENT_ID === null) { | |
scriptProperties.setProperty("CURRENT_ID", 0); | |
} | |
if (!sleepingTime) { | |
Logger.log("Serão registradas apenas as posições entre 6 horas da manhã e 22 horas da noite."); | |
} else { | |
// Chamada de API para o webservice da van | |
var response = UrlFetchApp.fetch(""); | |
// Convertendo pra JSON | |
var apiData = JSON.parse(response.getContentText()); | |
var dataList = apiData.Dados; | |
// Obtendo o objeto da planilha associada | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
// Obtendo a folha pelo nome | |
var sheet = ss.getSheetByName('EVENTOS'); | |
for(var i = 0; i < dataList.length; i++) { | |
// Obtendo o ID atual | |
var CURRENT_ID = parseInt(scriptProperties.getProperty("CURRENT_ID")); | |
// Realizando geocode nos dados de latitude e longitude | |
var address = Maps.newGeocoder().reverseGeocode(dataList[i].Latitude, dataList[i].Longitude); | |
if (address.results.length > 0) { | |
var textualAddress = address.results[0].formatted_address; | |
} else { | |
var textualAddress = ''; | |
} | |
var eventDate = dataList[i].TextoDataTransmissao; | |
var rowContent = []; | |
rowContent.push(CURRENT_ID); | |
rowContent.push(dataList[i].IdVeiculo); | |
rowContent.push(dataList[i].Descricao); | |
rowContent.push(dataList[i].Latitude); | |
rowContent.push(dataList[i].Longitude); | |
rowContent.push(textualAddress); | |
rowContent.push(getDistanceFromLatLongInKm(dataList[i].Latitude, dataList[i].Longitude, LATITUDE_A, LONGITUDE_A)); | |
rowContent.push(getDistanceFromLatLongInKm(dataList[i].Latitude, dataList[i].Longitude, LATITUDE_B, LONGITUDE_B)); | |
rowContent.push(eventDate); | |
rowContent.push(dataList[i].Velocidade); | |
rowContent.push(dataList[i].Ignicao); | |
rowContent.push(dataList[i].IdEvento); | |
rowContent.push(dataList[i].DescricaoEvento); | |
// Adicionando os dados coletados ao final da planilha | |
sheet.appendRow(rowContent); | |
// Incrementando o ID e salvando no properties do documento | |
scriptProperties.setProperty("CURRENT_ID", CURRENT_ID + 1); | |
} | |
Logger.log('Dados salvos com sucesso na planilha'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment