Slides

Integrating Gemini and Google Apps Script for Automated Google Slides Presentations

Gists

Abstract

Learn how Gemini AI and Google Apps Script automate Google Slides generation. A developed application demonstrates this synergy, streamlining initial presentation drafting and showcasing AI’s automation potential within Google Workspace.

Introduction

The field of AI, particularly large language models like Google’s Gemini, is advancing rapidly. A powerful application of this technology involves integrating Gemini with Google Apps Script. Google Apps Script provides a seamless way to automate tasks across Google Workspace by natively handling authorization and interaction with services like Google Docs, Google Sheets, and Google Slides. By combining Gemini’s generative capabilities with Apps Script, sophisticated automations become accessible.

Enhanced Text Manipulation in Google Slides using Google Apps Script

Gists

Description

This is a sample Google Apps Script designed to replace all instances of specific text within a Google Slides presentation, while simultaneously applying a desired text style. The built-in Presentation.replaceAllText() method within the Google Slides service is limited; it efficiently replaces text strings but lacks the functionality to modify text formatting during the replacement process. This limitation poses a challenge when aiming for styled text replacements. This report presents a detailed script solution that overcomes this constraint. The script iterates through all text elements within the slides, identifies matching text, and performs a replacement. Crucially, it also applies specified text formatting attributes, such as font, font size, color, and bold/italic settings, to the replaced text. This enhanced functionality allows users to maintain consistent and visually appealing presentations when automating text modifications. Furthermore, the script illustrates the use of TextRange methods for precise text manipulation and styling within Google Slides. This approach offers a more robust alternative to the standard replaceAllText() method, providing greater control over text replacement and formatting in Google Slides automation workflows.

Inserting Generated Text to Google Documents, Google Spreadsheets, and Google Slides using Gemini Pro API with Google Apps Script

Gists

Description

When the generated text can be automatically inserted into the cursor position of Google Document, Google Spreadsheet, and Google Slide, it will be useful for users. This report introduces sample scripts for achieving this.

Sample scripts

Here, I would like to introduce 3 sample scripts for a Google Document, a Google Spreadsheet, and a Google Slide.

Create an API key

These sample scripts request Gemini Pro API using an API key. So, please create your API key.

Retrieve Comments with Emoji Reactions from Google Documents, Google Slides, and Google Spreadsheets using Google Apps Script

Gists

Abstract

This report introduces the method for retrieving the Emoji reactions from the comments in Google Docs files (Google Documents, Google Slides, and Google Spreadsheets) using Google Apps Script.

Introduction

Recently, the Emoji reactions have been implemented in the comments on Google Docs files (Google Documents, Google Slides, and Google Spreadsheets). Ref With this implementation, the collaborative work has been higher. Here, it is considered that when the Emoji reactions can be retrieved from the Google Docs files, the statistics of the reactions will be also more useful for increasing collaboration. This report introduces a sample script for retrieving Comments including the Emoji reactions from Google Docs files.

Managing Row Height and Column Width of Table on Google Slides using Google Apps Script

Gists

This is a sample script for managing the row height and the column width of a table on Google Slides using Google Apps Script.

In the current stage, Google Slides service (SlidesApp) cannot manage the row height and the column width of the table on Google Slides, while the table width and height can be managed. But, fortunately, when Google Slides API is used, this can be achieved.

In this post, I would like to introduce a sample script for managing the row height and the column width of a table on Google Slides using Google Apps Script.

Workaround: Starting Animation GIF on Google Slide by Clicking

Gists

Introduction

This is a simple workaround for starting an animation GIF on Google Slide by clicking.

When an animation GIF is inserted into a slide of Google Slides, the animation is automatically started. By this, the timing for starting cannot be controlled by the user side. In this post, I would like to introduce a workaround for resolving this issue. Preparation The sample flow is as follows.

Prepare a sample animation GIF.

  1. Create a new Google Slide.
  2. Insert the animation GIF into a slide.
  3. Insert a rectangle shape of the same size as the animation GIF, and put over this to the animation GIF. And then, please set the order of the created shape to “Send to back”. By this, the animation GIF can be seen.
  4. Select the animation GIF and open “Motion” from “Insert” → “Animation”.
  5. Use “Appear (On Click)”.

By this flow, the preparation is finished.

Updated: GAS Library - DocsServiceApp

Overview

This is a Google Apps Script library for supporting Document service, Docs API, Spreadsheet service, Sheets API, Slides service and Slides API. The aim of this library is to compensate the processes that they services cannot achieve.

