Skip to content

Commit

Permalink
Merge tag 'v1.0.3' into develop
Browse files Browse the repository at this point in the history
Tagging version v1.0.3 v1.0.3
  • Loading branch information
plandem committed Jul 31, 2019
2 parents c412649 + af8fa2e commit f513d01
Show file tree
Hide file tree
Showing 25 changed files with 337 additions and 152 deletions.
37 changes: 5 additions & 32 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (c *Cell) Bool() (bool, error) {
return false, errTypeMismatch
}

//SetGeneral sets the value as general type
func (c *Cell) SetGeneral(value string) {
//setGeneral sets the value as general type
func (c *Cell) setGeneral(value string) {
c.ml.Type = types.CellTypeGeneral
c.ml.Value = value
c.ml.Formula = nil
Expand Down Expand Up @@ -290,7 +290,7 @@ func (c *Cell) SetValue(value interface{}) {
case nil:
c.Reset()
default:
c.SetGeneral(fmt.Sprintf("%v", value))
c.setGeneral(fmt.Sprintf("%v", value))
}
}

Expand All @@ -316,40 +316,13 @@ func (c *Cell) Styles() styles.DirectStyleID {

//SetStyles sets style format to requested DirectStyleID or styles.Info
func (c *Cell) SetStyles(s interface{}) {
if styleID, ok := s.(styles.DirectStyleID); ok {
c.ml.Style = styleID
return
}

//we can update styleSheet only when sheet is in write mode, to prevent pollution of styleSheet with fake values
if (c.sheet.mode() & sheetModeWrite) == 0 {
panic(errorNotSupportedWrite)
}

var format *styles.Info
if f, ok := s.(styles.Info); ok {
format = &f
} else if f, ok := s.(*styles.Info); ok {
format = f
} else {
panic("only DirectStyleID or styles.Info supported as styles for cell")
}

styleID := c.sheet.workbook.doc.styleSheet.addStyle(format)
c.ml.Style = styleID
c.ml.Style = c.sheet.resolveStyleID(s)
}

//SetValueWithFormat is helper function that internally works as SetValue and SetStyles with NumberFormat
func (c *Cell) SetValueWithFormat(value interface{}, formatCode string) {
//we can update styleSheet only when sheet is in write mode, to prevent pollution of styleSheet with fake values
if (c.sheet.mode() & sheetModeWrite) == 0 {
panic(errorNotSupportedWrite)
}

styleID := c.sheet.workbook.doc.styleSheet.addStyle(styles.New(styles.NumberFormat(formatCode)))

c.ml.Style = c.sheet.resolveStyleID(styles.New(styles.NumberFormat(formatCode)))
c.SetValue(value)
c.ml.Style = styleID
}

//Hyperlink returns resolved hyperlink.Info if there is any hyperlink or nil otherwise
Expand Down
8 changes: 6 additions & 2 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (c *Col) SetOptions(o *options.Info) {
c.ml.CustomWidth = true
}

if o.Format != nil {
c.SetStyles(o.Format)
}

c.ml.OutlineLevel = o.OutlineLevel
c.ml.Hidden = o.Hidden
c.ml.Collapsed = o.Collapsed
Expand All @@ -40,8 +44,8 @@ func (c *Col) Styles() styles.DirectStyleID {
}

//SetStyles sets default style for the column. Affects cells not yet allocated in the column. In other words, this style applies to new cells.
func (c *Col) SetStyles(styleID styles.DirectStyleID) {
c.ml.Style = styleID
func (c *Col) SetStyles(s interface{}) {
c.ml.Style = c.sheet.info().resolveStyleID(s)
}

//CopyTo copies col cells into another col with cIdx index.
Expand Down
10 changes: 5 additions & 5 deletions docs/src/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ module.exports = {
// editLinkText: 'Help to improve this page!',
lastUpdated: 'Last Updated',
displayAllHeaders: true,
// algolia: {
// apiKey: '<API_KEY>',
// indexName: '<INDEX_NAME>'
// },
algolia: {
apiKey: '3e97b85acaa0479ed415ab8ecdaf55d6',
indexName: 'xlsx2go'
},
nav: [
{text: 'Home', link: '/'},
{text: 'Guide', link: '/guide/'},
Expand Down Expand Up @@ -62,6 +62,6 @@ module.exports = {
'@vuepress/nprogress',
['container', {type: 'right', defaultTitle: ''}],
['container', {type: 'note', defaultTitle: ''}],
// ['@vuepress/google-analytics', {'ga': 'UA-00000000-0'}],
['@vuepress/google-analytics', {'ga': 'UA-122513-16'}],
],
};
2 changes: 1 addition & 1 deletion docs/src/code/rich_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Example_richText() {
sheet.CellByRef("B8").SetText(
"E=mc",
styles.New(
styles.Font.Effect(styles.FontEffectSuperscript),
styles.Font.Superscript,
),
"2",
styles.New(
Expand Down
26 changes: 26 additions & 0 deletions docs/src/code/styles_font_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package code

import (
"github.com/plandem/xlsx/format/styles"
)

func Example_stylesFont() {
//all possible settings for font
styles.New(
styles.Font.Name("Courier New"),
styles.Font.Bold,
styles.Font.Italic,
styles.Font.Strikeout,
styles.Font.Superscript,
styles.Font.Subscript,
styles.Font.Shadow,
styles.Font.Condense,
styles.Font.Extend,
styles.Font.Family(styles.FontFamilyRoman),
styles.Font.Color("#FF0000"),
styles.Font.Size(16),
styles.Font.Underline(styles.UnderlineTypeSingle),
styles.Font.Scheme(styles.FontSchemeMinor),
styles.Font.Charset(styles.FontCharsetMAC),
)
}
142 changes: 142 additions & 0 deletions docs/src/guide/number_format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Number Format Codes
[[toc]]
Number format code is way to show numeric value. It controls whether a number is displayed as an integer, a floating number, a date, a currency value or some other user defined format.

::: tip
Xlsx2Go supports built-in Excel codes and will try to detect/convert to proper internal ID, as well as custom codes - use it as is without worrying.
:::

### Built-in Codes
::: note General type
* `@`
* `General`
:::

::: note Integer number
* `0`
* `0%`
* `(#,##0_);(#,##0)`
* `(#,##0_);[RED](#,##0)`
:::

::: note Float number
* `0.00`
* `#,##0`
* `#,##0.00`
* `($#,##0_);($#,##0)`
* `($#,##0_);[RED]($#,##0)`
* `($#,##0.00_);($#,##0.00_)`
* `($#,##0.00_);[RED]($#,##0.00_)`
* `0.00%`
* `0.00E+00`
* `# ?/?`
* `# ??/??`
* `(#,##0.00);(#,##0.00)`
* `(#,##0.00);[RED](#,##0.00)`
* `_(*#,##0_);_(*(#,##0);_(*"-"_);_(@_)`
* `_($*#,##0_);_($*(#,##0);_(*"-"_);_(@_)`
* `_(*#,##0.00_);_(*(#,##0.00);_(*"-"??_);_(@_)`
* `_($*#,##0.00_);_($*(#,##0.00);_(*"-"??_);_(@_)`
* `##0.0E+0`
:::

::: note Date
* `m-d-yy`
* `d-mmm-yy`
* `d-mmm`
* `mmm-yy`
:::

::: note Time
* `h:mm AM/PM`
* `h:mm:ss AM/PM`
* `h:mm`
* `h:mm:ss`
:::

::: note Date+Time
* `m-d-yy h:mm`
:::

::: note DeltaTime
* `mm:ss`
* `[h]:mm:ss`
* `mm:ss.0`
:::

### Custom Codes
Format code can control any aspect of number formatting allowed by Excel:

::: tip Currency
The `$` in format appears as the local currency symbol.
:::

::: tip Colors
The color format should have one of the following values:

`[Black]` `[Blue]` `[Cyan]` `[Green]` `[Magenta]` `[Red]` `[White]` `[Yellow]`
:::

#### Examples
<table>
<tr>
<th style="text-align: right;">Number code</th>
<th style="text-align: right;">Go Value</th>
<th style="text-align: right;">Excel Output</th>
</th>
<tr>
<td style="text-align: right;"><code>dd/mm/yyyy hh:mm AM/PM</code></td>
<td style="text-align: right;">time.Now()</td>
<td style="text-align: right;">18/07/2019 12:30 AM</td>
</tr>
<tr>
<td style="text-align: right;"><code>mm/dd/yy</code></td>
<td style="text-align: right;">time.Now()</td>
<td style="text-align: right;">07/18/19</td>
</tr>
<tr>
<td style="text-align: right;"><code>mmm d yyyy</code></td>
<td style="text-align: right;">time.Now()</td>
<td style="text-align: right;">Jul 18 2019</td>
</tr>
<tr>
<td style="text-align: right;"><code>d mmmm yyyy</code></td>
<td style="text-align: right;">time.Now()</td>
<td style="text-align: right;">18 July 2019</td>
</tr>
<tr>
<td style="text-align: right;"><code>0.000</code></td>
<td style="text-align: right;">1.2345678</td>
<td style="text-align: right;">1.235</td>
</tr>
<tr>
<td style="text-align: right;"><code>#,##0</code></td>
<td style="text-align: right;">1234.567</td>
<td style="text-align: right;">1,235</td>
</tr>
<tr>
<td style="text-align: right;"><code>0 "dollar and" .00 "cents"</code></td>
<td style="text-align: right;">1.87</td>
<td style="text-align: right;">1 dollar and .87 cents</td>
</tr>
<tr>
<td style="text-align: right;" rowspan="3"><code>[Green]General;[Red]-General;General</code></td>
<td style="text-align: right;">12345</td>
<td style="text-align: right;"><span style="color:green">1235</span></td>
</tr>
<tr>
<td style="text-align: right;">-12345</td>
<td style="text-align: right;"><span style="color:red">-12345</span></td>
</tr>
<tr>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
</tr>
</table>

::: note

::: right
For more information about custom formats, check [Microsoft Documentation](https://support.office.com/en-us/article/create-a-custom-number-format-78f2a361-936b-4c03-8772-09fab54be7f4?ui=en-US&rs=en-US&ad=US)
:::

51 changes: 50 additions & 1 deletion docs/src/guide/styles-formatting.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,63 @@
# Styles Formatting
[[toc]]

Styles can be defined through special type - general type for all available styles. But information that will be using to style object, depends on usage, e.g. to style cells everything will be use, to style rich texts - only font information.

```go
// create a new styles
ss := styles.New(
styles.Font.Bold,
styles.Font.Color("#ff0000"),
)

//update styles
ss.Set(
styles.Border.Color("#009000"),
styles.Border.Type(styles.BorderStyleMedium),
)
```
::: warning Modify Styles
While you can modify created styles, you should keep in mind, that modifying will work only before applying styles to cell and any modifications after applying, will create new styles.
:::

```go
ss := styles.New(
styles.Font.Bold,
))

//font will be `bold`
sheet.CellByRef("A1").SetStyles(ss)

//modify styles
ss.Set(
styles.Font.Color("#ff0000"),
)

//`A2` - will be `bold and red`
//`A1` - will be only `bold` and without color
sheet.CellByRef("A2").SetStyles(ss)
```

### Font
::: warning
Excel can only display installed fonts, that's why using standard fonts(e.g.: `Calibri`, `Times New Roman` or `Courier New`) is highly recommended.
:::

::: note
The default font for cell is `Calibri` (Excel 2007+)
:::
<<< @/src/code/styles_font_test.go

::: note Predefined values
Xlsx2Go defined all built-in values to use for styling. For more information, check [API documentation](https://godoc.org/github.com/plandem/xlsx/format/styles#pkg-constants)
:::

### Fill

### Border

### Alignment

### Numbers
### Number Format

### Protection
Loading

0 comments on commit f513d01

Please sign in to comment.