我在这里提出了另一个问题:
我似乎在呼唤API。
我的脚本一次性调用API的23倍。
这就是我的脚本的样子:
var ss = SpreadsheetApp.getActiveSheet();
//Creates a menu called Crypto.
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Crypto')
.addItem('Update Prices','updatePrices')
.addItem('Update Sheet','updateSheet')
.addToUi();
}
//Copy cells.
function copyCell() {
ss.getRange("D2:D13").copyTo(ss.getRange("E2"), {contentsOnly:true});
ss.getRange("B2:B13").copyTo(ss.getRange("G2"), {contentsOnly:true});
ss.getRange("M1").copyTo(ss.getRange("M2"), {contentsOnly:true});
ss.getRange("M5").copyTo(ss.getRange("M6"), {contentsOnly:true});
}
/**
* Imports JSON data to your spreadsheet Ex: IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1/?convert=EUR","data/quotes/EUR/price")
* @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";
}
}
//Importing CMC Data into sheet
function importCMC() {
var btc_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1/?convert=EUR","data/quotes/EUR/price");
var btc_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B2").setValue([btc_eur]);
ss.getRange("H2").setValue([btc_btc]);
var bhc_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1831/?convert=EUR","data/quotes/EUR/price");
var bhc_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1831/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B3").setValue([bhc_eur]);
ss.getRange("H3").setValue([bhc_btc]);
var ltc_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR","data/quotes/EUR/price");
var ltc_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B4").setValue([ltc_eur]);
ss.getRange("H4").setValue([ltc_btc]);
var ada_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2010/?convert=EUR","data/quotes/EUR/price");
var ada_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2010/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B5").setValue([ada_eur]);
ss.getRange("H5").setValue([ada_btc]);
var trx_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1958/?convert=EUR","data/quotes/EUR/price");
var trx_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1958/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B6").setValue([trx_eur]);
ss.getRange("H6").setValue([trx_btc]);
var neo_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1376/?convert=EUR","data/quotes/EUR/price");
var neo_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1376/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B7").setValue([neo_eur]);
ss.getRange("H7").setValue([neo_btc]);
var ont_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2566/?convert=EUR","data/quotes/EUR/price");
var ont_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2566/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B8").setValue([ont_eur]);
ss.getRange("H8").setValue([ont_btc]);
var gas_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1785/?convert=EUR","data/quotes/EUR/price");
var gas_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/1785/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B9").setValue([gas_eur]);
ss.getRange("H9").setValue([gas_btc]);
var enj_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2130/?convert=EUR","data/quotes/EUR/price");
var enj_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2130/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B10").setValue([enj_eur]);
ss.getRange("H10").setValue([enj_btc]);
var tky_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2507/?convert=EUR","data/quotes/EUR/price");
var tky_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2507/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B11").setValue([tky_eur]);
ss.getRange("H11").setValue([tky_btc]);
var uuu_eur = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2645/?convert=EUR","data/quotes/EUR/price");
var uuu_btc = IMPORTJSON("https://api.coinmarketcap.com/v2/ticker/2645/?convert=BTC","data/quotes/BTC/price");
ss.getRange("B12").setValue([uuu_eur]);
ss.getRange("H12").setValue([uuu_btc]);
var cmc_usd = IMPORTJSON("https://api.coinmarketcap.com/v2/global/","data/quotes/USD/total_market_cap");
ss.getRange("M1").setValue([cmc_usd]);
}
//Getting euro prices from Coincapmarket and place them in specific cells.
function updatePrices() {
copyCell();
importCMC();
//Get current date
var now = new Date();
ss.getRange('F1').setValue(now)
}
//Getting euro prices from Coincapmarket and place them in specific cells.
function updateSheet() {
copyCell();
importCMC();
//Get date.
var now = new Date();
ss.getRange('F1').setValue(now)
ss.getRange("F1").copyTo((ss.getRange(ss.getRange("A18:A111").getValues().filter(String).length + 18, 1)), {contentsOnly:true});
ss.getRange("D15").copyTo((ss.getRange(ss.getRange("B18:B111").getValues().filter(String).length + 18, 2)), {contentsOnly:true});
//Copy the formula's from row 19 to last filled cell in A and B.
var row = 19;
CopyFormulasDown.copyFormulasDown(ss, row);
}
CMC的V2 api的问题是必须使用其他URL检查BTC价格。我不是数组中的英雄。遗憾。
我正在考虑将ticker API拉四次,然后找到正确的信息
1. https://api.coinmarketcap.com/v2/ticker/?convert=EUR
2. https://api.coinmarketcap.com/v2/ticker/?convert=EUR&start=101
3. https://api.coinmarketcap.com/v2/ticker/?convert=EUR&start=201
4. https://api.coinmarketcap.com/v2/ticker/?convert=EUR&start=301
因为api限制为100个代码我必须创建4.我使用IP的V2并且我使用ID来获得正确的货币。这是股票代码背后的数字。我必须使用4次下载,因为最高等级的货币是316。
通过调用多个api来优化此脚本的最佳方法是什么?
答案 0 :(得分:0)
我减少了我的剧本。它现在有4个api电话。对于coinmarketcap来说,有时候还有很多。
var ss = SpreadsheetApp.getActiveSheet();
//Creates a menu called Crypto.
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Crypto')
.addItem('Update Prices','updatePrices')
.addItem('Update Sheet','updateSheet')
.addToUi();
}
//Copy cells.
function copyCell() {
ss.getRange("D2:D13").copyTo(ss.getRange("E2"), {contentsOnly:true});
ss.getRange("B2:B13").copyTo(ss.getRange("G2"), {contentsOnly:true});
ss.getRange("M1").copyTo(ss.getRange("M2"), {contentsOnly:true});
ss.getRange("M5").copyTo(ss.getRange("M6"), {contentsOnly:true});
}
//Importing CMC Data into sheet
function importCMC() {
var response = UrlFetchApp.fetch("https://api.coinmarketcap.com/v2/ticker/?convert=EUR");
var content = response.getContentText();
var json = JSON.parse(content);
var btc_eur = json["data"]["1"]["quotes"]["EUR"]["price"];
ss.getRange("B2").setValue([btc_eur]);
var bch_eur = json["data"]["1831"]["quotes"]["EUR"]["price"];
ss.getRange("B3").setValue([bch_eur]);
var ltc_eur = json["data"]["2"]["quotes"]["EUR"]["price"];
ss.getRange("B4").setValue([ltc_eur]);
var ada_eur = json["data"]["2010"]["quotes"]["EUR"]["price"];
ss.getRange("B5").setValue([ada_eur]);
var trx_eur = json["data"]["1958"]["quotes"]["EUR"]["price"];
ss.getRange("B6").setValue([trx_eur]);
var neo_eur = json["data"]["1376"]["quotes"]["EUR"]["price"];
ss.getRange("B7").setValue([neo_eur]);
var ont_eur = json["data"]["2566"]["quotes"]["EUR"]["price"];
ss.getRange("B8").setValue([ont_eur]);
var gas_eur = json["data"]["1785"]["quotes"]["EUR"]["price"];
ss.getRange("B9").setValue([gas_eur]);
Utilities.sleep(5000)
var response = UrlFetchApp.fetch("https://api.coinmarketcap.com/v2/ticker/?convert=EUR&start=101");
var content = response.getContentText();
var json = JSON.parse(content);
var enj_eur = json["data"]["2130"]["quotes"]["EUR"]["price"];
ss.getRange("B10").setValue([enj_eur]);
var tky_eur = json["data"]["2507"]["quotes"]["EUR"]["price"];
ss.getRange("B11").setValue([tky_eur]);
Utilities.sleep(5000)
var response = UrlFetchApp.fetch("https://api.coinmarketcap.com/v2/ticker/2645/?convert=EUR");
var content = response.getContentText();
var json = JSON.parse(content);
var uuu_eur = json["data"]["quotes"]["EUR"]["price"];
ss.getRange("B12").setValue([uuu_eur]);
Utilities.sleep(5000)
var response = UrlFetchApp.fetch("https://api.coinmarketcap.com/v2/global/");
var content = response.getContentText();
var json = JSON.parse(content);
var cmc_usd = json["data"]["quotes"]["USD"]["total_market_cap"];
ss.getRange("M1").setValue([cmc_usd]);
}
//Getting euro prices from Coincapmarket and place them in specific cells.
function updatePrices() {
copyCell();
importCMC();
//Get current date
var now = new Date();
ss.getRange('F1').setValue(now)
}
//Getting euro prices from Coincapmarket and place them in specific cells.
function updateSheet() {
copyCell();
importCMC();
//Get date.
var now = new Date();
ss.getRange('F1').setValue(now)
ss.getRange("F1").copyTo((ss.getRange(ss.getRange("A18:A111").getValues().filter(String).length + 18, 1)), {contentsOnly:true});
ss.getRange("D15").copyTo((ss.getRange(ss.getRange("B18:B111").getValues().filter(String).length + 18, 2)), {contentsOnly:true});
//Copy the formula's from row 19 to last filled cell in A and B.
var row = 19;
CopyFormulasDown.copyFormulasDown(ss, row);
}