google-apps-scriptgoogle-sheetshyperlinkgoogle-slides-api

How to append hyperlink to replaceAllText command in Google Apps Script


Basing a script off of this tutorial - https://spreadsheet.dev/convert-each-row-google-sheets-into-slide-google-slides-apps-script, works perfectly fine. However I'm trying to populate a hyperlink as part of the script, i.e in my script

slide.replaceAllText("{{projectName}}", row[0]);
slide.replaceAllText("{{userStory}}", row[1]);
slide.replaceAllText("{{POC}}", row[2]);
slide.replaceAllText("{{stakeholderName}}", row[3]);
slide.replaceAllText("{{brief}}", row[4]);

"brief" in column 5 on my spreadsheet is a URL, and I'd like to have it not appear as the URL in my slide deck but as a hyperlink on the word "Brief".


Solution

  • I believe your goal is as follows.

    In this case, how about modifying your script as follows?

    From:

    slide.replaceAllText("{{brief}}", row[4]);
    

    To:

    slide.getShapes().forEach(shape => {
      const text = shape.getText();
      if (text.asString().includes("{{brief}}")) {
        text.setText("Brief").getTextStyle().setLinkUrl(row[4]);
      }
    });
    

    Reference: