Search code examples
google-apps-scriptgoogle-slides-apigoogle-slides

How can I set an image title and description when inserting an image into a Google slide using Google Apps Script?


Background

I have looked through the Google API for images and there are two methods I'm interested in using getDescription() and getTitle(). Both of these method appear in the autofill in my appscript, and both seem to work.

Problem

I cannot find a method to set the description and title of an image when inserting a an image into a Google slide.

This is the method for inserting an image, but there is not argument for a title or description.

currentPage.insertImage(imageUrl, left, top, width, height)

Question

How can I set an image title and description when inserting an image into a Google slide using Google Apps Script?


Solution

  • Although I'm not sure whether this workaround is useful for your situation, how about this answer? I always use for adding the title and description using Slides API, because I couldn't find the methods in SlidesApp. The sample script is as follows. In this case, I used batchUpdate of Slides API.

    Sample script :

    var id = currentPage.insertImage(imageUrl, left, top, width, height).getObjectId();
    SlidesApp.getActivePresentation().saveAndClose(); // Important
    
    var resource = {"requests": [
      {"updatePageElementAltText": {
        "objectId": id,
        "description": "sampleDescription",
        "title": "sampleTitle"
      }
    }]};
    Slides.Presentations.batchUpdate(resource, presentationId);
    var fields = {fields: "pageElements(description,objectId,title)"};
    var ele = Slides.Presentations.Pages.get(presentationId, currentPage.getObjectId(), fields).pageElements;
    ele.forEach(function(e){
      if (e.objectId == id) {
        Logger.log(e)
      }
    });
    

    Note :

    • When you use this script
      • Please enable Slides API at advanced Google services and API console.
      • Please prepare presentationId and currentPage.
      • Please use saveAndClose() after the image is inserted.

    Reference :

    Updated at November 20, 2018:

    The Slides service was updated at Google's update at November 14, 2018, and several methods were added for achieving this issue.

    new methods let you add alt titles and alt descriptions to page elements. The following methods have been added to the Group, Image, Line, PageElement, Shape, SheetsChart, Table, Video, and WordArt classes

    • setDescription(description)
    • setTitle(title)