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.
Here is the images from the file that will be used in this example:
How it works
12-19 import the UniOffice packages and other required dependencies.
The init function in lines
21-28 authenticates your request with your
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
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
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
Finally, the file with the changes is saved in line
Run the code
Run this command to perform the node find and replace.
go run main.go