π Introduction
Have you ever wished you could summarize the contents of multiple Google Docs with the help of AI and email the results to your inbox? This tutorial will show you how to do just that using Google Apps Script and Gemini AI.
With this system, you can:
- Automatically summarize Google Docs using Gemini AI.
- Email the summary directly to your inbox.
- Speed up reporting and document review for large sets of documents.
This is perfect for creating executive summaries, generating reports, or providing quick overviews of multiple documents.
π Key Features
- Read Google Docs from a Folder β Extracts text from every Google Doc in a specified folder.
- Summarize with Gemini AI β Calls the Gemini AI API to summarize the document’s contents.
- Email the Summary β Emails the summarized results to the user’s inbox.
π Prerequisites
- Google Drive Folder β Create a folder where the Docs are stored.
- Gemini AI API Key β Get an API key for Gemini AI.
- Google Apps Script β Access Google Apps Script and set up the automation.
π Full Apps Script
Here is the complete Apps Script that will extract, summarize, and email the summaries of Google Docs from a specific folder.
1οΈβ£ Main Function: Summarize and Email Google Docs
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 (powered by Gemini AI):\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();
// Send the text to Gemini AI to get a summary
const summary = callGeminiAI(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`;
}
}
// Send the email
MailApp.sendEmail({
to: userEmail,
subject: 'Summary of Google Docs (Generated by Gemini AI)',
body: emailBody
});
Logger.log('Email sent to ' + userEmail);
}
2οΈβ£ Call Gemini AI to Summarize the Text
This function calls Gemini AI to generate a summary for the content of a document.
function callGeminiAI(text) {
const payload = {
contents: [{ parts: [{ text: text }] }]
};
const options = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload)
};
try {
const response = UrlFetchApp.fetch(geminiModel, options);
const data = JSON.parse(response.getContentText());
const summary = data["candidates"][0]["content"]["parts"][0]["text"];
return summary;
} catch (error) {
Logger.log('Error calling Gemini AI: ' + error);
return 'Failed to summarize content using Gemini AI.';
}
}
π How It Works
- Extracts Docs from a Folder
- Loops through all Google Docs in a specific folder.
- Extracts the entire text of each document.
- Summarize with Gemini AI
- Sends the document text to Gemini AI.
- Gemini AI returns a summary of the content.
- Email the Summary
- Combines the summaries from all documents into one email.
- Sends the email to the userβs email address.
π Instructions for Use
1οΈβ£ Create a Google Drive Folder
- Create a folder in Google Drive.
- Add some Google Docs to this folder (manually or by script).
- Copy the folder ID from the URL (the part after
/folders/
).
2οΈβ£ Replace the Folder ID
Update this line with your folder ID:
const folderId = 'YOUR_FOLDER_ID_HERE';
3οΈβ£ Get Your Gemini API Key
- Go to Google Cloud Console.
- Create a project and enable Gemini AI API.
- Generate an API key and copy it.
- Replace the geminiApiKey in the script:
const geminiApiKey = 'YOUR_GEMINI_API_KEY_HERE';
4οΈβ£ Run the Script
- Open Google Apps Script.
- Run summarizeAndEmailGoogleDocs().
- Check your email for the summary.
π Example Email
Subject: Summary of Google Docs (Generated by Gemini AI)
Body:
Here is a summary of the contents from the Google Docs in your folder (powered by Gemini AI):
---
**Sample Document 1**
Gemini AI Summary: Google Apps Script is a tool that allows you to automate workflows and process data using cloud-based scripting. This document introduces the concept of automation with Apps Script.
---
**Sample Document 2**
Gemini AI Summary: Cloud automation can save time and effort. This document explains the importance of using automation tools like Google Apps Script to increase productivity and streamline workflows.
---
**Sample Document 3**
Gemini AI Summary: This document demonstrates how to extract and summarize multiple Google Docs using automation. It highlights the importance of summarizing large sets of documents for reporting or executive summaries.
π Customization Ideas
- Attach the Documents
- Attach the Google Docs as PDF files in the email.
- Change the Email Subject
- Update this line to customize the subject line:
subject: 'Summary of Google Docs (Generated by Gemini AI)'
- Update this line to customize the subject line:
- Custom Summarization
- Instead of summarizing the entire document, extract specific bullet points, headings, or specific paragraphs.
π Troubleshooting
Issue | Possible Cause | Solution |
---|---|---|
No API Key | Missing Gemini API key | Add your API key to the script. |
No Email Sent | Incorrect Folder ID | Verify that you set the correct folder ID. |
Document Not Found | File permissions issue | Ensure the file is accessible. |
Summary is too short | Text not sufficient for Gemini AI | Increase the amount of content sent to Gemini AI. |
Error calling Gemini AI | API not enabled or wrong API key | Ensure the API is enabled in Google Cloud Console. |
π Final Thoughts
With this Apps Script, you can:
- Summarize Google Docs with AI (powered by Gemini AI).
- Email the summary directly to your inbox.
This process is perfect for managing large sets of Google Docs, creating summaries, and sending reports. You can use it to automate daily, weekly, or monthly reporting workflows.
If you’d like any changes, help with the Gemini API, or want to customize the script, just let me know! π
