Working with Spreadsheet Management

Working with Spreadsheet Management

Working with Spreadsheet Management

This guide walks through the day-to-day operations of building and modifying spreadsheets: creating workbooks, writing cell values, adding formulas, building tables, and exporting to CSV.


Create a Workbook and Write Cells

NewWorkbook() returns a workbook with one worksheet already present. Write values with Cells().Set(ref, value):

wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]

ws.Cells().Set("A1", "Item")
ws.Cells().Set("B1", "Category")
ws.Cells().Set("C1", "Price")
ws.Cells().Set("A2", "Widget A")
ws.Cells().Set("B2", "Gadgets")
ws.Cells().Set("C2", 12.99)

wb.Save("outputfiles/inventory.xlsx")

Read Cells Back

Cells().Get(ref) returns the *Cell for a reference along with an error if the reference is invalid:

wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("C2", 12.99)

cell, err := ws.Cells().Get("C2")
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}
fmt.Printf("C2 = %v\n", cell.Value)

Use Cells().All() to enumerate every populated cell in the collection when you need to iterate over the whole sheet.

Add a Formula

Cell.SetFormula stores a formula string on a cell. Create the cell first with Set, then attach the formula — the formula is evaluated by Excel or LibreOffice on open, not by the library:

wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]

ws.Cells().Set("D2", nil)
cell, _ := ws.Cells().Get("D2")
cell.SetFormula("C2*2")

Build a Table Over the Data

Once your data range is populated, Worksheet.AddTable turns it into a structured, named table:

wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("A1", "Item")
ws.Cells().Set("A2", "Widget A")

tbl := ws.AddTable("A1:C2")
tbl.HasHeaderRow = true
tbl.StyleName = "TableStyleMedium6"
fmt.Printf("Created table %q covering %s\n", tbl.Name, tbl.Range)

Export to CSV

Workbook.ExportToCSV writes a given worksheet index to a CSV file with the delimiter of your choice:

wb := cells_foss.NewWorkbook()

if err := wb.ExportToCSV(0, "outputfiles/inventory.csv", ','); err != nil {
	fmt.Fprintf(os.Stderr, "Error exporting: %v\n", err)
	os.Exit(1)
}

The reverse direction, Workbook.ImportFromCSV(path, sheetName, delimiter), reads a CSV file into a named worksheet.

Remove a Cell

Cells().Remove(ref) clears a cell’s value:

wb := cells_foss.NewWorkbook()
ws := wb.Worksheets[0]
ws.Cells().Set("A2", "Widget A")

if err := ws.Cells().Remove("A2"); err != nil {
	fmt.Printf("Error removing cell: %v\n", err)
}

Tips and Best Practices

  • Create a cell with Set before calling SetFormula on it — SetFormula operates on an existing *Cell, not a bare reference string.
  • When exporting a workbook that has multiple worksheets, ExportToCSV’s sheet index parameter determines which worksheet is exported — index 0 is the first worksheet.
  • Use Cells().All() for read-heavy iteration over a whole sheet rather than calling Get in a loop over every possible reference.

Common Issues

IssueCauseFix
SetFormula has no effectCalled on a cell reference that was never created with Set firstCall ws.Cells().Set(ref, nil) to create the cell, then Get it and call SetFormula
CSV export is empty or wrong sheetWrong worksheet index passed to ExportToCSVConfirm the index matches the intended worksheet’s position in Workbook.Worksheets
Get returns an errorThe cell reference does not exist in the collectionConfirm the reference was previously set with Set, or handle the “not found” case explicitly

FAQ

Does Cells().Remove delete the whole row or column?

No — it clears a single cell’s value at the given reference.

Can I build a table over a range that already has a formula column?

Yes — AddTable operates on the cell range as written; formulas in the range are preserved.


API Reference Summary

Class / MethodDescription
Workbook.NewWorkbookCreate a new workbook with one worksheet
Cells.Set / Get / Remove / AllWrite, read, remove, and enumerate cells
Cell.SetFormula / GetFormulaAttach and read a formula string
Worksheet.AddTableBuild a structured table over a cell range
Workbook.ExportToCSV / ImportFromCSVConvert between XLSX and CSV

See Also