2017-01-18 14:47:23 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
2017-07-24 10:26:02 +08:00
|
|
|
"bytes"
|
2017-01-22 16:16:03 +08:00
|
|
|
"math"
|
2017-07-24 10:26:02 +08:00
|
|
|
"strconv"
|
2017-01-18 14:47:23 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
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:
|
|
|
|
//
|
|
|
|
// xlsx.GetColVisible("Sheet1", "D")
|
|
|
|
//
|
|
|
|
func (f *File) GetColVisible(sheet, column string) bool {
|
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-06-27 17:53:06 +08:00
|
|
|
col := TitleToNumber(strings.ToUpper(column)) + 1
|
2017-06-15 11:03:29 +08:00
|
|
|
visible := true
|
|
|
|
if xlsx.Cols == nil {
|
|
|
|
return visible
|
|
|
|
}
|
2017-10-16 10:42:43 +08:00
|
|
|
for c := range xlsx.Cols.Col {
|
|
|
|
if xlsx.Cols.Col[c].Min <= col && col <= xlsx.Cols.Col[c].Max {
|
|
|
|
visible = !xlsx.Cols.Col[c].Hidden
|
2017-06-15 11:03:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return visible
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetColVisible provides a function to set visible of a single column by given
|
2017-10-31 16:33:36 +08:00
|
|
|
// worksheet name and column name. For example, hide column D in Sheet1:
|
2017-06-15 11:03:29 +08:00
|
|
|
//
|
|
|
|
// xlsx.SetColVisible("Sheet1", "D", false)
|
|
|
|
//
|
|
|
|
func (f *File) SetColVisible(sheet, column string, visible bool) {
|
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-06-27 17:53:06 +08:00
|
|
|
c := TitleToNumber(strings.ToUpper(column)) + 1
|
2017-06-15 11:03:29 +08:00
|
|
|
col := xlsxCol{
|
|
|
|
Min: c,
|
|
|
|
Max: c,
|
|
|
|
Hidden: !visible,
|
|
|
|
CustomWidth: true,
|
|
|
|
}
|
|
|
|
if xlsx.Cols == nil {
|
|
|
|
cols := xlsxCols{}
|
|
|
|
cols.Col = append(cols.Col, col)
|
|
|
|
xlsx.Cols = &cols
|
|
|
|
return
|
|
|
|
}
|
2017-10-16 10:42:43 +08:00
|
|
|
for v := range xlsx.Cols.Col {
|
|
|
|
if xlsx.Cols.Col[v].Min <= c && c <= xlsx.Cols.Col[v].Max {
|
|
|
|
col = xlsx.Cols.Col[v]
|
2017-06-15 11:03:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
col.Min = c
|
|
|
|
col.Max = c
|
|
|
|
col.Hidden = !visible
|
|
|
|
col.CustomWidth = true
|
|
|
|
xlsx.Cols.Col = append(xlsx.Cols.Col, col)
|
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// SetColWidth provides function to set the width of a single column or multiple
|
|
|
|
// columns. For example:
|
2017-01-18 14:47:23 +08:00
|
|
|
//
|
2017-06-28 17:03:20 +08:00
|
|
|
// xlsx := excelize.NewFile()
|
2017-01-18 14:47:23 +08:00
|
|
|
// xlsx.SetColWidth("Sheet1", "A", "H", 20)
|
|
|
|
// err := xlsx.Save()
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) {
|
2017-06-27 17:53:06 +08:00
|
|
|
min := TitleToNumber(strings.ToUpper(startcol)) + 1
|
|
|
|
max := TitleToNumber(strings.ToUpper(endcol)) + 1
|
2017-01-18 14:47:23 +08:00
|
|
|
if min > max {
|
|
|
|
min, max = max, min
|
|
|
|
}
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
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
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
//
|
|
|
|
func (f *File) positionObjectPixels(sheet string, colStart, rowStart, x1, y1, width, height int) (int, int, int, int, int, int, int, int) {
|
|
|
|
xAbs := 0
|
|
|
|
yAbs := 0
|
|
|
|
|
|
|
|
// Calculate the absolute x offset of the top-left vertex.
|
|
|
|
for colID := 1; colID <= colStart; 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.
|
|
|
|
for rowID := 1; rowID <= rowStart; 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.
|
2017-06-29 11:14:33 +08:00
|
|
|
for x1 >= f.getColWidth(sheet, colStart) {
|
|
|
|
x1 -= f.getColWidth(sheet, colStart)
|
2017-01-22 16:16:03 +08:00
|
|
|
colStart++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust start row for offsets that are greater than the row height.
|
2017-06-29 11:14:33 +08:00
|
|
|
for y1 >= f.getRowHeight(sheet, rowStart) {
|
|
|
|
y1 -= f.getRowHeight(sheet, rowStart)
|
2017-01-22 16:16:03 +08:00
|
|
|
rowStart++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise end cell to the same as the start cell.
|
|
|
|
colEnd := colStart
|
|
|
|
rowEnd := rowStart
|
|
|
|
|
|
|
|
width += x1
|
|
|
|
height += y1
|
|
|
|
|
|
|
|
// Subtract the underlying cell widths to find end cell of the object.
|
2017-06-29 11:14:33 +08:00
|
|
|
for width >= f.getColWidth(sheet, colEnd) {
|
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) {
|
2017-01-22 16:16:03 +08:00
|
|
|
rowEnd++
|
2017-06-29 11:14:33 +08:00
|
|
|
height -= f.getRowHeight(sheet, 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
|
|
|
|
return colStart, rowStart, xAbs, yAbs, colEnd, rowEnd, x2, y2
|
|
|
|
}
|
|
|
|
|
2017-06-29 11:14:33 +08:00
|
|
|
// getColWidth provides function to get column width in pixels by given sheet
|
2017-01-22 16:16:03 +08:00
|
|
|
// name and column index.
|
2017-06-29 11:14:33 +08:00
|
|
|
func (f *File) getColWidth(sheet string, col int) int {
|
2017-03-12 20:38:26 +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
|
|
|
}
|
|
|
|
|
2017-09-13 22:00:33 +08:00
|
|
|
// GetColWidth provides function to get column width by given worksheet name and
|
2017-06-29 11:14:33 +08:00
|
|
|
// column index.
|
|
|
|
func (f *File) GetColWidth(sheet, column string) float64 {
|
|
|
|
col := TitleToNumber(strings.ToUpper(column)) + 1
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-06-29 11:14:33 +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 width
|
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.
|
|
|
|
return defaultColWidthPixels
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
|
2017-07-24 10:26:02 +08:00
|
|
|
// InsertCol provides function to insert a new column before given column index.
|
|
|
|
// For example, create a new column before column C in Sheet1:
|
|
|
|
//
|
|
|
|
// xlsx.InsertCol("Sheet1", "C")
|
|
|
|
//
|
|
|
|
func (f *File) InsertCol(sheet, column string) {
|
|
|
|
col := TitleToNumber(strings.ToUpper(column))
|
|
|
|
f.adjustHelper(sheet, col, -1, 1)
|
|
|
|
}
|
|
|
|
|
2017-09-13 22:00:33 +08:00
|
|
|
// RemoveCol provides function to remove single column by given worksheet name
|
2017-07-24 10:26:02 +08:00
|
|
|
// and column index. For example, remove column C in Sheet1:
|
|
|
|
//
|
|
|
|
// xlsx.RemoveCol("Sheet1", "C")
|
|
|
|
//
|
|
|
|
func (f *File) RemoveCol(sheet, column string) {
|
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-10-16 10:42:43 +08:00
|
|
|
for r := range xlsx.SheetData.Row {
|
|
|
|
for k, v := range xlsx.SheetData.Row[r].C {
|
2017-07-24 10:26:02 +08:00
|
|
|
axis := v.R
|
|
|
|
col := string(strings.Map(letterOnlyMapF, axis))
|
|
|
|
if col == column {
|
2017-10-16 10:42:43 +08:00
|
|
|
xlsx.SheetData.Row[r].C = append(xlsx.SheetData.Row[r].C[:k], xlsx.SheetData.Row[r].C[k+1:]...)
|
2017-07-24 10:26:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
col := TitleToNumber(strings.ToUpper(column))
|
|
|
|
f.adjustHelper(sheet, col, -1, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Completion column element tags of XML in a sheet.
|
|
|
|
func completeCol(xlsx *xlsxWorksheet, row, cell int) {
|
|
|
|
buffer := bytes.Buffer{}
|
2017-10-16 10:42:43 +08:00
|
|
|
for r := range xlsx.SheetData.Row {
|
|
|
|
if len(xlsx.SheetData.Row[r].C) < cell {
|
|
|
|
start := len(xlsx.SheetData.Row[r].C)
|
2017-07-24 10:26:02 +08:00
|
|
|
for iii := start; iii < cell; iii++ {
|
|
|
|
buffer.WriteString(ToAlphaString(iii))
|
2017-10-16 10:42:43 +08:00
|
|
|
buffer.WriteString(strconv.Itoa(r + 1))
|
|
|
|
xlsx.SheetData.Row[r].C = append(xlsx.SheetData.Row[r].C, xlsxC{
|
2017-07-24 10:26:02 +08:00
|
|
|
R: buffer.String(),
|
|
|
|
})
|
|
|
|
buffer.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:16:03 +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.
|
|
|
|
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)
|
|
|
|
}
|