0
votes

Function (https://blog.coingecko.com/import-coingecko-cryptocurrency-data-into-google-sheets/):

/**
* Imports JSON data to your spreadsheet
* @param url URL of your JSON data as string
* @param xpath simplified xpath as string
* @customfunction
*/
function IMPORTJSON(url,xpath){

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"; 
}
}

Google sheet formula:

=importJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin","0.current_price")

I want to replace the word bitcoin with value from another cell, say C3.

Here is my code that doesn't work (gives formula parse error):

=importJSON(=CONCATENATE("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=",$C3),"0.current_price")

1

1 Answers

2
votes

try:

=IMPORTJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids="&$C3), 
 "0.current_price")