Search code examples
google-apps-scriptgoogle-sheetsyahoo-finance

Google Apps Script returns desired result in execution log, but cell in spreadsheets is empty


On this spreadsheet, the Apps Script returns the right result when I run it, the execution log correctly returns "202000.0".

However, when I run the script in a cell (highlighted in orange on the linked spreadsheet), the cell is empty, does anyone know how to fix this by any chance?

The Apps Script code:

function fullTimeEmployees(url) {
  var url = 'https://finance.yahoo.com/quote/WBA/profile'
  var source = UrlFetchApp.fetch(url).getContentText()
  var jsonString = source.match(/root.App.main = ([\s\S\w]+?);\n/)
  if (!jsonString || jsonString.length == 1) return;
  var data = JSON.parse(jsonString[1].trim())
  Logger.log(data.context.dispatcher.stores.QuoteSummaryStore.assetProfile.fullTimeEmployees)
}

Also, does anyone know what I can change .fulltimeEmployees (at the end) to so it returns the full company name? as pictured here, highlighted in green company name section


Solution

  • Answer for question 1:

    However, when I run the script in a cell (highlighted in orange on the linked spreadsheet), the cell is empty, does anyone know how to fix this by any chance?

    In your script, no value is returned. This is the reason of your issue. So please modify your script as follows.

    function fullTimeEmployees(url) {
      var url = 'https://finance.yahoo.com/quote/WBA/profile'
      var source = UrlFetchApp.fetch(url).getContentText()
      var jsonString = source.match(/root.App.main = ([\s\S\w]+?);\n/)
      if (!jsonString || jsonString.length == 1) return;
      var data = JSON.parse(jsonString[1].trim())
      Logger.log(data.context.dispatcher.stores.QuoteSummaryStore.assetProfile.fullTimeEmployees)
      return data.context.dispatcher.stores.QuoteSummaryStore.assetProfile.fullTimeEmployees; // Added
    }
    

    Answer for question 2:

    Also, does anyone know what I can change .fulltimeEmployees (at the end) to so it returns the full company name? as pictured here, highlighted in green

    In this case, how about modifying as follows?

    From:

    data.context.dispatcher.stores.QuoteSummaryStore.assetProfile.fullTimeEmployees
    

    To:

    data.context.dispatcher.stores.QuoteSummaryStore.price.shortName
    
    • The value of this is Walgreens Boots Alliance, Inc..