- Unify the index row number index of functions `SetRowHeight()` and `GetRowHeight()` relate issue #68;

- Unify the return value data type of functions  `SetColWidth()` and `GetColWidth()`;
- go test updated
formula
Ri Xu 8 years ago
parent 14eca84073
commit 86466654e2
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7

@ -2,15 +2,14 @@ package excelize
import (
"math"
"strconv"
"strings"
)
// Define the default cell size and EMU unit of measurement.
const (
defaultColWidthPixels int = 64
defaultRowHeightPixels int = 20
EMU int = 9525
defaultColWidthPixels float64 = 64
defaultRowHeightPixels float64 = 20
EMU int = 9525
)
// GetColVisible provides a function to get visible of a single column by given
@ -157,26 +156,26 @@ func (f *File) positionObjectPixels(sheet string, colStart, rowStart, x1, y1, wi
// Calculate the absolute x offset of the top-left vertex.
for colID := 1; colID <= colStart; colID++ {
xAbs += f.GetColWidth(sheet, colID)
xAbs += f.getColWidth(sheet, colID)
}
xAbs += x1
// Calculate the absolute y offset of the top-left vertex.
// Store the column change to allow optimisations.
for rowID := 1; rowID <= rowStart; rowID++ {
yAbs += f.GetRowHeight(sheet, rowID)
yAbs += f.getRowHeight(sheet, rowID)
}
yAbs += y1
// Adjust start column for offsets that are greater than the col width.
for x1 >= f.GetColWidth(sheet, colStart) {
x1 -= f.GetColWidth(sheet, colStart)
for x1 >= f.getColWidth(sheet, colStart) {
x1 -= f.getColWidth(sheet, colStart)
colStart++
}
// Adjust start row for offsets that are greater than the row height.
for y1 >= f.GetRowHeight(sheet, rowStart) {
y1 -= f.GetRowHeight(sheet, rowStart)
for y1 >= f.getRowHeight(sheet, rowStart) {
y1 -= f.getRowHeight(sheet, rowStart)
rowStart++
}
@ -188,15 +187,15 @@ func (f *File) positionObjectPixels(sheet string, colStart, rowStart, x1, y1, wi
height += y1
// Subtract the underlying cell widths to find end cell of the object.
for width >= f.GetColWidth(sheet, colEnd) {
for width >= f.getColWidth(sheet, colEnd) {
colEnd++
width -= f.GetColWidth(sheet, colEnd)
width -= f.getColWidth(sheet, colEnd)
}
// Subtract the underlying cell heights to find end cell of the object.
for height >= f.GetRowHeight(sheet, rowEnd) {
for height >= f.getRowHeight(sheet, rowEnd) {
rowEnd++
height -= f.GetRowHeight(sheet, rowEnd)
height -= f.getRowHeight(sheet, rowEnd)
}
// The end vertices are whatever is left from the width and height.
@ -205,9 +204,9 @@ func (f *File) positionObjectPixels(sheet string, colStart, rowStart, x1, y1, wi
return colStart, rowStart, xAbs, yAbs, colEnd, rowEnd, x2, y2
}
// GetColWidth provides function to get column width in pixels by given sheet
// getColWidth provides function to get column width in pixels by given sheet
// name and column index.
func (f *File) GetColWidth(sheet string, col int) int {
func (f *File) getColWidth(sheet string, col int) int {
xlsx := f.workSheetReader(sheet)
if xlsx.Cols != nil {
var width float64
@ -221,21 +220,27 @@ func (f *File) GetColWidth(sheet string, col int) int {
}
}
// Optimisation for when the column widths haven't changed.
return defaultColWidthPixels
return int(defaultColWidthPixels)
}
// GetRowHeight provides function to get row height in pixels by given sheet
// name and row index.
func (f *File) GetRowHeight(sheet string, row int) int {
// GetColWidth provides function to get column width by given sheet name and
// column index.
func (f *File) GetColWidth(sheet, column string) float64 {
col := TitleToNumber(strings.ToUpper(column)) + 1
xlsx := f.workSheetReader(sheet)
for _, v := range xlsx.SheetData.Row {
if v.R == row && v.Ht != "" {
ht, _ := strconv.ParseFloat(v.Ht, 64)
return int(convertRowHeightToPixels(ht))
if xlsx.Cols != nil {
var width float64
for _, v := range xlsx.Cols.Col {
if v.Min <= col && col <= v.Max {
width = v.Width
}
}
if width != 0 {
return width
}
}
// Optimisation for when the row heights haven't changed.
return defaultRowHeightPixels
// Optimisation for when the column widths haven't changed.
return defaultColWidthPixels
}
// convertColWidthToPixels provieds function to convert the width of a cell from

@ -177,14 +177,30 @@ func TestNewFile(t *testing.T) {
}
}
func TestSetColWidth(t *testing.T) {
func TestColWidth(t *testing.T) {
xlsx := NewFile()
xlsx.SetColWidth("sheet1", "B", "A", 12)
xlsx.SetColWidth("sheet1", "A", "B", 12)
xlsx.GetColWidth("sheet1", "A")
xlsx.GetColWidth("sheet1", "C")
err := xlsx.SaveAs("./test/Workbook_4.xlsx")
if err != nil {
t.Log(err)
}
convertRowHeightToPixels(0)
}
func TestRowHeight(t *testing.T) {
xlsx := NewFile()
xlsx.SetRowHeight("Sheet1", 0, 50)
xlsx.SetRowHeight("Sheet1", 3, 90)
t.Log(xlsx.GetRowHeight("Sheet1", 1))
t.Log(xlsx.GetRowHeight("Sheet1", 3))
err := xlsx.SaveAs("./test/Workbook_5.xlsx")
if err != nil {
t.Log(err)
}
convertColWidthToPixels(0)
}
func TestSetCellHyperLink(t *testing.T) {
@ -268,16 +284,6 @@ func TestMergeCell(t *testing.T) {
}
}
func TestSetRowHeight(t *testing.T) {
xlsx := NewFile()
xlsx.SetRowHeight("Sheet1", 0, 50)
xlsx.SetRowHeight("Sheet1", 3, 90)
err := xlsx.SaveAs("./test/Workbook_5.xlsx")
if err != nil {
t.Log(err)
}
}
func TestSetCellStyleAlignment(t *testing.T) {
xlsx, err := OpenFile("./test/Workbook_2.xlsx")
if err != nil {

@ -116,10 +116,36 @@ func (f *File) SetRowHeight(sheet string, rowIndex int, height float64) {
rows := rowIndex + 1
cells := 0
completeRow(xlsx, rows, cells)
xlsx.SheetData.Row[rowIndex].Ht = strconv.FormatFloat(height, 'f', -1, 64)
xlsx.SheetData.Row[rowIndex].Ht = height
xlsx.SheetData.Row[rowIndex].CustomHeight = true
}
// getRowHeight provides function to get row height in pixels by given sheet
// name and row index.
func (f *File) getRowHeight(sheet string, row int) int {
xlsx := f.workSheetReader(sheet)
for _, v := range xlsx.SheetData.Row {
if v.R == row+1 && v.Ht != 0 {
return int(convertRowHeightToPixels(v.Ht))
}
}
// Optimisation for when the row heights haven't changed.
return int(defaultRowHeightPixels)
}
// GetRowHeight provides function to get row height by given worksheet name and
// row index.
func (f *File) GetRowHeight(sheet string, row int) float64 {
xlsx := f.workSheetReader(sheet)
for _, v := range xlsx.SheetData.Row {
if v.R == row+1 && v.Ht != 0 {
return v.Ht
}
}
// Optimisation for when the row heights haven't changed.
return defaultRowHeightPixels
}
// readXMLSST read xmlSST simple function.
func readXMLSST(f *File) (*xlsxSST, error) {
shardStrings := xlsxSST{}

@ -263,7 +263,7 @@ type xlsxRow struct {
CustomFormat bool `xml:"customFormat,attr,omitempty"`
CustomHeight bool `xml:"customHeight,attr,omitempty"`
Hidden bool `xml:"hidden,attr,omitempty"`
Ht string `xml:"ht,attr,omitempty"`
Ht float64 `xml:"ht,attr,omitempty"`
OutlineLevel uint8 `xml:"outlineLevel,attr,omitempty"`
Ph bool `xml:"ph,attr,omitempty"`
R int `xml:"r,attr,omitempty"`

Loading…
Cancel
Save