Validate Workbook

This guide will demonstrate the process of validating workbooks 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/validation folder in the unioffice-examples directory.

cd unioffice-examples/spreadsheet/validation/

How it works

The import section in lines 10-14 imports the necessary libraries. Then the init function initializes the package by setting the metered license key.

The main function is defined in lines 21-60. In this function a new workbook with a sheet is created in lines 22-24. Lines 27-32 creates a dropdown validation list that references another sheet using the following snippet.

vsheet := ss.AddSheet()
vsheet.SetName("Validation Data")
vsheet.Cell("A1").SetString("A")
vsheet.Cell("A2").SetString("B")
vsheet.Cell("A3").SetString("C")
vsheet.Cell("A4").SetString("D")

sheet.Cell("B1").SetString("references sheet")
dvCombo := sheet.AddDataValidation()
dvCombo.SetRange("B2")
dvList := dvCombo.SetList()
dvList.SetRange(vsheet.RangeReference("A1:A4"))

The dropdown list of valid results are can be seen in the following sample result.

Alt text

Lines 42-53 create validation options which are directly specified in the code using:

sheet.Cell("C1").SetString("value list")
dvComboDirect := sheet.AddDataValidation()
dvComboDirect.SetRange("C2")
dvListDirect := dvComboDirect.SetList()
dvListDirect.SetValues([]string{"foo", "bar", "baz"})
sheet.Cell("C1").SetString("positive whole numbers")
dvWhole := sheet.AddDataValidation()
dvWhole.SetRange("D2")
dvWholeCmp := dvWhole.SetComparison(spreadsheet.DVCompareTypeWholeNumber, spreadsheet.DVCompareOpGreaterEqual)
dvWholeCmp.SetValue("0")

The list of valid lists which are foo, bar and baz are shown in the spreadsheet when trying to input value in the respective column as follows.

Alt text

Finally, in lines 55-59, the workbook is validated and save to file.

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.