π Introduction
Have you ever wanted to create Google Docs automatically and send an email summary of their contents? With Google Apps Script, you can automate this entire process. This is perfect for report generation, executive summaries, or consolidating multiple Google Docs into one easy-to-read summary.
In this post, youβll learn how to:
- Create multiple Google Docs from a script.
- Extract and summarize contents from the Google Docs.
- Send an email with the combined summary of all the documents.
By the end of this guide, youβll have a powerful automation system that creates, processes, and summarizes Google Docs.
π What Youβll Learn
- Create Sample Google Docs β Generate three sample Google Docs with content.
- Summarize and Email Docs β Extract and summarize the text from each doc.
- Email the Summary β Send the summary to the userβs email address.
π Prerequisites
- Access to Google Drive β Create a folder in Google Drive to store the sample Google Docs.
- Google Apps Script β Access the Apps Script editor to copy and run the script.
- Folder ID β Get the ID of the Google Drive folder where you want the documents stored.
π Full Apps Script
This script contains two main functions:
- createSampleDocs() β Creates Google Docs with sample content.
- summarizeAndEmailGoogleDocs() β Extracts and summarizes the contents of the docs and sends an email summary.
π 1. Create Sample Google Docs
This function generates 3 Google Docs with sample content and moves them into a specific folder.
function createSampleDocs() {
const folderId = 'YOUR_FOLDER_ID_HERE'; // Replace with your folder ID
const folder = DriveApp.getFolderById(folderId);
const docData = [
{
fileName: 'Sample Document 1',
content: `
Welcome to Sample Document 1.
This document is an introduction to automation using Google Apps Script.
Google Apps Script is a powerful tool that allows you to automate workflows, process data, and interact with Google services.
`
},
{
fileName: 'Sample Document 2',
content: `
This is Sample Document 2.
Here, we talk about the importance of cloud automation and how it can save time and effort.
With Google Apps Script, you can create custom workflows and increase productivity in the workplace.
`
},
{
fileName: 'Sample Document 3',
content: `
Welcome to Sample Document 3.
In this document, we demonstrate how easy it is to extract data from multiple Google Docs and combine them into one summary.
This process can be useful for generating reports, overviews, or executive summaries.
`
}
];
docData.forEach(data => {
const doc = DocumentApp.create(data.fileName);
const body = doc.getBody();
body.appendParagraph(data.content);
doc.saveAndClose();
const file = DriveApp.getFileById(doc.getId());
folder.addFile(file); // Move the file to the target folder
DriveApp.getRootFolder().removeFile(file); // Remove it from the root folder
});
Logger.log('Sample Google Docs have been created in the folder: ' + folder.getName());
}
π 2. Extract and Summarize Docs
This function reads all the Google Docs from the specified folder and sends an email summary of their contents.
function summarizeAndEmailGoogleDocs() {
const folderId = 'YOUR_FOLDER_ID_HERE'; // Replace with your folder ID
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFilesByType(MimeType.GOOGLE_DOCS);
const userEmail = Session.getActiveUser().getEmail();
let emailBody = 'Here is a summary of the contents from the Google Docs in your folder:\n\n';
while (files.hasNext()) {
const file = files.next();
const fileName = file.getName();
Logger.log(`Processing file: ${fileName}`);
try {
const docId = file.getId();
const doc = DocumentApp.openById(docId);
const text = doc.getBody().getText();
const summary = summarizeText(text);
emailBody += `---\n**${fileName}**\n${summary}\n\n`;
} catch (e) {
Logger.log(`Failed to process file: ${fileName}. Error: ${e}`);
emailBody += `Failed to process file: ${fileName}\n\n`;
}
}
MailApp.sendEmail({
to: userEmail,
subject: 'Summary of Google Docs from Your Folder',
body: emailBody
});
Logger.log('Email sent to ' + userEmail);
}
function summarizeText(text) {
const paragraphs = text.split('\n\n'); // Split the text into paragraphs
let summary = '';
for (let i = 0; i < paragraphs.length && i < 3; i++) {
summary += paragraphs[i] + '\n\n';
if (summary.length >= 500) break;
}
if (summary.length > 500) {
summary = summary.substring(0, 500) + '...';
}
return summary.trim();
}
π How It Works
- Create Sample Docs
- Generates 3 sample Google Docs in a specified folder.
- Moves the Docs from the root Drive to the target folder.
- Summarize Docs and Email
- Extracts the first 3 paragraphs or 500 characters from each document.
- Sends the summary as an email to the user.
π Step-by-Step Instructions
1οΈβ£ Create a Google Drive Folder
- Create a folder in Google Drive.
- Get the folder’s ID from the URL (the part after
/folders/
).
2οΈβ£ Replace the Folder ID
Update this line in both scripts to match your folder ID:
const folderId = 'YOUR_FOLDER_ID_HERE';
3οΈβ£ Run the Script
- Open Google Apps Script.
- Copy and paste the entire script into the editor.
- Run createSampleDocs() to generate the Google Docs.
4οΈβ£ Summarize and Email the Docs
- Run summarizeAndEmailGoogleDocs().
- Check your email for the summary.
π Example Email
Subject: Summary of Google Docs from Your Folder
Body:
Here is a summary of the contents from the Google Docs in your folder:
---
**Sample Document 1**
Welcome to Sample Document 1.
This document is an introduction to automation using Google Apps Script.
Google Apps Script is a powerful tool that allows you to automate workflows, process data, and interact with Google services.
---
**Sample Document 2**
This is Sample Document 2.
Here, we talk about the importance of cloud automation and how it can save time and effort.
With Google Apps Script, you can create custom workflows and increase productivity in the workplace.
---
**Sample Document 3**
Welcome to Sample Document 3.
In this document, we demonstrate how easy it is to extract data from multiple Google Docs and combine them into one summary.
This process can be useful for generating reports, overviews, or executive summaries.
π Customization Ideas
- Include Attachments: Attach the Google Docs as PDF files in the email.
- Change the Summary: Extract only specific paragraphs, headings, or bullet points.
- HTML Email: Convert the plain text email into an HTML-formatted email.
- Send Email to Another User: Change
Session.getActiveUser().getEmail()
to a specific email address.
π Conclusion
This powerful script allows you to:
- Create Google Docs automatically.
- Extract and summarize the contents of each document.
- Email the summary to yourself or others.
This system is great for creating executive summaries, content aggregation, or generating reports from multiple documents.
