To extract news articles related to a specific keyword and send them to your email using Apps Script in Google Sheets, you can follow these steps:
- Open your Google Sheets document.
- Click on "Extensions" in the menu bar, then select "Apps Script."
- A new tab will open with the Apps Script editor.
- In the editor, delete any existing code and replace it with the following code:
javascriptfunction sendNewsToEmail() {
var keyword = "your_keyword"; // Replace "your_keyword" with the keyword you want to search
// Fetch news articles using Google News API
var url = "https://newsapi.org/v2/everything?q=" + encodeURIComponent(keyword) + "&apiKey=YOUR_API_KEY";
var response = UrlFetchApp.fetch(url);
var articles = JSON.parse(response.getContentText()).articles;
// Select the first 10 articles
articles = articles.slice(0, 10);
// Compose email body with articles
var emailBody = "";
for (var i = 0; i < articles.length; i++) {
var article = articles[i];
emailBody += "<h3>" + article.title + "</h3>";
emailBody += "<p>" + article.description + "</p>";
emailBody += "<a href='" + article.url + "'>Read more</a><br><br>";
}
// Send email
var recipient = "[email protected]"; // Replace with your email address
var subject = "Top 10 news articles for keyword: " + keyword;
MailApp.sendEmail({
to: recipient,
subject: subject,
htmlBody: emailBody
});
}
- Replace
"your_keyword"
in the code with the keyword you want to search for news articles. - Obtain an API key from newsapi.org by signing up for an account and create a new API key.
- Replace
"YOUR_API_KEY"
in the code with the API key you obtained from News API. - Replace
"[email protected]"
in the code with your email address where you want to receive the articles. - Save the script by clicking on the floppy disk icon or pressing
Ctrl + S
. - Click on the "Run" icon (play button) to execute the
sendNewsToEmail
function for the first time. - A dialog box will appear asking for authorization. Click "Review Permissions" and follow the prompts to grant the necessary permissions for the script.
- After authorizing, click on the "Run" icon again to execute the function.
- The function will fetch the news articles, compose an email with the top 10 articles, and send it to the specified email address.
Note: Make sure to enable the "Apps Script API" in your Google Cloud Platform project associated with the Google account you're using.
That's it! You should receive an email with the top 10 news articles related to your keyword. You can run this script manually whenever you want to get the latest articles, or you can set up a time-driven trigger to automatically send emails at specific intervals.
Sure! Here's the modified code to fetch news articles from News API without using an external library:
javascriptfunction sendNewsToEmail() {
var keyword = "인공지능"; // Enter your keyword in Korean
// Fetch news articles using News API
var url = "https://newsapi.org/v2/everything?q=" + encodeURIComponent(keyword) + "&apiKey=YOUR_API_KEY";
var response = UrlFetchApp.fetch(url);
var data = JSON.parse(response.getContentText());
var articles = data.articles;
// Select the first 10 articles
articles = articles.slice(0, 10);
// Compose email body with articles
var emailBody = "";
for (var i = 0; i < articles.length; i++) {
var article = articles[i];
emailBody += "<h3>" + article.title + "</h3>";
emailBody += "<p>" + article.description + "</p>";
emailBody += "<a href='" + article.url + "'>자세히 보기</a><br><br>";
}
// Send email
var recipient = "[email protected]"; // Replace with your email address
var subject = "인공지능과 관련된 상위 10개 뉴스 기사";
MailApp.sendEmail({
to: recipient,
subject: subject,
htmlBody: emailBody
});
}
위의 코드를 사용하여 News API에서 뉴스 기사를 가져온 후, 해당 기사를 이메일로 보낼 수 있습니다. 코드 내의 주석을 따라 진행하시면 됩니다.
주의사항: 코드 내의 "YOUR_API_KEY"
부분을 News API에서 발급받은 API 키로 대체해야 합니다.
이메일 주소를 "[email protected]"
로 수정하였습니다. 필요에 따라 코드에서 해당 부분을 변경하십시오.
이제 코드를 실행하면 "인공지능"과 관련된 상위 10개의 뉴스 기사가 이메일로 전송됩니다.