Search code examples
google-slides-apigoogle-slides

Linking shapes to slides using text search in Google Slides script


Trying to link shapes to slides the same way it's done with images. enter image description here enter image description here

.

.

Reason for this request is linking images seems much harder in terms of locating the exact one to be linked.

Have realized it might be best to link shapes through match/search text then insert the images after.

Codes attempted though please ignore if completely irrelevent.

function myFunction(){
    var searchText = "IMAGE1";

  var presentation = SlidesApp.getActivePresentation();
  var slide = presentation.getSlides()[4];
  

  // 2. Replace the shape which has the text of "searchText" with the image of "imageUrl".
  slide.getShapes().forEach(s => {
    if (s.getText().asString().toLocaleUpperCase().includes(searchText.toLocaleUpperCase())) {
      s.setLinkSlide('INSERT_SLIDE_LINK');
      
      }
  }
  )
}

Slides Example

Thank you


Solution

  • I believe your goal is as follows.

    • You want to link the shape to a slide by searching the text in the shape on Google Slides.
    • You want to achieve this using Google Apps Script.

    setLinkSlide can use Slides Object. I thought that this might be able to be used.

    Sample script:

    function myFunction() {
      const obj = { text1: 3, text2: 3, text3: 4, text4: 5, text5: 4, text6: 3 }; // This is from your showing sample image.
    
      const slides = SlidesApp.getActivePresentation().getSlides();
      slides.forEach(s => {
        s.getShapes().forEach(shape => {
          const t = obj[shape.getText().asString().toLowerCase().trim()];
          if (t) {
            shape.setLinkSlide(slides[t - 1]);
          }
        });
      });
    }
    

    Note:

    • This sample script is for your provided sample Google Slides. When you change this, please modify obj. Please be careful about this.

    Reference: