Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with a script to find and replace text in multiple confluence pages

Jagadeesh September 17, 2021

Hi,

I have been assigned a task to find and replace few words in confluence pages and I came across script runner as a very useful tool. I will be very thankful if anyone in this group can help me with a script that will help me to accomplish my task of finding and replacing text at the page level in confluence.
Thanks in advance,
Regards,
M J

3 answers

1 accepted

0 votes
Answer accepted
Kishan Sharma
Community Champion
September 18, 2021

Hi @Jagadeesh Welcome to the Atlassian Community!

This blog on scriptrunner site already have a script that you can download and modify as per your requirement. The example script works on a single space, but you can modify the code to loop through all your confluence spaces and its pages and perform search/replace as per your use case.

Jagadeesh September 20, 2021

Hi Kishan,

Thank you very much for your quick reply. Yes, I am using the sample script shared by the Script runner trial version tool, but it is throwing errors and I need someone who has already used this script to help me in debugging or modifying the current script. 

The sample script shows how to modify within a table in confluence page, I want to modify text within a page in confluence. I am not sure what code elements to use. if you have an idea how to modify the script, please do message me at this mail id

jmadineni@billtrust.com. I will be very thankful for helping me out.

Regards,

M Jags

Kishan Sharma
Community Champion
September 20, 2021

Hi Jagadeesh,

I will also suggest posting this question to Atlassian Developer Community so that you will get better solutions from the developers working on such scripting activities. I will also post here in case I able to get this to work.

Jagadeesh September 21, 2021

Hi Kishan,

Thanks for the suggestion. Sure, I will post in the developer community too. I have another 10 days before the trial version expires. In the meanwhile, if you know any groovy script developers please suggest their mail ids. I will try connecting with them.

0 votes
Kieran Gainer
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 4, 2023

Hey, I just publicized a Node.Js script I created and have been using for bulk replacing text within a Confluence Cloud space.

There are two .js scripts included, one for swapping page content (index.js), and one for updating page titles (updatePageTitles.js).

Hopefully this helps someone!

https://github.com/krgainer/confluence-bulk-change

Kishan Sharma
Community Champion
September 5, 2023

Welcome to the Atlassian Community and thank you for sharing the script @Kieran Gainer !

0 votes
Tiffany Wortham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 8, 2022

Hi Jagadeesh,

Here's what your script might look like if you wanted to find and replace text anywhere on particular Confluence pages:

import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import org.jsoup.Jsoup

def pageManager = ComponentLocator.getComponent(PageManager)
def spaceManager = ComponentLocator.getComponent(SpaceManager)

def targetSpace = spaceManager.getSpace("SAL")

pageManager.getPages(targetSpace, true).each { page ->
log.debug "Inspecting page ${page.title}"
def body = page.bodyContent.body
body = body.replace('Human Resources', 'HR')
pageManager.saveNewVersion(page) { pageObject ->
pageObject.setBodyAsString(body)
}
}

 

Cami
Contributor
November 10, 2022

Hello

@Tiffany Wortham Is this possible with script runner for cloud?

Tiffany Wortham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 30, 2022

Hi cami!

I'm sure this is possible with ScriptRunner for Confluence Cloud, but your script will look pretty different because you'll have to use the Confluence Cloud Rest API.

Cami
Contributor
December 1, 2022

Hey Tiffany. The Confluence Cloud REST API documentation states that connect apps cannot access the rest resource necessary to perform that action. Isn't ScriptRunner a connect app?

Like Brian Wilson likes this
stefan_baader_cpp_canon April 28, 2025

@Tiffany Wortham  Hi Tiffany, many thanks for your great blog article. I tried to adopt the script myself to change some heading tags: for example to change H1 headlines to H2. The other challenge is how to change a macro into another macro. Like {info} to be a {panel} element. Including the body off course. Maybe you can help me? I am a bit struggling with the code.
Stefan

 

Update: motivation to do that is when migrating Confluence pages to SharePoint, the TOC macro might have an issue when SharePoint knows only H2, H3 and H4. 

I achieved to replace <H1> by <strong>, <H2> by <H1>, <H3> by <H2> and <H4> by <H3>. 

Groovy script:

 

 

import com.atlassian.confluence.pages.PageManager

import com.atlassian.confluence.spaces.SpaceManager

import com.atlassian.sal.api.component.ComponentLocator

import org.jsoup.Jsoup

def pageManager = ComponentLocator.getComponent(PageManager)

def spaceManager = ComponentLocator.getComponent(SpaceManager)

def targetSpace = spaceManager.getSpace("MTS")

pageManager.getPages(targetSpace, true).each { page ->

    log.debug "Inspecting page ${page.title}"

    def body = page.bodyContent.body

    def parsedBody = Jsoup.parse(body)

    def headline1 = parsedBody.select('h1')

    if (!headline1.empty) {

        log.debug "Found table header with placeholder text: ${headline1}"

        pageManager.saveNewVersion(page) { pageObject ->

            headline1.tagName('strong')

            pageObject.setBodyAsString(parsedBody.toString())

        }

    }

    def headline2 = parsedBody.select('h2')

    if (!headline2.empty) {

        log.debug "Found table header with placeholder text: ${headline2}"

        pageManager.saveNewVersion(page) { pageObject ->

            headline2.tagName('h1')

            pageObject.setBodyAsString(parsedBody.toString())

        }

    }

    def headline3 = parsedBody.select('h3')

    if (!headline3.empty) {

        log.debug "Found table header with placeholder text: ${headline3}"

        pageManager.saveNewVersion(page) { pageObject ->

            headline3.tagName('h2')

            pageObject.setBodyAsString(parsedBody.toString())

        }

    }

    def headline4 = parsedBody.select('h4')

    if (!headline4.empty) {

        log.debug "Found table header with placeholder text: ${headline4}"

        pageManager.saveNewVersion(page) { pageObject ->

            headline4.tagName('h3')

            pageObject.setBodyAsString(parsedBody.toString())

        }

    }

}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events