Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 在Google工作表中导入JSON减少API连接?_Google Apps Script_Google Sheets_Google Sheets Api - Fatal编程技术网

Google apps script 在Google工作表中导入JSON减少API连接?

Google apps script 在Google工作表中导入JSON减少API连接?,google-apps-script,google-sheets,google-sheets-api,Google Apps Script,Google Sheets,Google Sheets Api,我在这里提出了另一个问题: 看来我调用的API太多了 我的脚本一次调用了23次API 我的脚本是这样的: var ss = SpreadsheetApp.getActiveSheet(); //Creates a menu called Crypto. function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu('Crypto') .addItem('Update Prices','update

我在这里提出了另一个问题:

看来我调用的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);


}
因为api限制为100个代码,所以我必须创建4个。我使用的是IP的V2,我使用的是ID来获得正确的货币。那是股票代码后面的数字。我必须使用4下载,因为货币的最高排名是316


在不调用太多api的情况下优化此脚本的最佳方法是什么?

我已经减少了脚本。它现在有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);


}

每次通话中BTC和EUR之间的转换是否都在变化?看起来你只需要找出一次汇率,然后取一次你的股票价格,然后用数学而不是HTTP请求来获取其他货币的价格。。。此外,考虑重写使用IpjJSON使用<代码> URLFETCHAPPSP.FETCHALWAL/<代码>。而且,仅仅因为你不熟悉数组,并不意味着你可以随意忽略它们——你不是。学习语言是你的责任。
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);


}