iTranslated by AI
Getting a List of Slide Image URLs from a Google Slides Presentation
Suppose you want to obtain a list of slide image URLs from a presentation created in Google Slides and retrieve them in JSON format. This assumes a case where you want to fetch only the slide image URLs and create your own view for displaying them.
In this article, we will create a JSON API using Google Apps Script (hereafter referred to as GAS).
How to Get Slide URLs
There is an API that allows you to manipulate Google Slides from Google Apps Script (Class SlidesApp | Apps Script | Google Developers). However, it seems there is no way to directly retrieve the slide's image URL itself using this API.
Therefore, I will substitute it with a URL that exports the slide as a PNG image by constructing it manually. You can create a URL to export a slide as a PNG image by constructing it as follows:
"https://docs.google.com/presentation/d/" +
presentation.getId() +
"/export/png?pageid=" +
slide.getObjectId();
The presentation and slide variables included in the code snippet above will be explained later.
GAS Script to Get a List of Slide Image URLs
I created a simple script like the one shown below on GAS. It functions as an API that receives a ?url=presentation_URL parameter and returns a list of image URLs for the slides contained in the Google Slides presentation specified by that URL in JSON format.
As a prerequisite for this script, the presentation passed as a parameter must be set to public.
Also, regarding how to publish a GAS script as a web app, there are many resources available elsewhere, so please refer to those.
function doGet(e) {
var url = e.parameters.url;
var presentation = getPresentationFor(url);
var result;
if (presentation) {
var slides = presentation.getSlides().map(function(slide) {
return getSlideUrlFor(presentation, slide);
});
result = {
error: false,
title: presentation.getName(),
url: url[0],
slides: slides
};
} else {
result = {
error: true,
presentation: undefined
}
}
return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}
function getPresentationFor(url) {
var presentation;
try {
presentation = SlidesApp.openByUrl(url);
} catch(e) {}
return presentation;
}
function getSlideUrlFor(presentation, slide) {
var url = "https://docs.google.com/presentation/d/" +
presentation.getId() +
"/export/png?pageid=" +
slide.getObjectId();
return url;
}
Checking the Operation of the Slide Image URL List API
Let's check the operation of the API using a public slide titled "The biggest innovation in the industry I belong to in the last 10 to 20 years" as an example.
$ curl -s -L "https://script.google.com/macros/s/AKfycby1lq2umAu34LkpMQJ6ncqF_8Of-vUgD7QngAVCuo2gQ9gfYIw4zMRW7ygc3hhl4uA4yg/exec?url=https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/edit" | jq
{
"error": false,
"title": "私が所属する業界における最近10〜20年間における最大のイノベーション",
"url": "https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/edit",
"slides": [
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_1",
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_8",
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_107",
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_22",
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_34",
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_51",
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_102",
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_181",
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_123",
"https://docs.google.com/presentation/d/1nwLrZzuXC_CWC42IgRl0QnNq5xsAc83gakgY2RHu3v8/export/png?pageid=g8ba55efc7c_0_149"
]
}
It seems to be working correctly. I've designed the response to include the presentation title as well as the URLs.
Discussion