You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 7, 2023. It is now read-only.
* initial checkin of news sample
* slight update to readme
* A simple update to test new SSH key
* another test submit
* another tweak to readme. Just testing github
* Initial Checkin of News Sentiment
* Updates to ReadMe per initial review
* final checkin per Readme review
description: Analyze the sentiment of news headlines retrieved for a user-specified topic from a free news API.
4
+
labels: Apps Script, Sheets, Cloud Natural Language
5
+
material_icon: Feed
6
+
create_time: 2021-03-22
7
+
update_time: 2021-03-29
8
+
---
9
+
10
+
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 `newsapi.org` using a user-provided `topic`.
11
+
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.
12
+
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.
13
+
14
+
## Technology highlights
15
+
16
+
- This solution demonstrates the following:
17
+
* Shows how to add custom menus to the Sheets UI.
18
+
* Uses `UrlFetch` to load news headlines from an external API.
19
+
* Demonstrates the Cloud Natural Language API to analyze headlines.
20
+
21
+
## Try it
22
+
23
+
### Before you begin: Obtain your API keys
24
+
To run this solution you need to get 2 API keys, one from the [Google Cloud Natural Language API](https://cloud.google.com/natural-language), and the second from the free News API @ [http://newsapi.org/](http://newsapi.org/).
25
+
26
+
#### Get the Google Cloud Natural Language API key
27
+
28
+
To get an API key for the Google Cloud Natural Language API, you must configure a Google Cloud Platform (GCP) project.
29
+
30
+
1. Create a new or use an existing GCP project:
31
+
1. Navigate to the main {{console_name_short}} page.
* Accept the defaults for organization and location.
38
+
* Click **Create**, and then select the new project in the console.
39
+
40
+
1. At the top of the console, click <spanclass=”material-icons”>menu</span> <spanaria-label="and then">></span> **APIs & Services**.
41
+
Click **+ Enable APIs and Services**.
42
+
1. Search for `Cloud Natural Language API`, and enable it.
43
+
1. On the left, click **Credentials** <spanaria-label="and then">></span> **+ Create credentials**.
44
+
1. In the drop-down menu, select **API key**.
45
+
1. Save this key to add to your Apps Script `Code.gs` file.
46
+
47
+
#### Register for the News API key.
48
+
To get an API key for the News API, you need to create a free News API account.
49
+
1. To create an account, go to the [News API](https://newsapi.org/) site.
50
+
1. Click **Get API Key** and follow the steps.
51
+
1. Save the key to add to your Apps Script `Code.gs` file.
52
+
53
+
### Create your copy of the solution
54
+
Copy and customize the solution with your API keys.
55
+
56
+
1. Click to make a copy of the [News Sentiment Analyzer - External spreadsheet](https://docs.google.com/spreadsheets/d/1Jw-d2ihbjSyO4SyzgXSiC5dzs36GY5aMGxuf_nc7WKU/copy).
57
+
58
+
1. To open the associated Apps Script Project, at the top, click **Tools <spanaria-label="and then">></span> Script editor**.
59
+
1. Update the `Code.gs` script file with your API keys:
60
+
* const googleAPIKey = `YOUR_GOOGLE_API_KEY`;
61
+
* const newsApiKey = `YOUR_NEWS_API_KEY`;
62
+
1. Save the script and return to the spreadsheet.
63
+
64
+
### Run the News Sentiment Analyzer
65
+
1. At the top of the spreadsheet, click the custom menu item **News Headlines Sentiments**.
66
+
1. Select **Analyze News Headlines**.
67
+
* 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.
68
+
1. In the popup dialog, enter a news topic or keyword. For example `Global Warming`.
69
+
1. Click **Ok** to start the analysis.
70
+
71
+
## Next steps
72
+
73
+
* Experiment with other news topics.
74
+
75
+
* 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.
76
+
77
+
* Add a time-based trigger to run the code on a daily basis with a set of topics selected from a different tab.
78
+
79
+
* For more help with Apps Script coding, try the [Apps Script Fundamentals codelabs](https://developers.google.com/apps-script/quickstart/fundamentals-codelabs).
80
+
81
+
* For more information on the Google Cloud Natural Language API, [try a quickstart](https://cloud.google.com/natural-language/docs/quickstarts).
0 commit comments