In Part 1 of this series, I showed you how to merge multiple CSV files into a single Google Spreadsheet, with each CSV file stored as a separate sheet. Now, let’s leverage ChatGPT to turn these sheets into a visually engaging Google Slides presentation.
The goal of this post is to:
- Use ChatGPT to generate the Apps Script code for creating a line chart for each sheet in the Google Spreadsheet.
- Use ChatGPT again to create the code for inserting these charts into a Google Slides presentation.
- Format and position the charts within the slides to ensure a polished look.
We’ll tackle this in three steps:
Step 1: Get ChatGPT to Generate the Code to Read and Create the Charts
When you have multiple sheets in a Google Spreadsheet and want to create a chart from specific columns, it can be tedious to set up each chart manually. We can use ChatGPT to automate this process.
Prompt to ChatGPT
Date
, Open
, and Close
columns. The Date
column should be on the x-axis, and Open
and Close
should be two separate series. Generate the chart so that it can be used in a presentation"Step 2: Get ChatGPT to Create the Code to Write the Charts to the Presentation
Now that we have a function to generate the charts as images, we need to insert these images into a Google Slides presentation. We can again ask ChatGPT to help us with this part of the code.
Prompt to ChatGPT
ChatGPT Output
function createChartsAndEmbedInSlides() {
var spreadsheetId = 'YOUR_SPREADSHEET_ID'; // Replace with your Google Spreadsheet ID
var presentationId = 'YOUR_PRESENTATION_ID'; // Replace with your Google Slides Presentation ID
var spreadsheet = SpreadsheetApp.openById(spreadsheetId);
var sheets = spreadsheet.getSheets(); // Get all sheets in the spreadsheet
var presentation = SlidesApp.openById(presentationId);
for (var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
var sheetName = sheet.getName();
var chartBlob = createLineChartFromSheet(sheet);
if (chartBlob) {
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);
slide.insertTextBox("Open and Close Prices for " + sheetName, 10, 10, 600, 50); // Add slide title
var image = slide.insertImage(chartBlob);
var slideWidth = presentation.getPageWidth();
var slideHeight = presentation.getPageHeight();
var aspectRatio = image.getWidth() / image.getHeight();
image.setLeft(0);
image.setTop(60); // Position below title
var maxImageHeight = slideHeight - 80; // Available height with padding
var maxImageWidth = slideWidth;
// Adjust image size to fit within slide boundaries
if (slideWidth / aspectRatio > maxImageHeight) {
image.setHeight(maxImageHeight);
image.setWidth(maxImageHeight * aspectRatio);
} else {
image.setWidth(maxImageWidth);
image.setHeight(maxImageWidth / aspectRatio);
}
}
}
Logger.log("Process completed! Check your presentation for the updated slides.");
}
Step 3: Set up and run the script. (covered in Part 1, included here)
Create the Google Slides Presentation
- Create a New Google Slides Presentation:
- Go to Google Slides and click Blank to create a new presentation.
- Name your presentation (e.g., “Stock Data Presentation”).
- Obtain the Presentation ID:
- Look at the URL of the Google Slides presentation:
https://docs.google.com/presentation/d/1ny5EzuY5kBGXrFG56kq5WvO8-Y4tQpculaMTReEcQoc/edit
- Copy the part of the URL between
/d/
and/edit
. In this example, the Presentation ID is: "1ny5EzuY5kBGXrFG56kq5WvO8-Y4tQpculaMTReEcQoc"
- Look at the URL of the Google Slides presentation:
Prepare Your Google Spreadsheet
Create a Google Spreadsheet:
- Go to Google Sheets and create a new spreadsheet.
- Use the spreadsheet created in Part 1, or create a new one by uploading the CSV files to separate sheets as demonstrated in the first blog post.
Obtain the Spreadsheet ID:
- Look at the URL of the Google Spreadsheet:
https://docs.google.com/spreadsheets/d/1vt852T-ZvRF5zd0gp1xdLtkt0pCK-Zhs7B7kf_uYkhU/edit
- Copy the part of the URL between
/d/
and/edit
. In this example, the Spreadsheet ID is: "1vt852T-ZvRF5zd0gp1xdLtkt0pCK-Zhs7B7kf_uYkhU"
- Look at the URL of the Google Spreadsheet:
Setting Up the Script
Now that you have your Google Spreadsheet and Google Slides Presentation IDs, it’s time to set up the script:
Open the Apps Script Editor:
- In your Google Spreadsheet, go to Extensions → Apps Script.
- This will open a new tab with the Apps Script editor.
Create a New Script:
- Delete any existing code in the editor.
- Copy and paste the complete code (provided in the final version above) into the script editor.
Set the Spreadsheet and Presentation IDs:
- Replace the placeholders in the script with your actual Spreadsheet ID and Presentation ID:
var spreadsheetId = '1vt852T-ZvRF5zd0gp1xdLtkt0pCK-Zhs7B7kf_uYkhU'; // Replace with your Spreadsheet ID var presentationId = '1ny5EzuY5kBGXrFG56kq5WvO8-Y4tQpculaMTReEcQoc'; // Replace with your Presentation ID
- Replace the placeholders in the script with your actual Spreadsheet ID and Presentation ID:
Save the Script:
- Click the floppy disk icon or press
Ctrl + S
to save the script. - Give it a name like "CreateChartsAndSlides".
- Click the floppy disk icon or press
Run the Script
- Run the Script:
- Click the Run button (▶️) in the Apps Script editor.
- Grant Permissions:
- The first time you run the script, Google will prompt you to grant permissions to access your Google Sheets and Google Slides. Review the permissions and click Allow.
- Monitor the Logs:
- You can monitor the script execution by clicking on View → Logs in the Apps Script editor.
- If the script encounters any errors, check the log messages for details.
Review the Presentation
Open Your Google Slides Presentation:
- Navigate back to the Google Slides presentation you created earlier. You should have one slide per sheet in your spreadsheet.
Conclusion
This two-part series has covered how to automate the process of creating a Google Slides presentation from multiple CSV files using Google Apps Script. With a few lines of code, we’ve transformed CSV data into an informative presentation, ready for analysis or sharing.
Comments
Post a Comment