forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spreadsheet: add support for merged cells
- Loading branch information
Showing
7 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"baliance.com/gooxml/spreadsheet" | ||
|
||
sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" | ||
) | ||
|
||
func main() { | ||
ss := spreadsheet.New() | ||
sheet := ss.AddSheet() | ||
|
||
sheet.Cell("A1").SetString("Hello World!") | ||
sheet.Cell("B1").SetString("will not be visible") // as it's not the first cell within a merged range Excel warns you when you do this through the UI | ||
sheet.AddMergedCells("A1", "C2") | ||
|
||
centered := ss.StyleSheet.AddCellStyle() | ||
centered.SetHorizontalAlignment(sml.ST_HorizontalAlignmentCenter) | ||
centered.SetVerticalAlignment(sml.ST_VerticalAlignmentCenter) | ||
sheet.Cell("A1").SetStyle(centered) | ||
|
||
for _, m := range sheet.MergedCells() { | ||
fmt.Println("merged region", m.Reference(), "has contents", m.Cell().GetString()) | ||
} | ||
|
||
if err := ss.Validate(); err != nil { | ||
log.Fatalf("error validating sheet: %s", err) | ||
} | ||
|
||
ss.SaveToFile("merged.xlsx") | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2017 Baliance. All rights reserved. | ||
// | ||
// Use of this source code is governed by the terms of the Affero GNU General | ||
// Public License version 3.0 as published by the Free Software Foundation and | ||
// appearing in the file LICENSE included in the packaging of this file. A | ||
// commercial license can be purchased by contacting [email protected]. | ||
|
||
package spreadsheet | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" | ||
) | ||
|
||
type MergedCell struct { | ||
wb *Workbook | ||
ws *sml.Worksheet | ||
x *sml.CT_MergeCell | ||
} | ||
|
||
// X returns the inner wrapped XML type. | ||
func (s MergedCell) X() *sml.CT_MergeCell { | ||
return s.x | ||
} | ||
|
||
// SetReference sets the regin of cells that the merged cell applies to. | ||
func (s MergedCell) SetReference(ref string) { | ||
s.x.RefAttr = ref | ||
} | ||
|
||
// Reference returns the region of cells that are merged. | ||
func (s MergedCell) Reference() string { | ||
return s.x.RefAttr | ||
} | ||
|
||
// Cell returns the actual cell behind the merged region | ||
func (s MergedCell) Cell() Cell { | ||
ref := s.Reference() | ||
if idx := strings.Index(s.Reference(), ":"); idx != -1 { | ||
ref = ref[0:idx] | ||
return Sheet{w: s.wb, x: s.ws}.Cell(ref) | ||
} | ||
fmt.Println("DIE") | ||
// couldn't find it, log an error? | ||
return Cell{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters