Below is an example Google Apps Script code that performs a few common operations related to extracting content from a Google Slides presentation. This script will:
List all slides titles in the presentation.
Extract all text from the slides.
Download a PDF copy of the presentation (this part requires a bit more setup to save the file, as Apps Script can't directly save files to your local machine, but it can prepare the file for download or save it to Google Drive).
Please follow these steps and the code snippet below:
Step 1: Access Google Apps Script
Open your Google Slides presentation.
Click on Extensions > Apps Script. This opens the Google Apps Script editor, which allows you to write and run scripts that interact with your Google Slides presentation.
Step 2: Paste the Script
Delete any code in the editor, and paste the following script:
javascript
function extractFromSlides() {
// Get the active presentation
var presentation = SlidesApp.getActivePresentation();
if (!presentation) {
Logger.log("No active presentation found.");
return;
}
// Get all slides in the presentation
var slides = presentation.getSlides();
// **List all slide titles** (Assumes the first text element is the title)
var slideTitles = [];
slides.forEach(function(slide, index) {
var textElements = slide.getPageElements()
.filter(SlidesApp.ElementType.TEXT);
if (textElements.length > 0) {
var title = textElements[0].asText().getText().asString();
slideTitles.push("Slide " + (index + 1) + " Title: " + title);
} else {
slideTitles.push("Slide " + (index + 1) + " has no text elements.");
}
});
Logger.log(slideTitles.join("\n"));
// **Extract all text from slides**
var allText = [];
slides.forEach(function(slide, index) {
var textElements = slide.getPageElements()
.filter(SlidesApp.ElementType.TEXT);
if (textElements.length > 0) {
var slideText = textElements.map(function(element) {
return element.asText().getText().asString();
}).join("\n");
allText.push("Slide " + (index + 1) + " Text:\n" + slideText);
} else {
allText.push("Slide " + (index + 1) + " has no text elements.");
}
});
Logger.log(allText.join("\n\n"));
// **Prepare a PDF for download (saves to Google Drive)**
try {
var blob = presentation.getAs('application/pdf');
var folderId = 'YOUR_GOOGLE_DRIVE_FOLDER_ID'; // Change this to your desired folder ID
var folder = DriveApp.getFolderById(folderId);
if (!folder) {
Logger.log("Folder not found. Please check the folder ID.");
return;
}
var file = folder.createFile(blob);
Logger.log('File saved to Drive with ID: ' + file.getId());
} catch (e) {
Logger.log('Error saving PDF to Drive: ' + e.message);
}
}
Step 3: Configure the Script (for PDF saving)
To save the PDF to Google Drive, you need to specify a folder ID in the script. Replace 'YOUR_GOOGLE_DRIVE_FOLDER_ID' with the actual ID of a Google Drive folder you own. You can find the folder ID in the URL when you open the folder in Google Drive. For example, if your folder URL is https://drive.google.com/drive/folders/abc123456789, then abc123456789 is your folder ID.
Authorization: The first time you run the script, you'll be prompted to authorize it. Follow the prompts to complete the authorization process. This is necessary because the script needs permission to access your Google Slides presentation and Google Drive.
Step 4: Run the Script
In the Apps Script editor, press the "Run" button (or press Ctrl+Enter/Cmd+Enter) next to the extractFromSlides function.
Since this script performs operations that require permission, you'll be asked to review and authorize the permissions on the first run.
Viewing the Output
Logger Output: To see the list of slide titles and the extracted text, open the Executions tab in the left sidebar of the Apps Script editor and click on the most recent execution. Then, click on the "Logs" tab at the bottom of the page.
PDF File: If configured, your PDF file will be saved to the specified Google Drive folder. You can find it there, and download it locally if needed.
Note: This script assumes the first text element on each slide is the title. If your slide structure is different, you might need to adjust the script accordingly. For example, if your titles are not always the first element, you may need to identify them by their formatting or position on the slide.
Adapting for Different Slide Structures:
If your slides have a consistent layout (e.g., titles are always at the top), you can modify the script to identify titles based on their position or formatting.
For more complex slides, consider using getPageElements() to iterate through all elements and filter by ElementType to handle different types of content (e.g., images, tables, etc.).
Troubleshooting Tips:
If the script fails to save the PDF, ensure the folder ID is correct and you have permission to write to that folder.
If text extraction misses some elements, verify that the elements are text boxes and not images of text.
This script provides a solid foundation and can be adapted for more complex scenarios, such as handling tables, images, or specific layouts, by modifying the element filtering logic.