Search code examples
google-apps-scripthyperlinkgoogle-slides-api

How can i insert hyperlink into a text with Google slide API (google app script)


I would like to insert a link into a text (word), but I can't find a way to do the trick. There is no function in the offical Documentation https://developers.google.cn/apps-script/reference/slides/page-element .

I tried something like this:

var pageElement = SlidesApp.openById('12hh9vWVhFdj9kH51U-07Rc5yc9gKitJ3f_sr1I38lQQYUDB').getSlides()[0]; pageElement.replaceAllText("Bonjour", "Aurevoir" + **link associated**);

The second line is where i can't find a way to do the trick.

How to insert a hyperlink into the text replaced ?

Thanks


Solution

  • To set / update a link you need to use the Advanced Slides Service based on the Slides API

    • After enabling the advanced service you can use the Slie API method presentations.batchUpdate with the request updateTextStyle
    • This allows you to specify a hyperlink, whereby the id of the shape in which the text of interest is located, its start and end index are required parameters that you have to retrieve in advance
    • So, you need to perform two different requests separately - change the text itself and then the hyperlink
    • It is important to use saveAndClose() to make sure that the two requests don't timely overlap
    • For the latter to work as intended, your script should be alone-standing rather bound to the open Slides document

    Sample code:

    function myFunction() {  
      var presentationId = "XXXX";
      var newText = "Aurevoir";
      var oldText = "Bonjour";
      var presentation = SlidesApp.openById(presentationId);
      var pageElement = presentation.getSlides()[0]; 
      pageElement.replaceAllText(oldText, newText);
      presentation.saveAndClose();
      var newLink = "www.stackoverflow.com";
      var pageElement = SlidesApp.openById(presentationId).getSlides()[0]; 
       
      var elements = pageElement.getPageElements();
      var requests =[];
      for( var i = 0; i < elements.length; i++){
        var element = elements[i];
        if(element.getPageElementType() == "SHAPE"){
          var text = element.asShape().getText().find(newText);
          if(text.length > 0){
            var objectId = element.getObjectId();
            for (var j =0; j < text.length; j++){
              var start = text[j].getStartIndex();
              var end = text[j].getEndIndex();
              requests.push(
                {
                  updateTextStyle: {
                    objectId: objectId,
                    textRange: {
                      startIndex: start,
                      endIndex: end,
                      type: "FIXED_RANGE"
                    },
                    fields: 'link',
                    style: {
                      link: {
                        url: newLink
                      }
                    }
                  }
                }
              );
              Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
            }
          } 
        }
      }
    }