Skip to content

Commit 3976323

Browse files
committed
refactor: Slides image and UI dialog samples into individual files
1 parent cf80358 commit 3976323

File tree

15 files changed

+378
-259
lines changed

15 files changed

+378
-259
lines changed

.github/scripts/biome-gs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ async function main() {
8888
}
8989

9090
// 2. Run Biome
91-
const biomeArgs =
92-
command === "format" ? "check --write ." : "check .";
91+
const biomeArgs = command === "format" ? "check --write ." : "check .";
9392
console.log(`Running biome ${biomeArgs}...`);
9493
try {
9594
const { stdout, stderr } = await execAsync(

GEMINI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This guide outlines best practices for developing Google Apps Script projects, f
77
* For new sample directories, ensure the top-level folder is included in the [`test.yaml`](.github/workflows/test.yaml) GitHub workflow's matrix configuration.
88
* Do not move or delete snippet tags: `[END apps_script_... ]` or `[END apps_script_... ]`.
99
* Keep code within snippet tags self-contained. Avoid depending on helper functions defined outside the snippet tags if the snippet is intended to be copied and pasted.
10-
10+
* Avoid function name collisions (e.g., multiple `onOpen` or `main` functions) by placing separate samples in their own directories or files. Do not append suffixes like `_2`, `_3` to function names. For variables, replace collisions with a more descriptive name.
1111

1212
## Tools
1313

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START apps_script_slides_image_add_image]
18+
/**
19+
* Adds an image to a presentation at a given slide index.
20+
* @param {string} imageUrl The image URL
21+
* @param {number} index The slide index to add the image to
22+
*/
23+
function addImageSlide(imageUrl, index) {
24+
const slide = deck.appendSlide(SlidesApp.PredefinedLayout.BLANK);
25+
const image = slide.insertImage(imageUrl);
26+
}
27+
// [END apps_script_slides_image_add_image]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START apps_script_slides_image_add_image_slide]
18+
/**
19+
* Creates a single slide using the image from the given link;
20+
* used directly by foreach(), hence the parameters are fixed.
21+
* @param {string} imageUrl A String object representing an image URL
22+
* @param {number} index The index into the array; unused (req'd by forEach)
23+
*/
24+
function addImageSlide(imageUrl, index) {
25+
const slide = deck.appendSlide(SlidesApp.PredefinedLayout.BLANK);
26+
const image = slide.insertImage(imageUrl);
27+
const imgWidth = image.getWidth();
28+
const imgHeight = image.getHeight();
29+
const pageWidth = deck.getPageWidth();
30+
const pageHeight = deck.getPageHeight();
31+
const newX = pageWidth / 2 - imgWidth / 2;
32+
const newY = pageHeight / 2 - imgHeight / 2;
33+
image.setLeft(newX).setTop(newY);
34+
}
35+
// [END apps_script_slides_image_add_image_slide]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START apps_script_slides_image_create]
18+
const NAME = "My favorite images";
19+
const deck = SlidesApp.create(NAME);
20+
// [END apps_script_slides_image_create]

slides/imageSlides/full/full.gs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START apps_script_slides_image_full_script]
18+
const NAME = "My favorite images";
19+
const presentation = SlidesApp.create(NAME);
20+
21+
/**
22+
* Creates a single slide using the image from the given link;
23+
* used directly by foreach(), hence the parameters are fixed.
24+
* @param {string} imageUrl A String object representing an image URL
25+
* @param {number} index The index into the array; unused (req'd by forEach)
26+
*/
27+
function addImageSlide(imageUrl, index) {
28+
const slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);
29+
const image = slide.insertImage(imageUrl);
30+
const imgWidth = image.getWidth();
31+
const imgHeight = image.getHeight();
32+
const pageWidth = presentation.getPageWidth();
33+
const pageHeight = presentation.getPageHeight();
34+
const newX = pageWidth / 2 - imgWidth / 2;
35+
const newY = pageHeight / 2 - imgHeight / 2;
36+
image.setLeft(newX).setTop(newY);
37+
}
38+
39+
/**
40+
* The driver application features an array of image URLs, setting of the
41+
* main title & subtitle, and creation of individual slides for each image.
42+
*/
43+
function main() {
44+
const images = [
45+
"http://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
46+
"http://www.google.com/services/images/phone-animation-results_2x.png",
47+
"http://www.google.com/services/images/section-work-card-img_2x.jpg",
48+
"http://gsuite.google.com/img/icons/product-lockup.png",
49+
"http://gsuite.google.com/img/home-hero_2x.jpg",
50+
];
51+
const [title, subtitle] = presentation.getSlides()[0].getPageElements();
52+
title.asShape().getText().setText(NAME);
53+
subtitle
54+
.asShape()
55+
.getText()
56+
.setText("Google Apps Script\nSlides Service demo");
57+
for (const imageUrl of images) {
58+
addImageSlide(imageUrl);
59+
}
60+
}
61+
// [END apps_script_slides_image_full_script]

slides/imageSlides/imageSlides.gs

Lines changed: 0 additions & 122 deletions
This file was deleted.

slides/imageSlides/main/main.gs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START apps_script_slides_image_main]
18+
/**
19+
* Adds images to a slides presentation.
20+
*/
21+
function main() {
22+
const images = [
23+
"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
24+
"http://www.google.com/services/images/phone-animation-results_2x.png",
25+
"http://www.google.com/services/images/section-work-card-img_2x.jpg",
26+
"http://gsuite.google.com/img/icons/product-lockup.png",
27+
"http://gsuite.google.com/img/home-hero_2x.jpg",
28+
];
29+
const [title, subtitle] = deck.getSlides()[0].getPageElements();
30+
title.asShape().getText().setText(NAME);
31+
subtitle
32+
.asShape()
33+
.getText()
34+
.setText("Google Apps Script\nSlides Service demo");
35+
for (const imageUrl of images) {
36+
addImageSlide(imageUrl);
37+
}
38+
}
39+
// [END apps_script_slides_image_main]

ui/dialogs/alert/alert.gs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START apps_script_alert_dialogs]
18+
/**
19+
* Creates a custom menu when a user opens a Spreadsheet.
20+
*/
21+
function onOpen() {
22+
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
23+
.createMenu("Custom Menu")
24+
.addItem("Show alert", "showAlert")
25+
.addToUi();
26+
}
27+
28+
/**
29+
* Shows an alert dialog.
30+
*/
31+
function showAlert() {
32+
const ui = SpreadsheetApp.getUi(); // Same variations.
33+
34+
const result = ui.alert(
35+
"Please confirm",
36+
"Are you sure you want to continue?",
37+
ui.ButtonSet.YES_NO,
38+
);
39+
40+
// Process the user's response.
41+
if (result === ui.Button.YES) {
42+
// User clicked "Yes".
43+
ui.alert("Confirmation received.");
44+
} else {
45+
// User clicked "No" or X in the title bar.
46+
ui.alert("Permission denied.");
47+
}
48+
}
49+
// [END apps_script_alert_dialogs]

0 commit comments

Comments
 (0)