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 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
- Go to the News API.
- Click Get API Key and follow the steps.
- Take note of your API key for use in a later step.
Get an API key for the Google Cloud Natural Language API
- Go to the Google Cloud Console. Make sure your billing-enabled project is open.
- At the top of the console, click Menu > APIs & Services.
- Click Enable APIs and Services.
- Search for
Cloud Natural Language API
and enable it. - Click Menu > APIs & Services > Credentials.
- At the top, click Create credentials > API Key.
- Take note of your API key for use in a later step.
Step 2: Set up the spreadsheet
- Click the button below to make a copy of the External API - News sentiment
analyzer sample spreadsheet.
Make a copy - Click Extensions > Apps Script.
- In the script file, update the following variables with your API keys:
const googleAPIKey = 'YOUR_GOOGLE_API_KEY'; const newsApiKey = 'YOUR_NEWS_API_KEY';
- Click Save
and return to the spreadsheet.
Step 3: Run the sentiment analysis
- Click News Headlines Sentiments > Analyze News Headlines. You might need to refresh the page for this custom menu to appear.
- When prompted, authorize the script.
- Click News Headlines Sentiments > Analyze News Headlines again.
- In the dialog, enter a news topic or keyword. For example
Climate change
. - 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.