Shared Formula

This guide demonstrates the process of applying a shared formula in spreadsheets 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/shared-formula folder in the unioffice-examples directory.

cd unioffice-examples/spreadsheet/shared-formula/

How it works

The example code starts by importing the necessary packages from UniOffice and the Go standard library. Then it defines the init function in lines 12-19 to set the metered license key to authenticate the library request for UniOffice.

The main function is defined in lines 21-48. Here, a new workbook with empty sheet is created in lines 22-24. In lines 26-37, a table with four rows and four columns is created and populated with data. Then shared formulas are applied to A5, A9 and A13 in lines 39-41. The formula string to be applied and the number of rows and columns it should be applied to are set in the argument of the method calls. For example when applying a shared formula for A5, the formula string is set to be "A1+1", the rows this formula should be applied to is set to be 2 (row 6 and 7) and the columns are set to be 3 (columns B, C and D). Finally, the workbook is validated and saved to file using:

ss.RecalculateFormulas()
if err := ss.Validate(); err != nil {
  log.Fatalf("error validating: %s", err)
}
ss.SaveToFile("shared-formula.xlsx")

Run the code

Run the code using the following command.

go run main.go

Sample Output

preview

Got any Questions?

We're here to help you.