Skip to content

Commit

Permalink
Merge pull request #1 from tealeg/master
Browse files Browse the repository at this point in the history
Sync from original
  • Loading branch information
derlinkshaender committed Jul 11, 2020
2 parents a8fe119 + d2955ab commit 4aef9a5
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 16 deletions.
4 changes: 4 additions & 0 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,14 @@ func (c *Cell) key() string {

}

// Hyperlink is a structure to store link information
// in-workbook links to cells or defined names are stored in Location
// external links are stores in Link
type Hyperlink struct {
DisplayString string
Link string
Tooltip string
Location string
}

// CellInterface defines the public API of the Cell.
Expand Down
29 changes: 29 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,35 @@ func TestFile(t *testing.T) {

}

// we can unmarshal hyperlinks with in-workbook links
func TestHyperlinks(t *testing.T) {
var xlsxFile *File
var err error
c := qt.New(t)

xlsxFile, err = OpenFile("./testdocs/testhyperlinks.xlsx")
c.Assert(err, qt.IsNil)
c.Assert(xlsxFile, qt.Not(qt.IsNil))

sheet, ok := xlsxFile.Sheet["Sample"]
c.Assert(ok, qt.Equals, true)
row, err := sheet.Row(1)
c.Assert(err, qt.IsNil)
cell := row.GetCell(0)
c.Assert(cell.Hyperlink.Location, qt.Equals, "cities")
c.Assert(cell.Hyperlink.Link, qt.Equals, "")

row, err = sheet.Row(2)
c.Assert(err, qt.IsNil)
c.Assert(row.GetCell(0).Hyperlink.Location, qt.Equals, "")
c.Assert(row.GetCell(0).Hyperlink.Link, qt.Equals, "https://www.bobross.com/")

row, err = sheet.Row(3)
c.Assert(err, qt.IsNil)
c.Assert(row.GetCell(0).Hyperlink.Location, qt.Equals, "Styles!C14")
c.Assert(row.GetCell(0).Hyperlink.Link, qt.Equals, "")
}

