首页 > 解决方案 > 使用自定义脚本作为公式时如何使用单元格的值?

问题描述

功能(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"; 
}
}

谷歌表格公式:

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

我想用另一个单元格中的值替换比特币这个词,比如 C3。

这是我的代码不起作用(给出公式解析错误):

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

标签: google-sheetsgoogle-sheets-formula

解决方案


尝试:

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

推荐阅读