Search code examples
google-slides-api

Is it possible to move a Shape from a slide to another?


I use the Google Slides API on a NodeJS server to edit a presentation and I can't find anything in the documentation on moving an object to another slide, a Shape for example.


Solution

  • Answer:

    You have to do this by getting the shape from the response of presentations.pages.get, removing it, and inserting it with presentations.batchUpdate.

    More Information:

    In order to 'move' an object from one slide to another using the API, you in fact have to make two requests: one to remove the current object, and one to insert it into the new slide.

    Firstly, you will need to make a request to presentations.pages.get in order to get all PageElement objects in the page. As per the documentation, a Shape is an instance of a PageElement object which represents a shape on a slide.

    The response of presentations.pages.get will be a Page resource:

    {
      "objectId": string,
      "pageType": enum (PageType),
      "pageElements": [
        {
          object (PageElement)
        }
      ],
      "revisionId": string,
      "pageProperties": {
        object (PageProperties)
      },
    
      // Union field properties can be only one of the following:
      "slideProperties": {
        object (SlideProperties)
      },
      "layoutProperties": {
        object (LayoutProperties)
      },
      "notesProperties": {
        object (NotesProperties)
      },
      "masterProperties": {
        object (MasterProperties)
      }
    }
    

    The Shape will be contained within the response['pageElements'] resource from this request and will be of the form:

    {
      "objectId": string,
      "size": {
        object (Size)
      },
      "transform": {
        object (AffineTransform)
      },
      "title": string,
      "description": string,
    
      // Union field element_kind can be only one of the following:
      "elementGroup": {
        object (Group)
      },
      "shape": {
        "shapeType": enum (Type),
        "text": {
          object (TextContent)
        },
        "shapeProperties": {
          object (ShapeProperties)
        },
        "placeholder": {
          object (Placeholder)
        }
      },
    }
    

    Once you have obtained the Shape object out of the response you get from presentations.pages.get, you will need to then create a CreateShapeRequest out of the retrieved properties:

    {
      "objectId": string,
      "elementProperties": {
        object (PageElementProperties)
      },
      "shapeType": enum (Type)
    }
    

    And a DeleteObjectRequest which can be used to remove the Shape on the previous slide:

    {
      "objectId": string
    }
    

    The DeleteObjectRequest and CreateShapeRequest can be both contained inside the same batchUpdate request. The request body should be of the form:

    {
      "requests": [
        {
          object (Request)
        }
      ],
      "writeControl": {
        object (WriteControl)
      }
    }
    

    The full documentation for the batchUpdate method can be seen here.

    References: