Replace text with link in a Google Doc

Describe the issue/error/question

Hello, I’m using an http node to update a google doc. I’m replacing a list of words by another using the google doc api and the following commands:

"requests": [
  {
    "replaceAllText": {
       "containsText": {
          "text": "{{to-replace}}",
          "matchCase": "false"
       },
       "replaceText": "Here is n8n"
    }
  }
]

It works fine.

But now, I would like to transform these texts into links.

For example in a google doc, I have the tag {{to-replace}} and I would like to change it to the label “Here is n8n” and that text should now links to the url “https://n8n.io

I found an article explaining on how to do it using Google Apps Script:

function singleLinkWithinParagraph(){
  // ## Inputs ##
  let text = "Here is n8n";
  let url = "https://n8n.io/";
  let textToFind = "{{to-replace}}";
  // ############
 
  let body = DocumentApp.getActiveDocument().getBody();
  
  let foundText = body.findText(textToFind);
 
  // Get the start and end location of the text in the paragraph.
  let startText = foundText.getStartOffset();
  let endText = startText + text.length - 1;
 
  // Get the element indext for this section of text.
  let element = foundText.getElement();
 
  // Replace the text and insert the URL.
  element.asText()
         .replaceText(textToFind, text)
         .setLinkUrl(startText, endText, url);
};

But I have no idea how to do it using n8n…

Any idea?

Hey @G-Rom,

Welcome to the community :tada:

It looks like there is a text style that you can set to link and add a URL to, I am not sure how it would look with a replace though.

Update Text Style: Requests  |  Google Docs API  |  Google Developers
Text Styles: REST Resource: documents  |  Google Docs API  |  Google Developers
Link: REST Resource: documents  |  Google Docs API  |  Google Developers

Yes, you’re right, I’ve seen it too. But I’m struggling to find the range of the text to replace throughout the api.

Hi @G-Rom, you might want to take a look at Looping series from Google sheet to a google doc - #2 by MutedJam

This workflow includes an approach to find the start index of a table cell but might help with finding your way around programming Google Docs in general.

1 Like

thanks, I have finally achieved what I have in mind by parsing the data from a get document node, searching links, record range and then update them using a batch update with http request.

Thanks for the help.

1 Like

Hello G-Rom

Is it possible to add your result it may help people looking for the same :wink:

@G-Rom , Please post the solution would really help, thank-you.

Hi all, here is my workflow to replace links (and also “tags”) in a google doc.

A few comments:

  • it takes a list of replacements as inputs (search/replace): you can have property like [[property1]] is replaced by “value1” or you can also have replacements of link like: “xxxx”(which is a part of a link, for example a google id), is replaced by another google id link.
  • first node loads google doc content
  • second node adds the content of the node to a property
  • third node searches links in the content and prepare a replacement command
  • fourth node searches for text occurrences to be replaced
  • final node update the google doc file using all the replacement commands.

Let me know if you have any question

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.