DocsServiceApp was updated to v1.2.0

  • v1.2.0 (September 29, 2022)

    1. Added a new method of getNamedFunctions(). This method can retrieve the named functions from Google Spreadsheet.

You can see the detail information here https://github.com/tanaikech/DocsServiceApp

Updated: GAS Library - DocsServiceApp

Overview

This is a Google Apps Script library for supporting Document service, Docs API, Spreadsheet service, Sheets API, Slides service and Slides API. The aim of this library is to compensate the processes that they services cannot achieve.

DocsServiceApp was updated to v1.1.0

  • v1.1.0 (September 28, 2022)

    1. Added a new method of getQuotePrefixCells(). This method can detect the cells with the quote prefix cells.

You can see the detail information here https://github.com/tanaikech/DocsServiceApp

Inverting Selected Objects on Google Slides using Google Apps Script

Gists

This is a sample script for inverting the selected objects on Google Slides using Google Apps Script.

I have the case that I want to invert the selected objects on Google Slides. This sample script can be achieved this goal using Google Apps Script.

Sample script

Please copy and paste the following script to the script editor of Google Slides, and save the script. And, please select the objects on the Slide and run the function main(). By this, the selected objects are inverted.

Exporting All Thumbnail Images Retrieved from Google Slides as Zip File using Google Apps Script

Gists

This is a sample script for exporting all thumbnail images retrieved from Google Slides as a zip file using Google Apps Script.

Sample script

Before you use this script, please enable Slides API at Advanced Google services. Ref

function myFunction() {
  const presentationId = "###"; // Please set Google Slides ID.
  const folderId = "###"; // Please set the folder ID.
  const outputFilename = "###"; // Please set the output filename.

  const blobs = SlidesApp.openById(presentationId)
    .getSlides()
    .map((e, i) =>
      UrlFetchApp.fetch(
        Slides.Presentations.Pages.getThumbnail(
          presentationId,
          e.getObjectId(),
          {
            "thumbnailProperties.mimeType": "PNG",
            "thumbnailProperties.thumbnailSize": "LARGE",
          }
        ).contentUrl
      )
        .getBlob()
        .setName(`page${("000" + (i + 1)).slice(-3)}.png`)
    );
  DriveApp.getFolderById(folderId).createFile(
    Utilities.zip(blobs).setName(outputFilename + ".zip")
  );
}

Reducing Table Height of Table Inserted from Google Spreadsheet to Google Slides using Google Apps Script

Gists

This is a sample script for reducing the table height of the table inserted from Google Spreadsheet to Google Slides using Google Apps Script.

Sample script

Please copy and paste the following script to the script editor of Google Slides. This sample script uses Slides API. So, please enable Slides API at Advanced Google services. Ref

As the sample situation, this script supposes that a table is manually copied from Google Spreadsheet to the 1st slide of Google Slides. So when you test this script, please copy the cells from Google Spreadsheet to the 1st slide of Google Slides and run the function.

Simply Editing Texts of Texts Boxes on Google Slides using Google Apps Script

Gists

This is a sample script for simply editing the texts of texts boxes on Google Slides using Google Apps Script. The supposed situation is as follows.

  • Google Slides has several text boxes of the same size and the same position.
  • You want to retrieve the list of texts from the text boxes and want to change the texts using a simpler method.

In this case, I thought that when the sidebar created by Google Apps Script is used for changing the texts, your goal might be able to be simply achieved.

Simple Photo Gallery Created by Google Slides and Web Apps using Google Apps Script

Overview

This is a sample script for achieving a simple photo gallery created by Google Slides and Web Apps using Google Apps Script.

Description

At Google, there is a great Google Photos. Ref Recently, I was required to have a simple photo gallery. At that time, I thought that when an independence photo gallery instead of Google Photos can be used, it will be useful. Also, this might be useful for other users. So I published this.

Creating Custom Grid View of Google Slides as Image and Spreadsheet using Google Apps Script

Gists

This is a sample script for creating the custom grid view of Google Slides as an image using Google Apps Script.

Demo

Usage

In order to use this script, please do the following flow.

1. Install GAS library

This sample script uses a library of DocsServiceApp. So please install DocsServiceApp. You can see the method for installing it at here.

2. Enable APIs

This sample script uses 2 APIs of Drive API and Slides API. So please enable them at Advanced Google services. Ref

Adding Slide Page Link to Shape using Google Apps Script

