2019-12-29 16:02:31 +08:00
|
|
|
// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
|
2018-09-14 00:44:23 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
|
|
|
// Package excelize providing a set of functions that allow you to write to
|
|
|
|
// and read from XLSX files. Support reads and writes XLSX file generated by
|
|
|
|
// Microsoft Excel™ 2007 and later. Support save file without losing original
|
2019-08-11 00:36:14 +08:00
|
|
|
// charts of XLSX. This library needs Go version 1.10 or later.
|
2018-09-14 00:58:48 +08:00
|
|
|
|
2017-01-18 14:47:23 +08:00
|
|
|
package excelize
|
|
|
|
|
2019-05-16 13:36:50 +08:00
|
|
|
import (
|
2019-09-24 21:53:19 +08:00
|
|
|
"errors"
|
2019-05-16 13:36:50 +08:00
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
)
|
2017-01-18 14:47:23 +08:00
|
|
|
|
2017-01-22 16:16:03 +08:00
|
|
|
// Define the default cell size and EMU unit of measurement.
|
|
|
|
const (
|
2017-06-29 11:14:33 +08:00
|
|
|
defaultColWidthPixels float64 = 64
|
|
|
|
defaultRowHeightPixels float64 = 20
|
|
|
|
EMU int = 9525
|
2017-01-22 16:16:03 +08:00
|
|
|
)
|
|
|
|
|
2017-06-15 11:03:29 +08:00
|
|
|
// GetColVisible provides a function to get visible of a single column by given
|
2017-10-31 16:33:36 +08:00
|
|
|
// worksheet name and column name. For example, get visible state of column D
|
2017-06-15 11:03:29 +08:00
|
|
|
// in Sheet1:
|
|
|
|
//
|
2020-01-22 06:42:44 +08:00
|
|
|
// visible, err := f.GetColVisible("Sheet1", "D")
|
2017-06-15 11:03:29 +08:00
|
|
|
//
|
2019-03-23 20:08:06 +08:00
|
|
|
func (f *File) GetColVisible(sheet, col string) (bool, error) {
|
|
|
|
visible := true
|
|
|
|
colNum, err := ColumnNameToNumber(col)
|
|
|
|
if err != nil {
|
|
|
|
return visible, err
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-06-15 11:03:29 +08:00
|
|
|
if xlsx.Cols == nil {
|
2019-03-23 20:08:06 +08:00
|
|
|
return visible, err
|
2017-06-15 11:03:29 +08:00
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2017-10-16 10:42:43 +08:00
|
|
|
for c := range xlsx.Cols.Col {
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
colData := &xlsx.Cols.Col[c]
|
|
|
|
if colData.Min <= colNum && colNum <= colData.Max {
|
|
|
|
visible = !colData.Hidden
|
2017-06-15 11:03:29 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 20:08:06 +08:00
|
|
|
return visible, err
|
2017-06-15 11:03:29 +08:00
|
|
|
}
|
|
|
|
|
2020-01-22 06:42:44 +08:00
|
|
|
// SetColVisible provides a function to set visible columns by given worksheet
|
|
|
|
// name, columns range and visibility.
|
|
|
|
//
|
|
|
|
// For example hide column D on Sheet1:
|
2017-06-15 11:03:29 +08:00
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// err := f.SetColVisible("Sheet1", "D", false)
|
2017-06-15 11:03:29 +08:00
|
|
|
//
|
2020-01-22 06:42:44 +08:00
|
|
|
// Hide the columns from D to F (included)
|
|
|
|
//
|
|
|
|
// err := f.SetColVisible("Sheet1", "D:F", false)
|
|
|
|
//
|
|
|
|
func (f *File) SetColVisible(sheet, columns string, visible bool) error {
|
|
|
|
var max int
|
|
|
|
|
|
|
|
colsTab := strings.Split(columns, ":")
|
|
|
|
min, err := ColumnNameToNumber(colsTab[0])
|
2019-03-23 20:08:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-22 06:42:44 +08:00
|
|
|
if len(colsTab) == 2 {
|
|
|
|
max, err = ColumnNameToNumber(colsTab[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
max = min
|
|
|
|
}
|
|
|
|
if max < min {
|
|
|
|
min, max = max, min
|
2017-06-15 11:03:29 +08:00
|
|
|
}
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-22 06:42:44 +08:00
|
|
|
colData := xlsxCol{
|
|
|
|
Min: min,
|
|
|
|
Max: max,
|
|
|
|
Width: 9, // default width
|
|
|
|
Hidden: !visible,
|
|
|
|
CustomWidth: true,
|
|
|
|
}
|
|
|
|
if xlsx.Cols != nil {
|
|
|
|
xlsx.Cols.Col = append(xlsx.Cols.Col, colData)
|
|
|
|
} else {
|
2017-06-15 11:03:29 +08:00
|
|
|
cols := xlsxCols{}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
cols.Col = append(cols.Col, colData)
|
2017-06-15 11:03:29 +08:00
|
|
|
xlsx.Cols = &cols
|
|
|
|
}
|
2020-01-22 06:42:44 +08:00
|
|
|
return nil
|
2017-06-15 11:03:29 +08:00
|
|
|
}
|
|
|
|
|
2020-01-22 06:42:44 +08:00
|
|
|
|
2018-05-11 10:14:18 +08:00
|
|
|
// GetColOutlineLevel provides a function to get outline level of a single
|
|
|
|
// column by given worksheet name and column name. For example, get outline
|
|
|
|
// level of column D in Sheet1:
|
2018-05-08 02:32:20 +08:00
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// level, err := f.GetColOutlineLevel("Sheet1", "D")
|
2018-05-08 02:32:20 +08:00
|
|
|
//
|
2019-03-23 20:08:06 +08:00
|
|
|
func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
|
2018-05-08 02:32:20 +08:00
|
|
|
level := uint8(0)
|
2019-03-23 20:08:06 +08:00
|
|
|
colNum, err := ColumnNameToNumber(col)
|
|
|
|
if err != nil {
|
|
|
|
return level, err
|
|
|
|
}
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-05-08 02:32:20 +08:00
|
|
|
if xlsx.Cols == nil {
|
2019-03-23 20:08:06 +08:00
|
|
|
return level, err
|
2018-05-08 02:32:20 +08:00
|
|
|
}
|
|
|
|
for c := range xlsx.Cols.Col {
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
colData := &xlsx.Cols.Col[c]
|
|
|
|
if colData.Min <= colNum && colNum <= colData.Max {
|
2019-09-26 22:28:14 +08:00
|
|
|
level = colData.OutlineLevel
|
2018-05-08 02:32:20 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 20:08:06 +08:00
|
|
|
return level, err
|
2018-05-08 02:32:20 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 10:14:18 +08:00
|
|
|
// SetColOutlineLevel provides a function to set outline level of a single
|
2019-09-24 21:53:19 +08:00
|
|
|
// column by given worksheet name and column name. The value of parameter
|
|
|
|
// 'level' is 1-7. For example, set outline level of column D in Sheet1 to 2:
|
2018-05-08 02:32:20 +08:00
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// err := f.SetColOutlineLevel("Sheet1", "D", 2)
|
2018-05-08 02:32:20 +08:00
|
|
|
//
|
2019-03-23 20:08:06 +08:00
|
|
|
func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
|
2019-09-24 21:53:19 +08:00
|
|
|
if level > 7 || level < 1 {
|
|
|
|
return errors.New("invalid outline level")
|
|
|
|
}
|
2019-03-23 20:08:06 +08:00
|
|
|
colNum, err := ColumnNameToNumber(col)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
colData := xlsxCol{
|
|
|
|
Min: colNum,
|
|
|
|
Max: colNum,
|
2018-05-08 02:32:20 +08:00
|
|
|
OutlineLevel: level,
|
|
|
|
CustomWidth: true,
|
|
|
|
}
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-08 02:32:20 +08:00
|
|
|
if xlsx.Cols == nil {
|
|
|
|
cols := xlsxCols{}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
cols.Col = append(cols.Col, colData)
|
2018-05-08 02:32:20 +08:00
|
|
|
xlsx.Cols = &cols
|
2019-03-23 20:08:06 +08:00
|
|
|
return err
|
2018-05-08 02:32:20 +08:00
|
|
|
}
|
|
|
|
for v := range xlsx.Cols.Col {
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
if xlsx.Cols.Col[v].Min <= colNum && colNum <= xlsx.Cols.Col[v].Max {
|
|
|
|
colData = xlsx.Cols.Col[v]
|
2018-05-08 02:32:20 +08:00
|
|
|
}
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
colData.Min = colNum
|
|
|
|
colData.Max = colNum
|
|
|
|
colData.OutlineLevel = level
|
|
|
|
colData.CustomWidth = true
|
|
|
|
xlsx.Cols.Col = append(xlsx.Cols.Col, colData)
|
2019-03-23 20:08:06 +08:00
|
|
|
return err
|
2018-05-08 02:32:20 +08:00
|
|
|
}
|
|
|
|
|
2019-05-16 13:36:50 +08:00
|
|
|
// SetColStyle provides a function to set style of columns by given worksheet
|
|
|
|
// name, columns range and style ID.
|
|
|
|
//
|
|
|
|
// For example set style of column H on Sheet1:
|
|
|
|
//
|
|
|
|
// err = f.SetColStyle("Sheet1", "H", style)
|
|
|
|
//
|
|
|
|
// Set style of columns C:F on Sheet1:
|
|
|
|
//
|
|
|
|
// err = f.SetColStyle("Sheet1", "C:F", style)
|
|
|
|
//
|
|
|
|
func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
|
|
|
xlsx, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var c1, c2 string
|
|
|
|
var min, max int
|
|
|
|
cols := strings.Split(columns, ":")
|
|
|
|
c1 = cols[0]
|
|
|
|
min, err = ColumnNameToNumber(c1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(cols) == 2 {
|
|
|
|
c2 = cols[1]
|
|
|
|
max, err = ColumnNameToNumber(c2)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
max = min
|
|
|
|
}
|
|
|
|
if max < min {
|
|
|
|
min, max = max, min
|
|
|
|
}
|
|
|
|
if xlsx.Cols == nil {
|
|
|
|
xlsx.Cols = &xlsxCols{}
|
|
|
|
}
|
|
|
|
var find bool
|
|
|
|
for idx, col := range xlsx.Cols.Col {
|
|
|
|
if col.Min == min && col.Max == max {
|
|
|
|
xlsx.Cols.Col[idx].Style = styleID
|
|
|
|
find = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !find {
|
|
|
|
xlsx.Cols.Col = append(xlsx.Cols.Col, xlsxCol{
|
|
|
|
Min: min,
|
|
|
|
Max: max,
|
|
|
|
Width: 9,
|
|
|
|
Style: styleID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// SetColWidth provides a function to set the width of a single column or
|
|
|
|
// multiple columns. For example:
|
2017-01-18 14:47:23 +08:00
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// f := excelize.NewFile()
|
|
|
|
// err := f.SetColWidth("Sheet1", "A", "H", 20)
|
2017-01-18 14:47:23 +08:00
|
|
|
//
|
2019-03-23 20:08:06 +08:00
|
|
|
func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error {
|
|
|
|
min, err := ColumnNameToNumber(startcol)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
max, err := ColumnNameToNumber(endcol)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-18 14:47:23 +08:00
|
|
|
if min > max {
|
|
|
|
min, max = max, min
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-18 14:47:23 +08:00
|
|
|
col := xlsxCol{
|
|
|
|
Min: min,
|
|
|
|
Max: max,
|
|
|
|
Width: width,
|
|
|
|
CustomWidth: true,
|
|
|
|
}
|
|
|
|
if xlsx.Cols != nil {
|
|
|
|
xlsx.Cols.Col = append(xlsx.Cols.Col, col)
|
|
|
|
} else {
|
|
|
|
cols := xlsxCols{}
|
|
|
|
cols.Col = append(cols.Col, col)
|
|
|
|
xlsx.Cols = &cols
|
|
|
|
}
|
2019-03-23 20:08:06 +08:00
|
|
|
return err
|
2017-01-18 14:47:23 +08:00
|
|
|
}
|
2017-01-22 16:16:03 +08:00
|
|
|
|
|
|
|
// positionObjectPixels calculate the vertices that define the position of a
|
|
|
|
// graphical object within the worksheet in pixels.
|
|
|
|
//
|
|
|
|
// +------------+------------+
|
|
|
|
// | A | B |
|
|
|
|
// +-----+------------+------------+
|
|
|
|
// | |(x1,y1) | |
|
|
|
|
// | 1 |(A1)._______|______ |
|
|
|
|
// | | | | |
|
|
|
|
// | | | | |
|
|
|
|
// +-----+----| OBJECT |-----+
|
|
|
|
// | | | | |
|
|
|
|
// | 2 | |______________. |
|
|
|
|
// | | | (B2)|
|
|
|
|
// | | | (x2,y2)|
|
|
|
|
// +-----+------------+------------+
|
|
|
|
//
|
2017-01-24 18:29:02 +08:00
|
|
|
// Example of an object that covers some of the area from cell A1 to B2.
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
|
|
|
// Based on the width and height of the object we need to calculate 8 vars:
|
|
|
|
//
|
|
|
|
// colStart, rowStart, colEnd, rowEnd, x1, y1, x2, y2.
|
|
|
|
//
|
|
|
|
// We also calculate the absolute x and y position of the top left vertex of
|
|
|
|
// the object. This is required for images.
|
|
|
|
//
|
|
|
|
// The width and height of the cells that the object occupies can be
|
|
|
|
// variable and have to be taken into account.
|
|
|
|
//
|
|
|
|
// The values of col_start and row_start are passed in from the calling
|
|
|
|
// function. The values of col_end and row_end are calculated by
|
|
|
|
// subtracting the width and height of the object from the width and
|
|
|
|
// height of the underlying cells.
|
|
|
|
//
|
|
|
|
// colStart # Col containing upper left corner of object.
|
|
|
|
// x1 # Distance to left side of object.
|
|
|
|
//
|
|
|
|
// rowStart # Row containing top left corner of object.
|
|
|
|
// y1 # Distance to top of object.
|
|
|
|
//
|
|
|
|
// colEnd # Col containing lower right corner of object.
|
|
|
|
// x2 # Distance to right side of object.
|
|
|
|
//
|
|
|
|
// rowEnd # Row containing bottom right corner of object.
|
|
|
|
// y2 # Distance to bottom of object.
|
|
|
|
//
|
|
|
|
// width # Width of object frame.
|
|
|
|
// height # Height of object frame.
|
|
|
|
//
|
|
|
|
// xAbs # Absolute distance to left side of object.
|
|
|
|
// yAbs # Absolute distance to top side of object.
|
|
|
|
//
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int, int, int) {
|
2017-01-22 16:16:03 +08:00
|
|
|
xAbs := 0
|
|
|
|
yAbs := 0
|
|
|
|
|
|
|
|
// Calculate the absolute x offset of the top-left vertex.
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
for colID := 1; colID <= col; colID++ {
|
2017-06-29 11:14:33 +08:00
|
|
|
xAbs += f.getColWidth(sheet, colID)
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
xAbs += x1
|
|
|
|
|
|
|
|
// Calculate the absolute y offset of the top-left vertex.
|
|
|
|
// Store the column change to allow optimisations.
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
for rowID := 1; rowID <= row; rowID++ {
|
2017-06-29 11:14:33 +08:00
|
|
|
yAbs += f.getRowHeight(sheet, rowID)
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
yAbs += y1
|
|
|
|
|
|
|
|
// Adjust start column for offsets that are greater than the col width.
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
for x1 >= f.getColWidth(sheet, col) {
|
|
|
|
x1 -= f.getColWidth(sheet, col)
|
|
|
|
col++
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust start row for offsets that are greater than the row height.
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
for y1 >= f.getRowHeight(sheet, row) {
|
|
|
|
y1 -= f.getRowHeight(sheet, row)
|
|
|
|
row++
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise end cell to the same as the start cell.
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
colEnd := col
|
|
|
|
rowEnd := row
|
2017-01-22 16:16:03 +08:00
|
|
|
|
|
|
|
width += x1
|
|
|
|
height += y1
|
|
|
|
|
|
|
|
// Subtract the underlying cell widths to find end cell of the object.
|
2019-04-04 16:34:05 +08:00
|
|
|
for width >= f.getColWidth(sheet, colEnd+1) {
|
2017-01-22 16:16:03 +08:00
|
|
|
colEnd++
|
2017-06-29 11:14:33 +08:00
|
|
|
width -= f.getColWidth(sheet, colEnd)
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract the underlying cell heights to find end cell of the object.
|
2017-06-29 11:14:33 +08:00
|
|
|
for height >= f.getRowHeight(sheet, rowEnd) {
|
|
|
|
height -= f.getRowHeight(sheet, rowEnd)
|
2019-04-04 16:26:26 +08:00
|
|
|
rowEnd++
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The end vertices are whatever is left from the width and height.
|
|
|
|
x2 := width
|
|
|
|
y2 := height
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
return col, row, xAbs, yAbs, colEnd, rowEnd, x2, y2
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// getColWidth provides a function to get column width in pixels by given
|
|
|
|
// sheet name and column index.
|
2017-06-29 11:14:33 +08:00
|
|
|
func (f *File) getColWidth(sheet string, col int) int {
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, _ := f.workSheetReader(sheet)
|
2017-01-22 16:16:03 +08:00
|
|
|
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 int(convertColWidthToPixels(width))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Optimisation for when the column widths haven't changed.
|
2017-06-29 11:14:33 +08:00
|
|
|
return int(defaultColWidthPixels)
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// GetColWidth provides a function to get column width by given worksheet name
|
|
|
|
// and column index.
|
2019-03-23 20:08:06 +08:00
|
|
|
func (f *File) GetColWidth(sheet, col string) (float64, error) {
|
|
|
|
colNum, err := ColumnNameToNumber(col)
|
|
|
|
if err != nil {
|
|
|
|
return defaultColWidthPixels, err
|
|
|
|
}
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return defaultColWidthPixels, err
|
|
|
|
}
|
2017-06-29 11:14:33 +08:00
|
|
|
if xlsx.Cols != nil {
|
|
|
|
var width float64
|
|
|
|
for _, v := range xlsx.Cols.Col {
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
if v.Min <= colNum && colNum <= v.Max {
|
2017-06-29 11:14:33 +08:00
|
|
|
width = v.Width
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if width != 0 {
|
2019-03-23 20:08:06 +08:00
|
|
|
return width, err
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 11:14:33 +08:00
|
|
|
// Optimisation for when the column widths haven't changed.
|
2019-03-23 20:08:06 +08:00
|
|
|
return defaultColWidthPixels, err
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// InsertCol provides a function to insert a new column before given column
|
|
|
|
// index. For example, create a new column before column C in Sheet1:
|
2017-07-24 10:26:02 +08:00
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// err := f.InsertCol("Sheet1", "C")
|
2017-07-24 10:26:02 +08:00
|
|
|
//
|
2019-03-23 20:08:06 +08:00
|
|
|
func (f *File) InsertCol(sheet, col string) error {
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
num, err := ColumnNameToNumber(col)
|
|
|
|
if err != nil {
|
2019-03-23 20:08:06 +08:00
|
|
|
return err
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
}
|
2019-03-23 20:08:06 +08:00
|
|
|
return f.adjustHelper(sheet, columns, num, 1)
|
2017-07-24 10:26:02 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// RemoveCol provides a function to remove single column by given worksheet
|
|
|
|
// name and column index. For example, remove column C in Sheet1:
|
2017-07-24 10:26:02 +08:00
|
|
|
//
|
2019-04-20 14:57:50 +08:00
|
|
|
// err := f.RemoveCol("Sheet1", "C")
|
2017-07-24 10:26:02 +08:00
|
|
|
//
|
2019-02-22 22:17:38 +08:00
|
|
|
// Use this method with caution, which will affect changes in references such
|
|
|
|
// as formulas, charts, and so on. If there is any referenced value of the
|
|
|
|
// worksheet, it will cause a file error when you open it. The excelize only
|
|
|
|
// partially updates these references currently.
|
2019-03-23 20:08:06 +08:00
|
|
|
func (f *File) RemoveCol(sheet, col string) error {
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
num, err := ColumnNameToNumber(col)
|
|
|
|
if err != nil {
|
2019-03-23 20:08:06 +08:00
|
|
|
return err
|
2017-07-24 10:26:02 +08:00
|
|
|
}
|
|
|
|
|
2019-04-15 11:22:57 +08:00
|
|
|
xlsx, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
for rowIdx := range xlsx.SheetData.Row {
|
2019-03-21 18:44:30 +08:00
|
|
|
rowData := &xlsx.SheetData.Row[rowIdx]
|
|
|
|
for colIdx := range rowData.C {
|
|
|
|
colName, _, _ := SplitCellName(rowData.C[colIdx].R)
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
if colName == col {
|
2019-03-21 18:44:30 +08:00
|
|
|
rowData.C = append(rowData.C[:colIdx], rowData.C[colIdx+1:]...)[:len(rowData.C)-1]
|
|
|
|
break
|
2017-07-24 10:26:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 20:08:06 +08:00
|
|
|
return f.adjustHelper(sheet, columns, num, -1)
|
2017-07-24 10:26:02 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// convertColWidthToPixels provieds function to convert the width of a cell
|
|
|
|
// from user's units to pixels. Excel rounds the column width to the nearest
|
|
|
|
// pixel. If the width hasn't been set by the user we use the default value.
|
|
|
|
// If the column is hidden it has a value of zero.
|
2017-01-22 16:16:03 +08:00
|
|
|
func convertColWidthToPixels(width float64) float64 {
|
|
|
|
var padding float64 = 5
|
|
|
|
var pixels float64
|
|
|
|
var maxDigitWidth float64 = 7
|
|
|
|
if width == 0 {
|
|
|
|
return pixels
|
|
|
|
}
|
|
|
|
if width < 1 {
|
|
|
|
pixels = (width * 12) + 0.5
|
|
|
|
return math.Ceil(pixels)
|
|
|
|
}
|
|
|
|
pixels = (width*maxDigitWidth + 0.5) + padding
|
|
|
|
return math.Ceil(pixels)
|
|
|
|
}
|