Using Rich Text Formatting

This guide demonstrates how to format cell content using rich text formatting in a spreadsheet using UniOffice.

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 spreadsheet/rich-text folder in the unioffice-examples directory.

cd unioffice-examples/spreadsheet/rich-text/

How it works

The import section in lines 4-12 imports the necessary libraries. The init function defined in lines 14-21 sets the license key using license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)).

The main function starts in line 23. It starts by creating a new spreadsheet. Then using the nested for loops in lines 30-48 creates the necessary table with rich text. The outer loop creates a new row at every iteration using sheet.AddRow(). Then the inner loop creates the cells with rich text.

In line 36 it creates a spreadsheet.RichText object from the cell using cell.SetRichTextString(). Then a spreadsheet.RichTextRun is created in line 37. The text, color and boldness properties are set to this RichTextRun object. Then a new run of text is added to the rich text in line 42 using rt.AddRun(). The italic, size, text and font name are added to this new run.

Then the workbook is validated and saved to file in lines 51-55.

Run the code

Use the following command to run the code.

go run main.go

Sample Output

preview

Got any Questions?

We're here to help you.