Created
May 25, 2020 13:38
-
-
Save ZackAkil/7e5b7a866e339c28ddf657830118419c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu("🔮 Predictions for cell") | |
.addItem('😃/😡 Sentiment', 'predict_on_row') | |
.addToUi(); | |
} | |
const output_column_number = 2 | |
function predict_on_row() { | |
var spreadsheet = SpreadsheetApp.getActive(); | |
const row_number = spreadsheet.getActiveCell().getRowIndex() | |
const text = spreadsheet.getActiveCell().getValue() | |
const sentiment_score = CloudNaturalLanguageAPI(text) | |
spreadsheet.getActiveSheet().getRange(row_number, output_column_number).setValue(sentiment_score) | |
} | |
function CloudNaturalLanguageAPI(text) { | |
const payload = JSON.stringify( | |
{ | |
"document":{ | |
"type":"PLAIN_TEXT", | |
"language": "EN", | |
"content": text | |
}, | |
"encodingType":"UTF8" | |
} | |
); | |
const url = 'https://language.googleapis.com/v1/documents:analyzeSentiment?key=' + API_KEY; | |
const response = UrlFetchApp.fetch(url, { | |
method: 'POST', | |
contentType: 'application/json', | |
payload: payload, | |
muteHttpExceptions: true | |
}).getContentText(); | |
const json_resp = JSON.parse(response) | |
Logger.log(json_resp) | |
return json_resp.documentSentiment.score | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment