excel
Reading and writing Excel spreadsheets (xlsx and xls), OpenDocument (ods) and CSV.
import excel
A workbook holds several sheets, and each sheet holds cells by row and column, from zero. Turn Excel references such as C5 into a row and column with CellRef and ParseCellRef.
Open and Save work the format out from the file extension. Formulas are stored and read with their last calculated result, but Salam does not evaluate a formula itself.
Types
Style
struct Style| Name | Type | Description |
|---|---|---|
bold | bool = false | Bold. |
italic | bool = false | Italic. |
underline | bool = false | Underlined. |
font_name | str = "Calibri" | The font name. |
font_size | int = 11 | The font size. |
font_color | str = "" | The text colour as hex, such as FF0000. |
bg_color | str = "" | The background colour. |
align_h | int = 0 | The horizontal alignment. |
align_v | int = 0 | The vertical alignment. |
num_format | str = "" | The number display format, such as 0.00. |
import excel
func main:
println excel.ColumnLabel(0), excel.ColumnLabel(25), excel.ColumnLabel(26), excel.ColumnLabel(703)
println excel.ColumnIndex("A"), excel.ColumnIndex("AA"), excel.CellRef(4, 2)
mut row := 0
mut col := 0
println excel.ParseCellRef("C5", row, col), row, col, excel.ParseCellRef("5C", row, col)
serial := excel.EpochToSerial(0)
println serial, excel.SerialToEpoch(45371.0)
text := excel.NewStringCell(0, 0, "some text")
number := excel.NewNumberCell(0, 1, 3.5)
flag := excel.NewBoolCell(0, 2, false)
date := excel.NewDateCell(0, 3, 0)
formula := excel.NewFormulaCell(0, 4, "A1*2", 7.0)
println text.kind == excel.CellString, number.num, flag.flag, date.kind == excel.CellDate, formula.formula, formula.num
println excel.CellEmpty, excel.CellNumber, excel.CellBool, excel.CellFormula
mut styles := excel.NewStyleSheet()
bold := excel.Style { bold = true, bg_color = "FFFF00", align_h = excel.AlignCenter }
id := styles.get_or_add(bold)
println id, styles.get_or_add(bold) == id, styles.count(), styles.get(id).bold
println bold.equals(styles.get(id)), bold.font_name, bold.font_size
println excel.AlignDefault, excel.AlignLeft, excel.AlignRight, excel.AlignTop, excel.AlignMiddle, excel.AlignBottom
styles.free()
endequals
func equals(other: Style): boolThe example for this item is shown with the ones above: excel-Cell.salam
StyleSheet
struct StyleSheet| Name | Type | Description |
|---|---|---|
styles | Vector | The styles. |
The example for this item is shown with the ones above: excel-Cell.salam
get_or_add
func get_or_add(s: Style): intThe example for this item is shown with the ones above: excel-Cell.salam
get
func get(id: int): StyleThe example for this item is shown with the ones above: excel-Cell.salam
count
func count(): intThe example for this item is shown with the ones above: excel-Cell.salam
free
func free()The example for this item is shown with the ones above: excel-Cell.salam
Cell
struct Cell| Name | Type | Description |
|---|---|---|
row | int = 0 | The row, from zero. |
col | int = 0 | The column, from zero. |
kind | int = 0 | The kind; one of the Cell constants. |
text | str = "" | The text of a string cell, or the error code. |
num | f64 = 0 | The number; for a date, the Excel serial number. |
flag | bool = false | The boolean value. |
formula | str = "" | The formula text. |
style_id | int = -1 | The style index; -1 means the default. |
The example for this item is shown with the ones above: excel-Cell.salam
CellPos
struct CellPos| Name | Type | Description |
|---|---|---|
row | int = 0 | The row. |
col | int = 0 | The column. |
import excel
func main:
mut sheet := excel.NewSheet("sales")
sheet.set_string(0, 0, "item")
sheet.set_string(0, 1, "count")
sheet.set_cell(1, 0, "tea")
sheet.set_int(1, 1, 12)
sheet.set_cell(2, 0, "sugar")
sheet.set_number(2, 1, 4.5)
sheet.set_bool(3, 0, true)
sheet.set_date(3, 1, 1710943509)
sheet.set_formula(4, 1, "SUM(B2:B3)", 16.5)
sheet.set_error(5, 0, "#DIV/0!")
println sheet.name, sheet.row_count(), sheet.col_count()
println sheet.get_string(1, 0), sheet.get_number(1, 1), sheet.get_number(2, 1), sheet.get_bool(3, 0), sheet.get_formula(4, 1)
cell := sheet.get_cell(5, 0)
println cell.kind == excel.CellError, cell.text, sheet.get_cell(3, 1).kind == excel.CellDate
println sheet.has_cell(1, 1), sheet.has_cell(9, 9)
sheet.clear_cell(5, 0)
println sheet.has_cell(5, 0)
mut row := Vector {} as Vector<str>
row.push("bread")
row.push("7")
sheet.set_row(6, row)
sheet.set_column(3, row)
mut table := Vector {} as Vector<str>
each value in ["a", "b", "c", "d"]:
table.push(value)
end
sheet.set_table(8, 0, table, 2)
println sheet.get_row(6).len(), sheet.get_column(3).len(), sheet.get_all().len()
println sheet.get_string(9, 1)
found := sheet.find("bread")
println found.len(), found[0].row, found[0].col
println sheet.filter_rows_equals(0, "tea")
sheet.merge_cells(0, 0, 0, 1)
mut hit := false
range := sheet.merged_range(0, 1, hit)
println sheet.is_merged(0, 1), hit, range.row1, range.col2, sheet.merges.len()
sheet.set_style(1, 1, 2)
println sheet.get_cell(1, 1).style_id, sheet.max_row, sheet.max_col, sheet.cells.size() > 0
sheet.free()
endMergeRange
struct MergeRange| Name | Type | Description |
|---|---|---|
row1 | int = 0 | The starting row. |
col1 | int = 0 | The starting column. |
row2 | int = 0 | The ending row. |
col2 | int = 0 | The ending column. |
The example for this item is shown with the ones above: excel-Sheet.salam
Sheet
struct Sheet| Name | Type | Description |
|---|---|---|
name | str = "" | The sheet name. |
cells | HashMap | The cells by their reference. |
max_row | int = -1 | The greatest filled row number; -1 for an empty sheet. |
max_col | int = -1 | The greatest filled column number. |
merges | Vector | The merged ranges. |
The example for this item is shown with the ones above: excel-Sheet.salam
set_string
func set_string(row: int, col: int, value: str)The example for this item is shown with the ones above: excel-Sheet.salam
set_cell
func set_cell(row: int, col: int, value: str)set_string; the value is stored as a string.The example for this item is shown with the ones above: excel-Sheet.salam
set_number
func set_number(row: int, col: int, value: f64)The example for this item is shown with the ones above: excel-Sheet.salam
set_int
func set_int(row: int, col: int, value: i64)The example for this item is shown with the ones above: excel-Sheet.salam
set_bool
func set_bool(row: int, col: int, value: bool)The example for this item is shown with the ones above: excel-Sheet.salam
set_date
func set_date(row: int, col: int, epoch: i64)The example for this item is shown with the ones above: excel-Sheet.salam
set_formula
func set_formula(row: int, col: int, formula: str, cached: f64)The example for this item is shown with the ones above: excel-Sheet.salam
set_error
func set_error(row: int, col: int, code: str)#DIV/0!.The example for this item is shown with the ones above: excel-Sheet.salam
set_style
func set_style(row: int, col: int, style_id: int)The example for this item is shown with the ones above: excel-Sheet.salam
clear_cell
func clear_cell(row: int, col: int)The example for this item is shown with the ones above: excel-Sheet.salam
has_cell
func has_cell(row: int, col: int): boolThe example for this item is shown with the ones above: excel-Sheet.salam
get_cell
func get_cell(row: int, col: int): CellThe example for this item is shown with the ones above: excel-Sheet.salam
get_string
func get_string(row: int, col: int): strThe example for this item is shown with the ones above: excel-Sheet.salam
get_number
func get_number(row: int, col: int): f64The example for this item is shown with the ones above: excel-Sheet.salam
get_bool
func get_bool(row: int, col: int): boolThe example for this item is shown with the ones above: excel-Sheet.salam
get_formula
func get_formula(row: int, col: int): strThe example for this item is shown with the ones above: excel-Sheet.salam
row_count
func row_count(): intThe example for this item is shown with the ones above: excel-Sheet.salam
col_count
func col_count(): intThe example for this item is shown with the ones above: excel-Sheet.salam
set_row
func set_row(row: int, values: Vector<str>)The example for this item is shown with the ones above: excel-Sheet.salam
set_column
func set_column(col: int, values: Vector<str>)The example for this item is shown with the ones above: excel-Sheet.salam
set_table
func set_table(start_row: int, start_col: int, flat: Vector<str>, num_cols: int)num_cols of them per row.The example for this item is shown with the ones above: excel-Sheet.salam
get_row
func get_row(row: int): Vector<Cell>The example for this item is shown with the ones above: excel-Sheet.salam
get_column
func get_column(col: int): Vector<Cell>The example for this item is shown with the ones above: excel-Sheet.salam
get_all
func get_all(): Vector<Cell>The example for this item is shown with the ones above: excel-Sheet.salam
find
func find(value: str): Vector<CellPos>The example for this item is shown with the ones above: excel-Sheet.salam
filter_rows_equals
func filter_rows_equals(col: int, value: str): Vector<int>The example for this item is shown with the ones above: excel-Sheet.salam
merge_cells
func merge_cells(row1: int, col1: int, row2: int, col2: int)The example for this item is shown with the ones above: excel-Sheet.salam
is_merged
func is_merged(row: int, col: int): boolThe example for this item is shown with the ones above: excel-Sheet.salam
merged_range
func merged_range(row: int, col: int, found &: bool): MergeRangeThe example for this item is shown with the ones above: excel-Sheet.salam
free
func free()The example for this item is shown with the ones above: excel-Sheet.salam
Workbook
struct Workbook| Name | Type | Description |
|---|---|---|
sheets | Vector | The sheets in order. |
styles | StyleSheet | The styles shared by the cells. |
active_sheet | int = 0 | The index of the active sheet. |
source_path | str = "" | The path of the file the workbook was read from. |
format | int = 0 | The file format; one of the Format constants. |
last_error | str = "" | The last error message. |
import excel
func main:
mut book := excel.NewWorkbook()
mut a := book.add_sheet("A")
a.set_string(0, 0, "hello")
a.set_number(0, 1, 2.5)
mut b := book.add_sheet("B")
b.set_int(0, 0, 42)
println book.sheet_count(), book.sheet_names(), book.sheet_index("B"), book.sheet_at(0).name
println book.rename_sheet(1, "second"), book.sheet("second").get_number(0, 0)
println book.set_active(1), book.active().name, book.active_sheet
println excel.WriteXLSX(book, "book.xlsx"), excel.WriteODS(book, "book.ods"), book.save("copy.xlsx"), excel.Save(book, "book.csv")
println excel.DetectFormat("book.xlsx") == excel.FormatXLSX, excel.DetectFormat("book.ods") == excel.FormatODS, excel.DetectFormat("a.txt"), excel.FormatXLS, excel.FormatCSV, excel.FormatUnknown
mut ok := false
mut err := ""
mut read := excel.Open("book.xlsx", ok, err)
println ok, read.sheet_names(), read.sheet("A").get_string(0, 0), read.format == excel.FormatXLSX
mut copy := excel.ReadXLSX("copy.xlsx", ok, err)
mut ods := excel.ReadODS("book.ods", ok, err)
println ok, ods.sheet("second").get_number(0, 0), copy.sheet_count()
mut meta := excel.Inspect("book.xlsx", ok, err)
println ok, meta.exists, meta.size > 0, meta.sheet_names, meta.sheet_rows, meta.sheet_cols
meta.free()
mut csv := excel.ReadCSVWorkbook("book.csv", ok, err)
println ok, csv.sheet_at(0).get_string(0, 0), excel.WriteCSVWorkbook(csv, "again.csv")
_ := excel.Open("missing.xlsx", ok, err)
println ok, err != ""
_ := excel.ReadXLS("missing.xls", ok, err)
println ok, excel.IsXLS(null, 0)
println book.remove_sheet_named("A"), book.remove_sheet(5), book.remove_sheet(0), book.sheet_count()
mut fresh := excel.New()
println fresh.sheet_count()
fresh.free()
read.free()
copy.free()
ods.free()
csv.free()
book.free()
endadd_sheet
func add_sheet(name: str): Sheet*The example for this item is shown with the ones above: excel-Workbook.salam
remove_sheet
func remove_sheet(idx: int): boolidx; false when the index is wrong.The example for this item is shown with the ones above: excel-Workbook.salam
remove_sheet_named
func remove_sheet_named(name: str): boolThe example for this item is shown with the ones above: excel-Workbook.salam
rename_sheet
func rename_sheet(idx: int, new_name: str): boolThe example for this item is shown with the ones above: excel-Workbook.salam
sheet_index
func sheet_index(name: str): intThe example for this item is shown with the ones above: excel-Workbook.salam
sheet_count
func sheet_count(): intThe example for this item is shown with the ones above: excel-Workbook.salam
sheet_names
func sheet_names(): Vector<str>The example for this item is shown with the ones above: excel-Workbook.salam
sheet_at
func sheet_at(idx: int): Sheet*idx.The example for this item is shown with the ones above: excel-Workbook.salam
sheet
func sheet(name: str): Sheet*The example for this item is shown with the ones above: excel-Workbook.salam
active
func active(): Sheet*The example for this item is shown with the ones above: excel-Workbook.salam
set_active
func set_active(idx: int): boolThe example for this item is shown with the ones above: excel-Workbook.salam
save
func save(path: str): boolThe example for this item is shown with the ones above: excel-Workbook.salam
free
func free()The example for this item is shown with the ones above: excel-Workbook.salam
FileMeta
struct FileMeta| Name | Type | Description |
|---|---|---|
path | str = "" | The path. |
format | int = 0 | The format. |
exists | bool = false | The file is there. |
size | i64 = 0 | The size in bytes. |
sheet_names | Vector | The sheet names. |
sheet_rows | Vector | The number of rows of each sheet. |
sheet_cols | Vector | The number of columns of each sheet. |
The example for this item is shown with the ones above: excel-Workbook.salam
free
func free()The example for this item is shown with the ones above: excel-Workbook.salam
Constants and variables
Alignment Align…
align_h (left, centre, right) and align_v (top, middle, bottom). Zero means the default.| Name | Value |
|---|---|
AlignDefault | 0 |
AlignLeft | 1 |
AlignCenter | 2 |
AlignRight | 3 |
AlignTop | 1 |
AlignMiddle | 2 |
AlignBottom | 3 |
The example for this item is shown with the ones above: excel-Cell.salam
Cell kinds Cell…
kind of each cell.| Name | Value |
|---|---|
CellEmpty | 0 |
CellString | 1 |
CellNumber | 2 |
CellBool | 3 |
CellDate | 4 |
CellFormula | 5 |
CellError | 6 |
The example for this item is shown with the ones above: excel-Cell.salam
File formats Format…
| Name | Value |
|---|---|
FormatUnknown | 0 |
FormatXLSX | 1 |
FormatXLS | 2 |
FormatODS | 3 |
FormatCSV | 4 |
The example for this item is shown with the ones above: excel-Workbook.salam
Functions
WriteODS
func WriteODS(book &: Workbook, path: str): boolThe example for this item is shown with the ones above: excel-Workbook.salam
NewStyleSheet
func NewStyleSheet(): StyleSheetThe example for this item is shown with the ones above: excel-Cell.salam
ReadXLSX
func ReadXLSX(path: str, ok &: bool, err &: str): WorkbookThe example for this item is shown with the ones above: excel-Workbook.salam
NewStringCell
func NewStringCell(row: int, col: int, value: str): CellThe example for this item is shown with the ones above: excel-Cell.salam
NewNumberCell
func NewNumberCell(row: int, col: int, value: f64): CellThe example for this item is shown with the ones above: excel-Cell.salam
NewBoolCell
func NewBoolCell(row: int, col: int, value: bool): CellThe example for this item is shown with the ones above: excel-Cell.salam
NewDateCell
func NewDateCell(row: int, col: int, epoch: i64): CellThe example for this item is shown with the ones above: excel-Cell.salam
NewFormulaCell
func NewFormulaCell(row: int, col: int, formula: str, cached: f64): CellThe example for this item is shown with the ones above: excel-Cell.salam
ColumnLabel
func ColumnLabel(col: int): strThe example for this item is shown with the ones above: excel-Cell.salam
ColumnIndex
func ColumnIndex(label: str): intThe example for this item is shown with the ones above: excel-Cell.salam
CellRef
func CellRef(row: int, col: int): strC5, from a zero based row and column.The example for this item is shown with the ones above: excel-Cell.salam
ParseCellRef
func ParseCellRef(ref: str, row_out &: int, col_out &: int): boolC5 into a zero based row and column.The example for this item is shown with the ones above: excel-Cell.salam
EpochToSerial
func EpochToSerial(epoch: i64): f64The example for this item is shown with the ones above: excel-Cell.salam
SerialToEpoch
func SerialToEpoch(serial: f64): i64The example for this item is shown with the ones above: excel-Cell.salam
ReadODS
func ReadODS(path: str, ok &: bool, err &: str): WorkbookThe example for this item is shown with the ones above: excel-Workbook.salam
SheetToCSV
func SheetToCSV(sheet: Sheet): strimport excel
func main:
mut sheet := excel.CSVToSheet("name,score\nAli,17\n\"Sara, A\",19.5\n", "scores")
println sheet.name, sheet.row_count(), sheet.get_string(2, 0), sheet.get_number(2, 1)
println excel.SheetToCSV(sheet)
println excel.SheetToJSON(sheet)
mut from_json := excel.JSONToSheet(`[["a", "b"], ["1", "x"], ["2", "y"]]`, "data")
println from_json.row_count(), from_json.col_count(), from_json.get_string(0, 1), from_json.get_string(2, 0)
xml := "<row r=\"2\"><c r=\"A2\" t=\"s\"><v>0</v></c><br/></row>"
start := excel.XmlFind(xml, "c", 0)
println start, excel.XmlAttr(xml, start, "r"), excel.XmlTagEnd(xml, start)
println excel.XmlInner(xml, "v", 0), excel.XmlSelfClosing(xml, excel.XmlFind(xml, "br", 0))
println excel.XmlFindClose(xml, "row", 10)
println excel.XmlEscape("a<b&c"), excel.XmlUnescape("a<b&c")
sheet.free()
from_json.free()
endCSVToSheet
func CSVToSheet(text: str, sheet_name: str): SheetThe example for this item is shown with the ones above: excel-Convert.salam
ReadCSVWorkbook
func ReadCSVWorkbook(path: str, ok &: bool, err &: str): WorkbookThe example for this item is shown with the ones above: excel-Workbook.salam
WriteCSVWorkbook
func WriteCSVWorkbook(book &: Workbook, path: str): boolThe example for this item is shown with the ones above: excel-Workbook.salam
DetectFormat
func DetectFormat(path: str): intThe example for this item is shown with the ones above: excel-Workbook.salam
New
func New(): WorkbookNewWorkbook.The example for this item is shown with the ones above: excel-Workbook.salam
Open
func Open(path: str, ok &: bool, err &: str): Workbookok is false and err holds the message.The example for this item is shown with the ones above: excel-Workbook.salam
Save
func Save(book &: Workbook, path: str): boolThe example for this item is shown with the ones above: excel-Workbook.salam
IsXLS
func IsXLS(p: void*, n: int): boolThe example for this item is shown with the ones above: excel-Workbook.salam
ReadXLS
func ReadXLS(path: str, ok &: bool, err &: str): WorkbookThe example for this item is shown with the ones above: excel-Workbook.salam
WriteXLSX
func WriteXLSX(book &: Workbook, path: str): boolThe example for this item is shown with the ones above: excel-Workbook.salam
NewSheet
func NewSheet(name: str): SheetThe example for this item is shown with the ones above: excel-Sheet.salam
XmlEscape
func XmlEscape(s: str): strThe example for this item is shown with the ones above: excel-Convert.salam
XmlUnescape
func XmlUnescape(s: str): strThe example for this item is shown with the ones above: excel-Convert.salam
XmlFind
func XmlFind(xml: str, tag: str, from: int): intfrom onwards.The example for this item is shown with the ones above: excel-Convert.salam
XmlTagEnd
func XmlTagEnd(xml: str, start: int): int> of the tag that starts at start.The example for this item is shown with the ones above: excel-Convert.salam
XmlSelfClosing
func XmlSelfClosing(xml: str, start: int): boolThe example for this item is shown with the ones above: excel-Convert.salam
XmlAttr
func XmlAttr(xml: str, start: int, attr: str): strThe example for this item is shown with the ones above: excel-Convert.salam
XmlFindClose
func XmlFindClose(xml: str, tag: str, content_start: int): intThe example for this item is shown with the ones above: excel-Convert.salam
XmlInner
func XmlInner(xml: str, tag: str, from: int): strThe example for this item is shown with the ones above: excel-Convert.salam
SheetToJSON
func SheetToJSON(sheet: Sheet): strThe example for this item is shown with the ones above: excel-Convert.salam
JSONToSheet
func JSONToSheet(text: str, sheet_name: str): SheetThe example for this item is shown with the ones above: excel-Convert.salam
NewWorkbook
func NewWorkbook(): WorkbookThe example for this item is shown with the ones above: excel-Workbook.salam
Inspect
func Inspect(path: str, ok &: bool, err &: str): FileMetaThe example for this item is shown with the ones above: excel-Workbook.salam