Tiffany Wortham
17 November 2020
Find and replace text across multiple Confluence pages
A step-by-step on how to find and replace text across several Confluence pages at once using ScriptRunner: plus, the free script snippet to make it happen!
As part of one of our webinars, a ScriptRunner user asked us this question:
"We keep having team names changed, which means we need to replace the team names on Confluence pages. How would you go about a find and replace ScriptRunner script that finds and replaces text within a Confluence page body?"
Keep reading for guidance on how to find and replace text across several Confluence pages at once using a ScriptRunner script. This post includes a step-by-step video demo and the groovy code. Feel free to download it and use it for your own needs.
The solution
I will begin by pointing out that Confluence has a native find and replace feature (look for the magnifying glass at the right of the editor panel), but it is pretty limited since it’s only for finding and replacing text on a single page.
Imagine a space which has pages that contain tables with department names in their headers. A Confluence admin wants to abbreviate every occurrence of “Human Resources” in all table headers to “HR”. The script below will only change the text “Human Resources” to “HR” when it is found in a table header, and leaves all other occurrences of “Human Resources” as they are. You can always make this more general and choose to simply replace all occurrences of whatever text you want to replace on a page.
For a one-time use, run this script in ScriptRunner’s Script Console. This script was written with a one-time use in mind, but it can also be adapted to work with some of our built-in scripts or other ScriptRunner features, such as the CQL Service Escalation job, which will set the script up to run on a schedule so that any new pages are regularly updated.
And here's the script.
You could adjust this script to fit a wide variety of use cases, such as finding and replacing text on every page in a particular space, or any page that meets some other criteria. Additionally, as in this example, you may only want to replace particular occurrences of that text within a page.
1import com.atlassian.confluence.pages.PageManager
2import com.atlassian.confluence.spaces.SpaceManager
3import com.atlassian.sal.api.component.ComponentLocator
4import org.jsoup.Jsoup
5
6def pageManager = ComponentLocator.getComponent(PageManager)
7def spaceManager = ComponentLocator.getComponent(SpaceManager)
8
9def targetSpace = spaceManager.getSpace("SAL")
10
11pageManager.getPages(targetSpace, true).each { page ->
12 log.debug "Inspecting page ${page.title}"
13 def body = page.bodyContent.body
14 def parsedBody = Jsoup.parse(body)
15 def tableHeaderWithPlaceholder = parsedBody.select('th:contains(Human Resources)')
16 if (!tableHeaderWithPlaceholder.empty) {
17 log.debug "Found table header with placeholder text: ${tableHeaderWithPlaceholder}"
18 pageManager.saveNewVersion(page) { pageObject ->
19 tableHeaderWithPlaceholder.html("HR")
20 pageObject.setBodyAsString(parsedBody.toString())
21 }
22 }
23}
Step-by-step video
If you would like to watch a step-by-step video demo of this in action, check out this video.
ScriptRunner for Confluence is an amazingly versatile tool that lets you customise, extend and automate Confluence as much as you need. Explore what you can achieve with a 30-day free trial!