在谷歌表格单元格上,我只想使用此公式获取当前的加密货币价格:
=ValueCrypto(A1)
我为 coinmarketcap 尝试了这个功能:
function ValueCrypto(crypto) {
var url = "https://api.coinmarketcap.com/v1/ticker/" + crypto + "/?convert=EUR";
var response = UrlFetchApp.fetch(url);
var data = JSON.parse(response.getContentText());
return data[0].price_eur;
}
该函数给我错误“我们不再在这里服务这个端点”
我也尝试更改端点并将我的 apy 密钥添加到函数中:
function ValueCrypto(crypto) {
var url = "pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=" + crypto
var requestOptions = {
method: 'GET',
uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
qs: {
start: 1,
limit: 5000,
convert: 'EUR'
},
headers: {
'X-CMC_PRO_API_KEY': 'myapikey'
},
json: true,
gzip: true
};
var response = UrlFetchApp.fetch(url);
var data = JSON.parse(response.getContentText());
return data[0].price_eur;
}
现在的错误是: 异常:http://pro-api.coinmarketcap.com 的请求失败返回代码 401。截断的服务器响应:{ “地位”: { "时间戳": "2021-01-02T11:31:39.880Z", “错误代码”:1002, "error_message": "API 密钥丢失。", ...(使用 muteHttpExceptions 选项检查完整响应)
答案 0 :(得分:0)
您的代码似乎没有使用定义 API 密钥的 requestOptions
。尝试像这样将其传递给 UrlFetchApp
:
var response = UrlFetchApp.fetch(url, requestOptions);
请参阅 UriFetchApp 的文档。
答案 1 :(得分:0)
看到CoinMarketCap API的Quick Start Guide官方文档,作为示例curl命令,发现如下示例curl命令。
curl -H "X-CMC_PRO_API_KEY: b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c" -H "Accept: application/json" -d "start=1&limit=5000&convert=USD" -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest
在这种情况下,需要使用start=1&limit=5000&convert=USD
的数据作为查询参数。
当我看到您的脚本时,似乎使用了 symbol, start, limit, convert
的值。在这种情况下,请使用 symbol=${crypto}&start=1&limit=5000&convert=EUR
之类的值。并且,headers: {'X-CMC_PRO_API_KEY': 'myapikey'}
可用于 params
的 fetch(url, params)
。
网址似乎是 https://###
。
当以上几点反映到你的脚本中时,它变成如下。
在使用之前,请使用您的 API 密钥设置 'X-CMC_PRO_API_KEY': 'myapikey'
。
function ValueCrypto(crypto) {
// This is from https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
String.prototype.addQuery = function(obj) {
return this + Object.keys(obj).reduce(function(p, e, i) {
return p + (i == 0 ? "?" : "&") +
(Array.isArray(obj[e]) ? obj[e].reduce(function(str, f, j) {
return str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : "")
},"") : e + "=" + encodeURIComponent(obj[e]));
},"");
}
var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest"; // or var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest";
var query = {
symbol: crypto,
start: 1,
limit: 5000,
convert: 'EUR'
};
var endpoint = url.addQuery(query); // <--- https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?symbol=###&start=1&limit=5000&convert=EUR
var response = UrlFetchApp.fetch(endpoint, {headers: {'X-CMC_PRO_API_KEY': 'myapikey', 'Accept': 'application/json'}});
return response.getContentText();
}
var url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest";
。从官方文档来看,我认为data[0].price_eur
可能是undefined
。因为 data[0].price_eur
对 api.coinmarketcap.com/v1/ticker
有用。所以在这个示例脚本中,我返回了 response.getContentText()
。当您将 =ValueCrypto(A1)
放入单元格时,可以看到返回的值。从这个值,你能不能显示出样本结果值和你想要的输出值?为此,我想对其进行修改。
当您的 API 密钥无效时,会发生错误。请注意这一点。
答案 2 :(得分:0)
这些对我有用
function fetchAll() {
const apiKey = 'xxxxxxx-xxxxxxx-xxxxxxx-xxxxxxx-xxx'
fetchCoin({ crypto: "SAFEMOON", fiat: "CAD", firstCell: "B9", apiKey })
fetchCoin({ crypto: "SAFEMOON", fiat: "USD", firstCell: "B8", apiKey })
}
function fetchCoin({ crypto, fiat, firstCell, apiKey }) {
const ascii = firstCell[0].toLowerCase().charCodeAt(0)
try {
var options = {
headers: { 'X-CMC_PRO_API_KEY': apiKey }
}
var url = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${crypto}&convert=${fiat}`
var response = UrlFetchApp.fetch(url, options);
var res = JSON.parse(response);
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii - 1)}${firstCell[1]}`).setValue(fiat)
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 0)}${firstCell[1]}`).setValue(res.data[crypto].quote[fiat].price)
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 1)}${firstCell[1]}`).setValue(res.data[crypto].quote[fiat].percent_change_1h)
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 2)}${firstCell[1]}`).setValue(res.data[crypto].quote[fiat].percent_change_24h)
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 3)}${firstCell[1]}`).setValue(res.data[crypto].quote[fiat].percent_change_7d)
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 4)}${firstCell[1]}`).setValue(res.data[crypto].quote[fiat].percent_change_30d)
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 5)}${firstCell[1]}`).setValue(Utilities.formatDate(new Date(res.data[crypto].quote[fiat].last_updated), 'America/New_York', 'MMMM dd, yyyy HH:mm:ss Z'))
Logger.log({ url, ascii,res })
} catch (e) {
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii - 1)}${firstCell[1]}`).setValue("")
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 0)}${firstCell[1]}`).setValue(`Something is broke... ${e.message}`)
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 1)}${firstCell[1]}`).setValue("")
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 2)}${firstCell[1]}`).setValue("")
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 3)}${firstCell[1]}`).setValue("")
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 4)}${firstCell[1]}`).setValue("")
SpreadsheetApp.getActiveSpreadsheet().getRange(`${String.fromCharCode(ascii + 5)}${firstCell[1]}`).setValue("")
}
}