Created
July 23, 2020 11:37
-
-
Save sagunsh/f882d63a4fe800f5d9b090391239069f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 addMenu() { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Datanews API') | |
.addItem('Get News','myFunction') | |
.addToUi(); | |
} | |
function myFunction() { | |
var API_KEY = "your api key"; | |
var url = "http://api.datanews.io/v1/headlines?q=travel&apiKey=" + API_KEY; | |
var response = UrlFetchApp.fetch(url); | |
var data = JSON.parse(response.getContentText()); | |
var results = data.hits; | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var header = ["Title", "Description", "URL", "Published Date"] | |
var items = [header]; | |
results.forEach(function (result) { | |
items.push([result.title, result.description, result.url, result.pubDate]); | |
}); | |
sheet.getRange(1,1,items.length,items[0].length).setValues(items); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a question, line 13, hits? My code is not running, forEach is undefined. I guess hits is incorrect...