Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-formulagoogle-finance

Can you write Google finance directly to an array?


Can googlefinance write results directly to an array? I am currently writing it to a sheet first and then pulling the sheet range into the array. It would save a lot of processing if I could write it directly to an array so I am investigating. If you have knowledge and expertise on this could you let me know?

I get an error when I try. Is it just incorrect syntax or is what I am trying to do not possible? I would like to avoid writing to the sheet to save on time and in sheet processing. But not sure if the function is allowed to write into a variable instead of a sheet

function TEST() {

 var APPLEPRICEARRAY = GOOGLEFINANCE("AAPL","price","1/1/2009","12/31/2020","WEEKLY")

};

Solution

  • Is it just incorrect syntax?

    You seem to confuse functions (formulas) that are exposed in the Google Sheets application with services that are available in Google Apps Script environment. What you wrote would require GOOGLEFINANCE to be a service "attached" to global scope, so yes, this is incorrect.

    But the error you get is not a syntax error, your reference is invalid (no GOOGLEFINANCE variable is ever declared in the global scope, therefore none can be referenced), hence you get ReferenceError.

    Can googlefinance write results directly to an array?

    No, for reasons explained above, it cannot. I apologize for this, but you are comparing apples with oranges: an array is a data structure (an indexed collection, to be precise) in JavaScript (on which Google Apps Script language is based), while formulas are not even built-in objects: they are not part of the language.

    Is what I am trying to do not possible?

    Unfortunately, Google Finance API has been shut down for a long time now, so no, not possible.


    Not screaming with ALL-CAPS is considered a common courtesy as well