Gists

This is a sample script for adding the slide page link to the shape using Google Apps Script.

When I use Google Slides, there is the case that I want to jump to the specific slide on the same Google Slides. And, I have the case that I want to jump from the last slide to the 1st slide. In those cases, I had manually added the slide page link to each shape. But when the number of slides are large, I thought that when a script for achieving this is prepared, it will be useful. So I created this.

GAS Library - DocsServiceApp

Overview

This is a Google Apps Script library for supporting Document service, Docs API, Spreadsheet service, Sheets API, Slides service and Slides API. The aim of this library is to compensate the processes that they services cannot achieve.

Description

The Google services, which are Document service, Docs API, Spreadsheet service, Sheets API, Slides service and Slides API, are growing now. But, unfortunately, there are still the processes that they cannot done. I created this GAS library for supporting the Google services.

Decoding QR code on Google Slides using Google Apps Script

Gists

This is a sample script for decoding a QR code put in Google Slides using Google Apps Script. In this case, Javascript is used at the opened dialog. And Canvas API and jsQR are used. So unfortunately, this method cannot be used with the time-driven trigger and the Google Apps Script project of the standalone type.

Of course, this method can be also used for Google Document and Google Spreadsheet. But at Google Spreadsheet, I recommend to retrieve the image of QR code with the fetch method from URL.

Cropping Images in Google Slides using Google Apps Script

Gists

This is a sample script for cropping images in the Google Slides using Google Apps Script. In the current stage, in order to crop the images in Google Slides, it is required to use replace(blobSource, crop) Because, although there is the “cropProperties” of “UpdateImagePropertiesRequest” in Slides API, unfortunately, in the current stage, this cannot be still used. This has already been reported. Ref

About cropping using replace(blobSource, crop), I thought that how to use might be a bit difficult. So here, I would like to introduce a sample script for using replace(blobSource, crop).

Managing Texts on Google Slides using Google Apps Script

Gists

This is a sample script for managing the texts on Google Slides using Google Apps Script. Recently, I got the request like this. I published this here, because I thought that this might be also useful for other users.

Demo

In this demonstration, the text of {{baz}} on Google Slides are searched and replaced to other text, and also, the text style is changed.

Sample situation

In this case, it supposes that there are 3 types of shapes in the slide. Those are the text box, the rectangle shape and the explosion shape. Each shape has the texts and the title of the shape like shape1, shape2 and shape3. The sample script searches the shape using the title. You can see it at the following image.

Figma to Google Slides using Google Apps Script

Gists

In this sample script, all pages in the Figma file are retrieved and the retrieved pages are put to new Google Slides as the image.

Usage

1. Retrieve access token

You can see the method for retrieving the access token at here. Although there is also OAuth2 for retrieving the access token, in your situation, I thought that the method for directly generating the access token on the site might be suitable. So in this answer, the generated access token on the site is used. Please retrieve the access token as follows.

Limitations for Inserting Images to Google Docs

Gists

When an image is inserted to Google Docs (Spreadsheet, Document and Slides) using the method of insertImage using Google Apps Script, there is the case that the error occurs. The error messages are “server error” and “invalid image data”. Here, I would like to introduce the limitations for inserting images to Google Docs. As the result, it was found that the limitation is due to both the mimeTypes and the area of image rather than the file size.

Summarizing Slides as Thumbnails

Gists

This is a sample script for summarizing Slides as thumbnails. For example, it supposes a Slides including 15 pages. When this script is run, it summarizes 6 pages to one page as images. I created this because there are no methods for directly achieving this. This is useful for myself. If this is also useful for you, I’m glad.

The flow of this workaround is as follows.

Flow:

  1. Copy the original Slides file as a temporary file.
  2. Retrieve images of all slides.
    • When the Slides file is exported to PNG file, the top page is exported as PNG file. I used this.
  3. Put and arrange the retrieved images to the temporary file.
    • When the image is inserted to a slide, retrieve the size and change the size, then put the image to the calculated position.

Sample script 1:

function myFunction() {
  // Please set these parameters
  var id = "### file ID ###"; // file ID of original Slides
  var col = 3; // Number of columns
  var row = 2; // Number of rows
  var wsize = 200; // Size of width of each image (pixels)
  var sep = 5; // Space of each image (pexels)


  // Create temporary file
  var originalFile = DriveApp.getFileById(id);
  var tempFile = originalFile.makeCopy();
  var idt = tempFile.getId();
  
  // Retrieve slides as images
  var s = SlidesApp.openById(idt);
  var slides = s.getSlides();
  var accessToken = ScriptApp.getOAuthToken();
  var baseUrl = "https://docs.google.com/presentation/d/" + idt + "/export/";
  var url = baseUrl + "png?access_token=" + accessToken;
  var blobs = slides.map(function(e) {
    var blob = UrlFetchApp.fetch(url).getBlob();
    slides[0].remove();
    s.saveAndClose();
    s = SlidesApp.openById(idt);
    slides = s.getSlides();
    return blob;
  });

  // Put images 
  var ph = s.getPageHeight();
  var pw = s.getPageWidth();
  var leftOffset = (pw - ((wsize * col) + (sep * (col - 1)))) / 2;
  if (leftOffset < 0) throw new Error("Images are sticking out from a slide.");
  var len = col * row;
  var loops = Math.ceil(blobs.length / (col * row));
  for (var loop = 0; loop < loops; loop++) {
    var ns = s.insertSlide(loop);
    var topOffset, top;
    var left = leftOffset;
    for (var i = len * loop; i < len + (len * loop); i++) {
      if (i === blobs.length) break;
      var image = ns.insertImage(blobs[i]);
      var w = image.getWidth();
      var h = image.getHeight();
      var hsize = h * wsize / w;
      if (i === 0 || i % len === 0) {
        topOffset = (ph - ((hsize * row) + sep)) / 2;
        if (topOffset < 0) throw new Error("Images are sticking out from a slide.");
        top = topOffset;
      }
      image.setWidth(wsize).setHeight(hsize).setTop(top).setLeft(left).getObjectId();
      if (i === col - 1 + (loop * len)) {
        top = topOffset + hsize + sep;
        left = leftOffset;
      } else {
        left += wsize + sep;
      }
    }
  }
  s.saveAndClose();
}

Sample script 2:

When you use this script, please enable Slides API at API console.

GAS Library - ArrangeStackingOrder

Overview

ArrangeStackingOrder is a GAS library for arranging the stacking order of page elements on Google Slides using Google Apps Script (GAS).

Demo

This is a demonstration of this library when this is used as a Google Slides Addon.

Description

Do you have situations that you want to arrange the stacking order of page elements on Google Slides using GAS? I had it before. At that time, I could achieve it by creating a simple script. Recently, I found that users who have the same situation. I thought that if there is a library for this, it will be useful for me and other developers. So I created this. If this was useful for your situation, I’m glad.

Add-on - ShapeApp

ShapeApp for Google Slides was published as an add-on application

When you use Google Slides, have you ever thought about creating and updating shapes on Slides by inputting parameters, and arranging selected shapes? I have thought about them. Recently, since Class SlidesApp was added to GAS, it came to be able to easily to create various applications for Slides. So I created this. This application is add-on application which was made of GAS.

Difference Between Given Values and Retrieved Values for Shapes on Google Slides

Gists

This is a document for explaining the difference between given values and retrieved values for shapes on Google Slides. When a shape is created to a slide using Slides API, most users give the size of height and width as pt. When the size is retrieved from the created shape as pt, the size is often difference from the given size.

For example, when a square shape is created by giving the height and width of 100 pt, the size which is retrieved from the created square becomes 99.99212598425197 pt for the height and width.

Retrieving Size of Tables in Google Slides using Google Apps Script

Gists

This sample script is for retrieving the size (width and height) of a table in Google Slides using Google Apps Script.

There are no methods for directly retrieving the table size using SlidesApp yet. So I thought of a workaround using Slides API.

  • When the slide information is retrieved using Slides.Presentations.Pages.get() of Slides API, the information of tables is also included. In the information, the height and width of table are also included.
  • But the unit is EMU (English Metric Unit), and the height and width is separated by each cell. So it is required to sum each height and width while modifying the unit.

The modified script reflected this is as follows.

SlideApp for Google Slides

Gists

By recent Google updated, Class SlideApp is added to Google Slides. SlideApp will be bring a lot of applications. Here, I would like to introduce 2 samples.

1. Sidebar

function showSidebar() {
  var html = HtmlService
    .createHtmlOutput('Hello, world! <input type="button" value="Close" onclick="google.script.host.close()" />')
    .setTitle('My custom sidebar')
    .setWidth(300);
  SlidesApp.getUi().showSidebar(html);
}

2. Copy slides in existing Slide to a new Slide

This sample script create a new Slide with slides you want to copy.