Connect to an external API: Analyze news headlines

Coding level: Intermediate
Duration: 25 minutes
Project type: Automation with a custom menu

About this solution

This solution showcases how to connect to an external API from Apps Script. The script connects to the free News API and performs natural language sentiment analysis on news headlines.

How it works

From a Google Sheets spreadsheet, the script connects to the free News API and gets current news headlines based on user-provided keywords. Once the news headlines are loaded into the sheet, the script uses the Google Cloud Natural Language API to run a sentiment analysis for each headline. As the sentiments are returned, the script updates the sheet with both a numeric score as well as a happy, neutral, or sad icon that reflects the sentiment.

Apps Script services

This solution uses the following services:

  • Spreadsheet Service: Displays and formats the news headlines and sentiments.
  • UrlFetch Service: Connects to a non-Google API, the News API, to get the news headlines. To get the sentiment analysis, UrlFetch Service also connects to the Google Cloud Natural Language API since there isn't a built-in Apps Script service for it.

Before you begin

To use this sample, you need the following prerequisites:

  • A Google Account (Google Workspace accounts might require administrator approval)
  • A Google Cloud project with billing enabled
  • A web browser with access to the internet

Try it

Step 1: Set up your API keys

To access the News API and the Google Cloud Natural Language API, you must create API keys.

Get an API key for the News API

  1. Go to the News API.
  2. Click Get API Key and follow the steps.
  3. Take note of your API key for use in a later step.

Get an API key for the Google Cloud Natural Language API

  1. Go to the Google Cloud Console. Make sure your billing-enabled project is open.
  2. At the top of the console, click Menu > APIs & Services.
  3. Click Enable APIs and Services.
  4. Search for Cloud Natural Language API and enable it.
  5. Click Menu > APIs & Services > Credentials.
  6. At the top, click Create credentials > API Key.
  7. Take note of your API key for use in a later step.

Step 2: Set up the spreadsheet

  1. Click the button below to make a copy of the External API - News sentiment analyzer sample spreadsheet.
    Make a copy
  2. Click Extensions > Apps Script.
  3. In the script file, update the following variables with your API keys:
    const googleAPIKey = 'YOUR_GOOGLE_API_KEY';
    const newsApiKey = 'YOUR_NEWS_API_KEY';
    
  4. Click Save Save icon and return to the spreadsheet.

Step 3: Run the sentiment analysis

  1. Click News Headlines Sentiments > Analyze News Headlines. You might need to refresh the page for this custom menu to appear.
  2. When prompted, click Review permissions and authorize the script.
  3. Click News Headlines Sentiments > Analyze News Headlines again.
  4. In the dialog, enter a news topic or keyword. For example Climate change.
  5. Click OK.

Open the Apps Script project

The sample script's code includes in-line comments to help you understand how it works. To access the code, in the spreadsheet, click Extensions > Apps Script.

Contributors

This sample is maintained by Google with the help of Google Developer Experts.