I'm trying to use an array to get specific results from an API to display in a row in a spreadsheet using google script. This is a sample of what the API log looks like:
[20-06-07 22:38:53:839 BST] [ {
"symbol" : "AMZN",
"date" : "2020-03-31",
"currentRatio" : 1.07870933748165254,
"quickRatio" : 0.842142238837801593,
},{...
The code written below seems to work fine until the forEach array, where I get this error:
"TypeError: results.forEach is not a function"
function callAPI(symbol) {
// Call the api
var dataSheet = SpreadsheetApp.getActive().getSheetByName("Fundamental Analysis Model");
var url = 'https://financialmodelingprep.com/api/v3/ratios/'
var apikey = '?period=quarter&apikey=x'
var response = UrlFetchApp.fetch(url + symbol + apikey);
// Parse the JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
console.log(data)
return JSON.parse(json)
}
function displayFinancials(array) {
var dataSheet = SpreadsheetApp.getActive().getSheetByName("Fundamental Analysis Model");
var symbol = "AMZN"
var api = callAPI(symbol);
var results = api[0]
var output = []
results.forEach(function(elem) {
output.push([elem["currentRatio"],elem["quickratio"]]);
});
dataSheet.appendRow([output])
}
If anyone can help correct the array so it will output the 2 example results that would be amazing. Thanks!
Log Data:
[20-06-07 23:46:12:257 BST] Logging output too large. Truncating output. [ { symbol: 'AMZN',
date: '2020-03-31',
currentRatio: 1.0787093374816525,
quickRatio: 0.8421422388378016,
cashRatio: 0.341245248460062,
daysOfSalesOutstanding: 86.28187456926258,
daysOfInventoryOutstanding: 155.51901394129743,
operatingCycle: 241.80088851056001,
daysOfPayablesOutstanding: 330.35316447115713,
cashConversionCycle: -244.07128990189454,
grossProfitMargin: 0.4134416582728092,
operatingProfitMargin: 0.052868048560674334,
pretaxProfitMargin: 0.04483645231405397,
netProfitMargin: 0.03359751895244659,
effectiveTaxRate: 0.21992314513745195,
returnOnAssets: 0.011458248582973991,
returnOnEquity: 0.03883748008334355,
returnOnCapitalEmployed: 0.02390356610399429,
netIncomePerEBT: 0.7493349098433343,
ebtPerEbit: 1,
ebitPerRevenue: 0.04483645231405397,
debtRatio: 0.7049693090698704,
debtEquityRatio: 2.3894778771908323,
longTermDebtToCapitalization: 0.26420092662525785,
totalDebtToCapitalization: null,
interestCoverage: 8.41542288557214,
cashFlowToDebtRatio: null,
companyEquityMultiplier: 3.3894778771908323,
receivablesTurnover: 4.230320699708455,
payablesTurnover: 1.1048781705612143,
inventoryTurnover: 2.346979901362889,
fixedAssetTurnover: 0.9700818987130202,
assetTurnover: 0.34104448602862075,
operatingCashFlowPerShare: 6.152610441767068,
freeCashFlowPerShare: -7.491967871485944,
cashPerShare: 98.97991967871486,
payoutRatio: 0,
operatingCashFlowSalesRatio: 0.04060859884429836,
freeCashFlowOperatingCashFlowRatio: -1.2176892950391645,
cashFlowCoverageRatios: null,
shortTermCoverageRatios: null,
capitalExpenditureCoverageRatio: 0.45091979396615156,
dividendPaidAndCapexCoverageRatio: null,
dividendPayoutRatio: null,
priceBookValueRatio: 18.875658781713444,
priceToBookRatio: 18.875658781713444,
priceToSalesRatio: 16.32895085617346,
priceEarningsRatio: 486.0165680473373,
priceToFreeCashFlowsRatio: -330.22031626909677,
priceToOperatingCashFlowsRatio: 402.10574412532634,
priceCashFlowRatio: 402.10574412532634,
priceEarningsToGrowthRatio: 74.06250027159547,
priceSalesRatio: 16.32895085617346,
dividendYield: null,
enterpriseValueMultiple: 131.35365201582718,
priceFairValue: 18.875658781713444 },...
[20-06-07 23:46:12:265 BST] TypeError: results.forEach is not a function
at displayFinancials(Stock Research & Analysis:123:13)