Google finance 200 day moving average is getting as #NA in Apps Script

Syed Mohammed Mehdi picture Syed Mohammed Mehdi · Jun 18, 2017 · Viewed 16.3k times · Source

I'm calculating Google finance 200 day moving average formula in google sheet using formula

=average(query(sort(GoogleFinance("GOOG","price", TODAY()-320, TODAY()),1,0),"select Col2 limit 200"))

Then in google app script I'm getting the above cell value in variable as below

var val = sheet.getRange("T101").getValue();

but in google script I'm getting that variable value as #NA. Can anyone please advise what is causing the issue?

Answer

Ray Shan picture Ray Shan · Oct 20, 2018

To expand on @Ric ky's answer, the trick here is to get to a range so the average math can be performed on it.

For this working answer:

=AVERAGE(INDEX(GoogleFinance("GOOG","all",WORKDAY(TODAY(),-200),TODAY()),,3))

Here's why it works:

=AVERAGE( INDEX( // used to get 1 value or a range of values from a reference GoogleFinance("GOOG","all",WORKDAY(TODAY(),-200),TODAY()), // returns an expanded array with column headers, used by INDEX as the reference , // bypass INDEX's row argument to return an entire column to be AVERAGE'd 3 // we want the High column with an index of 3; index is 1-based ) )

A visual:

enter image description here