|
| 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] |
0 commit comments