我在这里提出了另一个问题:
似乎我调用了很多API。
我的脚本一下子调用了23次API。
这是我的脚本的样子:
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的BTC的问题是,V2价格必须通过其他URL进行检查。我不是数组里的英雄。抱歉。
我在考虑把报价器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由于应用编程接口限制为100个股票,我必须创建4个。我使用IP的V2,并使用ID来获取正确的货币。这是自动收报机后面的数字。我必须使用4个下载,因为最高排名的货币是316。
在不调用太多api的情况下优化这个脚本的最好方法是什么?
发布于 2018-06-09 02:02:15
我已经缩减了我的脚本。它现在有4个api调用。对于硬币市场来说,有时还是太多了。
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);
}https://stackoverflow.com/questions/50577522
复制相似问题