Skip to content

Commit 03b526b

Browse files
committed
First working prototype. Creates image slides from Drive folder
0 parents  commit 03b526b

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.clasp.json

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## CreateSlidesFromDriveFolder
2+
3+
A Google Slides Editor Add-on that reads all images in a Google Drive folder and creates a slide for each image in the current presentation
4+
5+
### Develop
6+
7+
Clone this repo. This goes without saying, right?!
8+
9+
Make sure you have node.js (and npm naturally installed on your development system)
10+
11+
You are going to need the Clasp CLI in order to work with this project.
12+
13+
**Install Clasp globally**
14+
15+
```bash
16+
$ npm install -g @google/clasp
17+
```
18+
19+
**Create a Clasp project**
20+
21+
Pay attention, the project must be in the src folder, not the root!
22+
23+
```bash
24+
$ cd src
25+
$ clasp login
26+
$ clasp create CreateSlidesFromDriveFolder
27+
$ clasp push
28+
$ clasp open
29+
```
30+
31+
This will open your browser and you should see the code from the src folder in the Google Script editor
32+
33+
**IMPORTANT: Never edit the code in the online editor, only in your local development environment**. We will be using Clasp to push code modifications from local development environmnent to the online testing environment.
34+
35+
#### Test your code in Google Script & Slides
36+
37+
Before you begin, create a new Slides docuemnt for testing purposes on https://slides.new . Name your presenation for easy identification
38+
39+
Now that your code is online, you have to create a test in the online editor
40+
41+
- Press the arrow on the Deploy button
42+
- Select Text Deployments
43+
- Press the settings icon next to Select type (top left of the dialog)
44+
- Select Editor Add-on
45+
- Use the default settings, select the presentation you created for testing purposes
46+
- Approve your settings
47+
- Select the test from the list and press Execute. The presentation will open
48+
- Open the Extensions menu. Your add-on should have a subment on it.
49+
- Test
50+
51+
**How to get a folder id**
52+
53+
Each folder in Google Drive has a unique ID. Just copy the id after the /folders/ part of the URL. For example, if the folder URL is https://drive.google.com/drive/folders/1I19gYQ8US-jVSFogbs_q8BZjcCu4PkCC, the id is 1I19gYQ8US-jVSFogbs_q8BZjcCu4PkCC
54+
55+
#### Add features and use Clasp to push to your testing environment
56+
57+
You coding is done in the local development machine from now on. Testing is done online though, so here is how to push your code and test.
58+
59+
- Edit the code in the src folder
60+
- Save
61+
- Open a terminal in the src folder
62+
63+
```bash
64+
$ clasp push
65+
$ clasp open
66+
```
67+
68+
- Run your test deployment
69+
- Test
70+
- Close the Slides editor
71+
- Repeat

package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "createslidesfromdrivefolder",
3+
"version": "0.0.1",
4+
"description": "A Google Slides Editor Add-on that reads all images in a Google Drive folder and creates a slide for each image in the current presentation",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "Tailor VJ",
11+
"license": "AGPLv3"
12+
}

src/Code.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function onOpen() {
2+
var ui = SlidesApp.getUi();
3+
ui.createAddonMenu()
4+
.addItem('Create Slides from Images', 'showSidebar')
5+
.addToUi();
6+
}
7+
8+
function showSidebar() {
9+
Logger.log('Showing sidebar...');
10+
var html = HtmlService.createHtmlOutputFromFile('Page')
11+
.setTitle('Select Folder')
12+
.setWidth(300);
13+
SlidesApp.getUi().showSidebar(html);
14+
}
15+
16+
function createSlidesFromImages(folderId) {
17+
if (!folderId) {
18+
Logger.log('Folder ID not provided.');
19+
return;
20+
}
21+
22+
Logger.log('Folder ID: ' + folderId);
23+
24+
// Get the folder and its files
25+
var folder = DriveApp.getFolderById(folderId);
26+
var filesIterator = folder.getFilesByType(MimeType.PNG); // Adjusted to PNG
27+
28+
// Count the total number of files
29+
var files = [];
30+
while (filesIterator.hasNext()) {
31+
files.push(filesIterator.next());
32+
}
33+
Logger.log('Total PNG files found: ' + files.length);
34+
35+
// Get the current Slides presentation
36+
var presentation = SlidesApp.getActivePresentation();
37+
var slideWidth = presentation.getPageWidth();
38+
var slideHeight = presentation.getPageHeight();
39+
40+
// Iterate through the files and add them to the Slides
41+
for (var i = 0; i < files.length; i++) {
42+
var file = files[i];
43+
Logger.log('Processing image: ' + file.getName());
44+
45+
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);
46+
var image = slide.insertImage(file);
47+
48+
// Center and stretch the image to fill the height of the Slides document
49+
var originalWidth = image.getWidth();
50+
var originalHeight = image.getHeight();
51+
var newHeight = slideHeight;
52+
var newWidth = originalWidth * (newHeight / originalHeight);
53+
54+
image.setWidth(newWidth);
55+
image.setHeight(newHeight);
56+
image.setLeft((slideWidth - newWidth) / 2);
57+
image.setTop(0);
58+
}
59+
60+
Logger.log('Total images processed: ' + files.length);
61+
}

src/Page.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<base target="_top">
5+
</head>
6+
<body>
7+
<div>
8+
<input type="text" id="folderId" placeholder="Enter folder ID">
9+
<button onclick="submitFolderId()">Submit</button>
10+
</div>
11+
<script>
12+
function submitFolderId() {
13+
var folderId = document.getElementById('folderId').value;
14+
google.script.run.createSlidesFromImages(folderId);
15+
}
16+
</script>
17+
</body>
18+
</html>

src/appsscript.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"timeZone": "Europe/Sofia",
3+
"dependencies": {
4+
"enabledAdvancedServices": [
5+
{
6+
"userSymbol": "Drive",
7+
"version": "v2",
8+
"serviceId": "drive"
9+
},
10+
{
11+
"userSymbol": "Slides",
12+
"version": "v1",
13+
"serviceId": "slides"
14+
}
15+
]
16+
},
17+
"exceptionLogging": "STACKDRIVER",
18+
"runtimeVersion": "V8"
19+
}

0 commit comments

Comments
 (0)