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

How to attach a link to a shape or text box on Apps Script?


I know how to add a shape to a slide, but I know in Google Slides you are able to link another slide to display once you click on that text box. Is it possible to in App Script create a shape and then attach a link to it?

I have looked at the API documentation and was confused as to if it was possible.

This is a Sheets/Slides Add-On.

What I need to do is when a button is pressed on my Add On menu that I created, is to create a Slide in Google Slides, and use the information from a Sheets. I want to create a TextBox, and have when you click it link to another slide in the Slide Show. I just want it all automated.

If it is possible, how do I apply it to a shape I created?

var elementId = 'MyTextBox_001';
  var pt350 = {
    magnitude: 350,
    unit: 'PT'
  };
  var requests = [{
    createShape: {
      objectId: elementId,
      // tried linkUrl: "link";
      shapeType: 'TEXT_BOX',
      elementProperties: {
        pageObjectId: "slide_001_001",

        size: {
          height: pt350,
          width: pt350
        },
        transform: {
          scaleX: 1,
          scaleY: 1,
          translateX: 350,
          translateY: 100,
          unit: 'PT'
        }
      }
    }
  },

// Insert text into the box, using the supplied element ID.
{
   insertText: {
     objectId: elementId,
     // tried linkUrl: "link";
     insertionIndex: 0,
     text: SpreadsheetApp.getActiveSheet().getRange(3,2).getValue()
    }
 }];

// have tried elementId.setLinkUrl(linkHere);

Solution

  • You can create a slide, add a shape and add a link to it using the following code:

      var presentationId =  "<YOUR_PRESENTATION_ID>";
      var newSlideId = Utilities.getUuid();
      var newElementId = Utilities.getUuid();
      var pt350 = {
        magnitude: 350,
        unit: 'PT'
      };
      var requests = [
        {
          createSlide: {
            objectId: newSlideId
          }
        },
        {
          createShape: {
            objectId: newElementId,
            shapeType: 'TEXT_BOX',
            elementProperties: {
              pageObjectId: newSlideId,
              size: {
                height: pt350,
                width: pt350
              },
              transform: {
                scaleX: 1,
                scaleY: 1,
                translateX: 350,
                translateY: 100,
                unit: 'PT'
              }
            }
          }
        },
        {
          insertText: {
            objectId: newElementId,
            insertionIndex: 0,
            text: SpreadsheetApp.getActiveSheet().getRange(3,2).getValue()
          }
        },
        {
          updateShapeProperties: {
            objectId: newElementId,
            shapeProperties: {
              link: {
                pageObjectId: "<YOUR_PAGE_ID>"
              }
            },
            fields: "link"
          }
        }];
    

    Note that you will need your presentation's id and the destination page's id. If you prefer, you can use a slide index as a destination (more information here).

    In case you need more help creating a slides add-on, you can visit this example.

    If you have any other question regarding this topic, please don't hesitate to reach back.

    Regards