-
-
Save sebolio/d06e5a0229efa9260b368cdfb1e9e0a5 to your computer and use it in GitHub Desktop.
usar datos de apis en google spreadsheet
This file contains 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
//-- IMPORTANTE -- | |
// Este script usa la celda "A1" para saber que tiene que recargar las APIs, | |
// por lo tanto esa celda debe reservarse para este script, y ojalá de color blanco para no molestar | |
/** | |
* Importa un dato de una API JSON a una celda, usar "," o ";" segun idioma. | |
* Uso: =IMPORTJSON("http://api.pagina.com";"personas/juan/edad"; $A$1) | |
* @param url URL de la API | |
* @param xpath Ruta de la API ("personas/juan/edad") | |
* @customfunction | |
*/ | |
function IMPORTJSON(url,xpath){ | |
SpreadsheetApp.flush() | |
try{ | |
// /rates/EUR | |
var res = UrlFetchApp.fetch(url); | |
var content = res.getContentText(); | |
var json = JSON.parse(content); | |
var patharray = xpath.split("/"); | |
//Logger.log(patharray); | |
for(var i=0;i<patharray.length;i++){ | |
json = json[patharray[i]]; | |
} | |
//Logger.log(typeof(json)); | |
if(typeof(json) === "undefined"){ | |
return "Node Not Available"; | |
} else if(typeof(json) === "object"){ | |
var tempArr = []; | |
for(var obj in json){ | |
tempArr.push([obj,json[obj]]); | |
} | |
return tempArr; | |
} else if(typeof(json) !== "object") { | |
return json; | |
} | |
} | |
catch(err){ | |
return "Error getting data"; | |
} | |
} | |
// AÑADIR MENU PARA RECARGAR APIS (y ejecutar al inicio) | |
function recargarAPIs() { SpreadsheetApp.getActiveSpreadsheet().getRange('A1').setValue(new Date().getTime()); } | |
function onOpen() { | |
SpreadsheetApp.getActiveSpreadsheet().addMenu("Recargar APIs", [{ | |
name : "Dale papi", | |
functionName : "recargarAPIs", | |
}]); | |
recargarAPIs(); | |
}; |
Author
sebolio
commented
Sep 13, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment