Analyze the sentiment of news headlines

Analyze the sentiment of news headlines retrieved for a user-specified topic from a free news API.

Last updated: March, 2021

This solution showcases natural language sentiment analysis on current news headlines. It consists of a Google Sheet that uses Apps Script to fetch the current news headlines from the free news API using a user-provided topic. Once the news headlines are fetched and loaded into the sheet, it uses the Google Cloud Natural Language API to run a sentiment analysis for each headline. As the sentiments are returned from the API, the script updates the sheet with both a numeric score as well as a happy, meh, or sad icon depending on the sentiment.

Technology highlights

  • This solution demonstrates the following:
    • Shows how to add custom menus to the Sheets UI.
    • Uses UrlFetch to load news headlines from an external API.
    • Demonstrates the Cloud Natural Language API to analyze headlines.

Try it

Before you begin: Obtain your API keys

To run this solution you need to get 2 API keys, one from the Google Cloud Natural Language API, and the second from the free News API.

Get the Google Cloud Natural Language API key

To get an API key for the Google Cloud Natural Language API, you must configure a Google Cloud Platform (GCP) project.

  1. Create a new or use an existing GCP project.
  2. Navigate to the Google Cloud Platform console.
    • If you create a new project:
      • Select your associated billing account.
      • Accept the defaults for organization and location.
      • Click Create, and then select the new project in the console.
  3. At the top of the console, click Menu > APIs & Services.
  4. Click + Enable APIs and Services.
  5. Search for Cloud Natural Language API, and enable it.
  6. On the left, click Credentials > + Create credentials.
  7. In the drop-down menu, select API key.
  8. Save this key to add to your Apps Script file.

Register for the News API key

To get an API key for the News API, you need to create a free News API account.

  1. To create an account, go to the News API site.
  2. Click Get API Key and follow the steps.
  3. Save the key to add to your Apps Script file.

Create your copy of the solution

Copy and customize the solution with your API keys.

  1. Make a copy of the News Sentiment Analyzer - External spreadsheet.

  2. To open the associated Apps Script Project, at the top, click Tools > Script editor.

  3. Update the script file with your API keys:

    • const googleAPIKey = YOUR_GOOGLE_API_KEY;
    • const newsApiKey = YOUR_NEWS_API_KEY;
  4. Save the script and return to the spreadsheet.

Run the News Sentiment Analyzer

  1. At the top of the spreadsheet, click the custom menu item News Headlines Sentiments.
  2. Select Analyze News Headlines.
    • Note: Upon first run, you must go through the Google authorization steps. This leaves the script running, but paused. After you authorize the script, click Dismiss, and select Analyze News Headlines again.
  3. In the dialog, enter a news topic or keyword. For example Global Warming.
  4. Click Ok to start the analysis.

Next steps

  • Experiment with other news topics.

  • Try adding code to do further steps. For example, you could save all topics and their average sentiments over time in a different tab to see if any trends emerge.

  • Add a time-based trigger to run the code on a daily basis with a set of topics selected from a different tab.

  • For more help with Apps Script coding, try the Apps Script Fundamentals codelabs.

  • For more information on the Google Cloud Natural Language API, try a quickstart.


Were you able to get the solution up and running?

If you have an idea for another solution you'd like to see featured in our gallery submit a request on the GitHub issue tracker.