Node Find and Replace
This guide demonstrates how to search for nodes in documents and replace the content of those nodes.
Before you begin
You should get your API key from your UniCloud account.
If this is your first time using UniOffice SDK, follow this guide to set up a local development environment.
Clone the project repository
In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.
git clone https://github.com/unidoc/unioffice-examples
To get the example navigate to the path document/node-find-and-replace
folder in the unioffice-examples directory.
cd unioffice-examples/document/node-find-and-replace/
Here is the images from the file that will be used in this example:
How it works
Lines 12-19
import the UniOffice packages and other required dependencies.
The init function in lines 21-28
authenticates your request with your UNIDOC_LICENSE_API_KEY
.
In the main function from line 30 to 63
, the document is opened, and all its nodes are obtained using the Nodes
function in line 38
.
Then, from line 41 to 44
, the FindNodeByText
function is used, which searches for nodes with the text Cell 1
and returns them as a list. Subsequently, the ReplaceText
function is used to replace all occurrences of Cell 1
as text with Cell Replacement
.
In lines 50-59
, it retrieves all nodes that match a specific Regex expression using the FindNodeByRegexp
function, also obtaining a list. Then, it replaces anything that matches that expression with the text Why I am replaced?
using the ReplaceTextByRegexp
function in line 56
.
Finally, the file with the changes is saved in line 59
.
Run the code
Run this command to perform the node find and replace.
go run main.go