/** * Imports JSON data to your spreadsheet Ex: =GETDIVIDEND("https://cloud.iexapis.com/stable/stock/AAPL/dividends/1y?token=xxxx") * @param url URL of your JSON data as string alongwith the stock ticker and your personalized security token from IEX */ function GETDIVIDEND(url){ try { var res = UrlFetchApp.fetch(url); var content = res.getContentText(); var json = JSON.parse(content); var recentDivi = json[0].amount; var freq = json[0].frequency; if(freq =="Quarterly"){ return recentDivi*4; }else if(freq == "Semi-Annual"){ return recentDivi*2; }else if(freq == "Monthly"){ return recentDivi*12; } return 0; } catch (err) { return 0; } } /** * Imports JSON data to your spreadsheet Ex: =if(isblank(A2),,GETDIVIDEND2($A2,"xxxx")) * @param symbl your stock ticker * @parm key your personalized free iex cloud token */ function GETDIVIDEND2(symbl,key){ try { var recentDivi; var freq; urlnxt="https://cloud.iexapis.com/stable/stock/"+symbl+"/dividends/next?token="+key; var resnxt = UrlFetchApp.fetch(urlnxt); Logger.log(resnxt); Logger.log(resnxt.length); if(resnxt=="[]"){ var url6m="https://cloud.iexapis.com/stable/stock/"+symbl+"/dividends/6m?token="+key; var res6m=UrlFetchApp.fetch(url6m); if(res6m != "[]"){ var cont6m = res6m.getContentText(); var json6m = JSON.parse(cont6m); recentDivi = json6m[0].amount; freq = json6m[0].frequency; }else{ var urldef="https://cloud.iexapis.com/stable/stock/"+symbl+"/dividends?token="+key; var resdef=UrlFetchApp.fetch(urldef); if(resdef != "[]"){ var contdef = resdef.getContentText(); var jsondef = JSON.parse(contdef); recentDivi = jsondef[0].amount; freq = jsondef[0].frequency; } } }else{ var content = resnxt.getContentText(); var json = JSON.parse(content); recentDivi = json.amount; freq = json.frequency; } if(freq.toLowerCase() =="quarterly"){ return recentDivi*4; }else if(freq.toLowerCase() == "semi-annual"){ return recentDivi*2; }else if(freq.toLowerCase() == "monthly"){ return recentDivi*12; } return 0; } catch (err) { return 0; } }