Coding level: Intermediate
Duration: 20 minutes
Project type: Automation with a custom menu
Objectives
- Understand what the solution does.
- Understand what the Apps Script services do within the solution.
- Set up your environment.
- Set up the script.
- Run the script.
About this solution
You can analyze text data, such as open-ended feedback, at scale. To perform entity and sentiment analysis from within Google Sheets, this solution uses UrlFetch Service to connect to the Google Cloud Natural Language API.
How it works
The script gathers text from the spreadsheet and connects to the Google Cloud Natural Language API to analyze entities and sentiment present in the string. A pivot table summarizes the average sentiment score for each entity mentioned across all rows of text data.
Apps Script services
This solution uses the following services:
- Spreadsheet service–Sends the text data to the Google Cloud Natural Language API and marks each row as "Complete" once its sentiment has been analyzed.
- UrlFetch service–Connects to Google Cloud Natural Language API to perform entity and sentiment analysis on the text.
Prerequisites
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.
A Google Cloud project with an associated billing account. Refer to Enable billing for a project.
Set up your environment
Open your Cloud project in the Google Cloud console
If it's not open already, open the Cloud project that you intend to use for this sample:
- In the Google Cloud console, go to the Select a project page.
- Select the Google Cloud project you want to use. Or, click Create project and follow the on-screen instructions. If you create a Google Cloud project, you might need to turn on billing for the project.
Turn on the Google Cloud Natural Language API
This solution connects to the Google Cloud Natural Language API. Before using Google APIs, you need to turn them on in a Google Cloud project. You can turn on one or more APIs in a single Google Cloud project.
In your Cloud project, turn on the Google Cloud Natural Language API.
Configure the OAuth consent screen
This solution requires a Cloud project with a configured consent screen. Configuring the OAuth consent screen defines what Google displays to users and registers your app so that you can publish it later.
- In the Google Cloud console, go to Menu > APIs & Services > OAuth consent screen.
- For User type select Internal, then click Create.
- Complete the app registration form, then click Save and Continue.
For now, you can skip adding scopes and click Save and Continue. In the future, when you create an app for use outside of your Google Workspace organization, you must change the User type to External, and then, add the authorization scopes that your app requires.
- Review your app registration summary. To make changes, click Edit. If the app registration looks OK, click Back to Dashboard.
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.
In the Google Cloud console, go to Menu > APIs & Services > Credentials.
Click Create credentials > API key.
Take note of your API key for use in a later step.
Set up the script
Create the Apps Script project
- Click the button below to make a copy of the Sentiment analysis for
feedback sample spreadsheet. The Apps Script
project for this solution is attached to the spreadsheet.
Make a copy - Click Extensions > Apps Script.
- Update the following variable in the script file with your API key:
const myApiKey = 'YOUR_API_KEY'; // Replace with your API key.
- Click Save .
Add text data
- Return to the spreadsheet.
- Add text data to the id and comments columns. You can use sample vacation property reviews from Kaggle or use your own data. You can add more columns if needed, but to run successfully, the script must have data in the id and comments columns.
Run the script
- At the top of the spreadsheet, click Sentiment Tools > Mark entities and sentiment. You might need to refresh the page for this custom menu to appear.
When prompted, authorize the script. If the OAuth consent screen displays the warning, This app isn't verified, continue by selecting Advanced > Go to {Project Name} (unsafe).
Click Sentiment Tools > Mark entities and sentiment again.
When the script finishes, switch to the Pivot Table sheet to see the results.
Review the code
To review the Apps Script code for this solution, click View source code below:
View source code
Code.gs
Contributors
This sample is maintained by Google with the help of Google Developer Experts.
Next steps
- Blog: Analyzing text in Google Sheets using Google Cloud Natural Language API and Apps Script
- Google Cloud Natural Language API documentation