// Helper function used to test contents of a given xlsxXf against
// expectations.
func testXf(c *qt.C, result, expected *xlsxXf) {
Expand Down
3 changes: 3 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,9 @@ func makeHyperlinkTable(worksheet *xlsxWorksheet, fi *File, rsheet *xlsxSheet) (
if xlsxLink.DisplayString != "" {
newHyperLink.DisplayString = xlsxLink.DisplayString
}
if xlsxLink.Location != "" {
newHyperLink.Location = xlsxLink.Location
}
cellRef := xlsxLink.Reference
x, y, err := GetCoordsFromCellIDString(cellRef)
if err != nil {
Expand Down
Binary file added testdocs/testhyperlinks.xlsx
Binary file not shown.
Binary file modified tutorial/samplefile.xlsx
100755 → 100644
Binary file not shown.
88 changes: 72 additions & 16 deletions tutorial/tutorial.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
= The `tealeg/xlsx` Tutorial: Reading and writing `xlsx` files with Go
:author: Armin Hanisch
:email: mail@arminhanisch.de
:revnumber: v0.1
:revdate: 07.07.2020
:revremark: Initial version
:revnumber: v0.2
:revdate: 2020-07-09
:revremark: added "What's in a cell", corrections

This work is licensed under the BSD-3-Clause license. See the `LICENSE` file in the repository.

Expand Down Expand Up @@ -93,7 +93,7 @@ To create a new, empty xlsx-File, use the `NewFile()` function.

wb := xlsx.NewFile()

This function returns a new `xlsx.File` struct.
This function returns a new `xlsx.File` struct.

== Working with sheets

Expand Down Expand Up @@ -210,14 +210,14 @@ fmt.Println(row)

The row struct exports only two fields, `Hidden` (a boolean that shows
if the row is hidden or not) and `Sheet` (a pointer back to the sheet
that contains the row). So how to you access anything in the row? We’ll
see in the chapter <<cells>>; but let’s see how to add
and remove rows first.
that contains the row).
So how do you access anything in the row?
We’ll see in the chapter about Cells, but let’s see how to add and remove rows first.

==== Where does my data end?

Very good question. Our sample file consists of only four rows in the
``Sample'' sheet.
`Sample` sheet.

[cols="<,<,>,>",options="header",]
|===
Expand Down Expand Up @@ -328,8 +328,8 @@ This will return a pointer to a new `Cell`

=== Cells

[quote, me, in a requirements workshop]
If all you know is Excel, every problem looks like rows and columns.
> If all you know is Excel, every problem looks like rows and columns. +
> -- _me in a requirements workshop_

Cells are the core of any spreadsheet. The `+xlsx+` package provides
ways to access, create and change cells that will be discussed in this
Expand Down Expand Up @@ -441,8 +441,7 @@ func main() {

If you didn’t change the sample file, the output should look like this:

[source,shell]
....
----
== xlsx package tutorial ==
Max row is 4
Cell value: Name
Expand All @@ -461,8 +460,7 @@ Cell value: Charlie
Cell value: 02-08-20
Cell value: 32145
Cell value: 1607.25 €
....
----

NOTE: If you work with a version *before* `v3.2.0`, there is no way to
know *which* cell (in terms of column and row number) you are receiving at the moment
Expand Down Expand Up @@ -504,15 +502,73 @@ the code for this:
// into a number and then formatted using the formatting or not.
....

==== Getting cell values
=== Getting cell values

You can retrieve the contents of a cell using these functions

* `Value()` – returns a string
* `FormattedValue()` – return a value with the cell’s format applied
and an error code
* `String()` – returns the cell’s value as a string
* `Formula()` – return a string containing the cell’s formula
* `Formula()` – return a string containing the cell’s formula (or an empty string, if no formula)
* `Int()` - return the cell's content as integer and an error code
* `Float()` - return the cell's content as a float64 and an error code
* `Bool()` - return `true` or `false`
** if cell has `CellTypeBool` and the value equals `1`, return `true`
** if cell has `CellTypeNumeric` and the value is a non-zero, return `true`
** otherwise return `true`, if the result of `Value()` is a non-empty string

=== What's in a cell?

Often you need to find out about the content of a cell because the cell type alone does not suffice.
Why not? Let's have a look.
The sample file contains a sheet "Sample" with content shown below.

[cols="^1,<5,<5,>5,>5",options="header",]
|===
| |A |B |C |D
|**1**|Name |Date |Number |Formula
|**2**|Alice |3/20/2020 |24315 |1215.75 €
|**3**|Bob |4/17/2020 |21345 |1067.25 €
|**4**|Charlie |2/8/2020 |32145 |1607.25 €
|===

We will have a look at cell `D2` (which is row 1, col 3). The sample code below reads the cell and outputs cell content retrieved using the functions from the last chapter.

[source,go]
----
// let sh be a reference to a xslx.Sheet
// get the Cell in D1, which is row 0, col 3
theCell, err := sh.Cell(0, 3)
if err != nil {
panic(err)
}
// we got a cell, but what's in it?
fv, err := theCell.FormattedValue()
if err != nil {
panic(err)
}
fmt.Println("Numeric cell?:", theCell.Type() == xlsx.CellTypeNumeric)
fmt.Println("String:", theCell.String())
fmt.Println("Formatted:", fv)
fmt.Println("Formula:", theCell.Formula())
----

You should get an output that looks like this:

----
Numeric cell?: true
String: 1215.75 €
Formatted: 1215.75 €
Formula: C2*0.05
----

As you see, calling `Type()` for the cell returns "_I'm numeric_".
Which is nice, but not the whole truth, because the cell actually contains a formula.
The formula is shown in the last line of the output.
If you have a "_real_" numeric cell that contains just a number, the result of calling `Formula()` is the empty string.
So if you want to distinguish between these, check if the formula of a cell is empty. Then a numeric cell is really a numeric cell.

=== Setting cell values

Expand Down
1 change: 1 addition & 0 deletions xmlWorksheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ type xlsxHyperlink struct {
Reference string `xml:"ref,attr"`
DisplayString string `xml:"display,attr,omitempty"`
Tooltip string `xml:"tooltip,attr,omitempty"`
Location string `xml:"location,attr,omitempty"`
}

// Return the cartesian extent of a merged cell range from its origin
Expand Down

0 comments on commit 4aef9a5

Please sign in to comment.