-
Notifications
You must be signed in to change notification settings - Fork 1
/
How to Use Gemini AI to Summarize Google Docs and Email the Results
63 lines (54 loc) · 1.89 KB
/
How to Use Gemini AI to Summarize Google Docs and Email the Results
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function summarizeAndEmailGoogleDocs() {
const folderId = '1pT5B****td'; // 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);
}
/**
* Call Gemini AI API to summarize the text
* @param {string} text - The text to summarize
* @return {string} - The summary returned by Gemini AI
*/
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.';
}
}