2023-01-02 11:47:31 +08:00
|
|
|
// Copyright 2016 - 2023 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.
|
|
|
|
//
|
2022-02-17 00:09:11 +08:00
|
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
|
|
|
|
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
|
|
|
|
// Supports complex components by high compatibility, and provided streaming
|
|
|
|
// API for generating or reading data from a worksheet with huge amounts of
|
2023-01-02 11:47:31 +08:00
|
|
|
// data. This library needs Go version 1.16 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 (
|
2020-06-16 17:53:22 +08:00
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
2019-05-16 13:36:50 +08:00
|
|
|
"math"
|
2020-06-22 00:05:19 +08:00
|
|
|
"strconv"
|
2019-05-16 13:36:50 +08:00
|
|
|
"strings"
|
2020-02-07 00:25:01 +08:00
|
|
|
|
|
|
|
"github.com/mohae/deepcopy"
|
2019-05-16 13:36:50 +08:00
|
|
|
)
|
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 (
|
2021-03-05 00:40:37 +08:00
|
|
|
defaultColWidth float64 = 9.140625
|
2017-06-29 11:14:33 +08:00
|
|
|
defaultColWidthPixels float64 = 64
|
2020-08-22 18:58:43 +08:00
|
|
|
defaultRowHeight float64 = 15
|
2017-06-29 11:14:33 +08:00
|
|
|
defaultRowHeightPixels float64 = 20
|
|
|
|
EMU int = 9525
|
2017-01-22 16:16:03 +08:00
|
|
|
)
|
|
|
|
|
2020-06-16 17:53:22 +08:00
|
|
|
// Cols defines an iterator to a sheet
|
|
|
|
type Cols struct {
|
2021-11-05 00:01:34 +08:00
|
|
|
err error
|
|
|
|
curCol, totalCols, totalRows, stashCol int
|
|
|
|
rawCellValue bool
|
|
|
|
sheet string
|
|
|
|
f *File
|
|
|
|
sheetXML []byte
|
2022-11-12 00:02:11 +08:00
|
|
|
sst *xlsxSST
|
2021-11-05 00:01:34 +08:00
|
|
|
}
|
|
|
|
|
2022-07-18 00:21:34 +08:00
|
|
|
// GetCols gets the value of all cells by columns on the worksheet based on the
|
|
|
|
// given worksheet name, returned as a two-dimensional array, where the value
|
|
|
|
// of the cell is converted to the `string` type. If the cell format can be
|
|
|
|
// applied to the value of the cell, the applied value will be used, otherwise
|
|
|
|
// the original value will be used.
|
|
|
|
//
|
|
|
|
// For example, get and traverse the value of all cells by columns on a
|
|
|
|
// worksheet named
|
|
|
|
// 'Sheet1':
|
2020-06-16 17:53:22 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// cols, err := f.GetCols("Sheet1")
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// for _, col := range cols {
|
|
|
|
// for _, rowCell := range col {
|
|
|
|
// fmt.Print(rowCell, "\t")
|
|
|
|
// }
|
|
|
|
// fmt.Println()
|
|
|
|
// }
|
2021-09-05 11:59:50 +08:00
|
|
|
func (f *File) GetCols(sheet string, opts ...Options) ([][]string, error) {
|
2020-06-16 17:53:22 +08:00
|
|
|
cols, err := f.Cols(sheet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results := make([][]string, 0, 64)
|
|
|
|
for cols.Next() {
|
2021-09-05 11:59:50 +08:00
|
|
|
col, _ := cols.Rows(opts...)
|
2020-06-16 17:53:22 +08:00
|
|
|
results = append(results, col)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2020-06-27 00:02:47 +08:00
|
|
|
// Next will return true if the next column is found.
|
2020-06-16 17:53:22 +08:00
|
|
|
func (cols *Cols) Next() bool {
|
|
|
|
cols.curCol++
|
2021-11-05 00:01:34 +08:00
|
|
|
return cols.curCol <= cols.totalCols
|
2020-06-16 17:53:22 +08:00
|
|
|
}
|
|
|
|
|
2020-06-27 00:02:47 +08:00
|
|
|
// Error will return an error when the error occurs.
|
2020-06-16 17:53:22 +08:00
|
|
|
func (cols *Cols) Error() error {
|
|
|
|
return cols.err
|
|
|
|
}
|
|
|
|
|
2020-06-22 00:05:19 +08:00
|
|
|
// Rows return the current column's row values.
|
2021-09-05 11:59:50 +08:00
|
|
|
func (cols *Cols) Rows(opts ...Options) ([]string, error) {
|
2022-11-12 00:02:11 +08:00
|
|
|
var rowIterator rowXMLIterator
|
2020-06-16 17:53:22 +08:00
|
|
|
if cols.stashCol >= cols.curCol {
|
2022-11-12 00:02:11 +08:00
|
|
|
return rowIterator.cells, rowIterator.err
|
2020-06-16 17:53:22 +08:00
|
|
|
}
|
2023-01-20 11:10:04 +08:00
|
|
|
cols.rawCellValue = getOptions(opts...).RawCellValue
|
2022-11-12 00:02:11 +08:00
|
|
|
if cols.sst, rowIterator.err = cols.f.sharedStringsReader(); rowIterator.err != nil {
|
|
|
|
return rowIterator.cells, rowIterator.err
|
|
|
|
}
|
2020-06-22 00:05:19 +08:00
|
|
|
decoder := cols.f.xmlNewDecoder(bytes.NewReader(cols.sheetXML))
|
|
|
|
for {
|
|
|
|
token, _ := decoder.Token()
|
|
|
|
if token == nil {
|
|
|
|
break
|
|
|
|
}
|
2021-02-05 22:52:31 +08:00
|
|
|
switch xmlElement := token.(type) {
|
2020-06-22 00:05:19 +08:00
|
|
|
case xml.StartElement:
|
2022-11-12 00:02:11 +08:00
|
|
|
rowIterator.inElement = xmlElement.Name.Local
|
|
|
|
if rowIterator.inElement == "row" {
|
|
|
|
rowIterator.cellCol = 0
|
|
|
|
rowIterator.cellRow++
|
2021-02-05 22:52:31 +08:00
|
|
|
attrR, _ := attrValToInt("r", xmlElement.Attr)
|
2020-11-18 22:08:40 +08:00
|
|
|
if attrR != 0 {
|
2022-11-12 00:02:11 +08:00
|
|
|
rowIterator.cellRow = attrR
|
2020-06-28 00:02:32 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-12 00:02:11 +08:00
|
|
|
if cols.rowXMLHandler(&rowIterator, &xmlElement, decoder); rowIterator.err != nil {
|
|
|
|
return rowIterator.cells, rowIterator.err
|
2020-06-22 00:05:19 +08:00
|
|
|
}
|
2021-02-05 22:52:31 +08:00
|
|
|
case xml.EndElement:
|
|
|
|
if xmlElement.Name.Local == "sheetData" {
|
2022-11-12 00:02:11 +08:00
|
|
|
return rowIterator.cells, rowIterator.err
|
2021-02-05 22:52:31 +08:00
|
|
|
}
|
2020-06-22 00:05:19 +08:00
|
|
|
}
|
2020-06-16 17:53:22 +08:00
|
|
|
}
|
2022-11-12 00:02:11 +08:00
|
|
|
return rowIterator.cells, rowIterator.err
|
2021-02-05 22:52:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// columnXMLIterator defined runtime use field for the worksheet column SAX parser.
|
|
|
|
type columnXMLIterator struct {
|
|
|
|
err error
|
|
|
|
cols Cols
|
|
|
|
cellCol, curRow, row int
|
|
|
|
}
|
|
|
|
|
|
|
|
// columnXMLHandler parse the column XML element of the worksheet.
|
|
|
|
func columnXMLHandler(colIterator *columnXMLIterator, xmlElement *xml.StartElement) {
|
|
|
|
colIterator.err = nil
|
|
|
|
inElement := xmlElement.Name.Local
|
|
|
|
if inElement == "row" {
|
|
|
|
colIterator.row++
|
|
|
|
for _, attr := range xmlElement.Attr {
|
|
|
|
if attr.Name.Local == "r" {
|
|
|
|
if colIterator.curRow, colIterator.err = strconv.Atoi(attr.Value); colIterator.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
colIterator.row = colIterator.curRow
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 00:01:34 +08:00
|
|
|
colIterator.cols.totalRows = colIterator.row
|
2021-02-05 22:52:31 +08:00
|
|
|
colIterator.cellCol = 0
|
|
|
|
}
|
|
|
|
if inElement == "c" {
|
|
|
|
colIterator.cellCol++
|
|
|
|
for _, attr := range xmlElement.Attr {
|
|
|
|
if attr.Name.Local == "r" {
|
|
|
|
if colIterator.cellCol, _, colIterator.err = CellNameToCoordinates(attr.Value); colIterator.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 00:01:34 +08:00
|
|
|
if colIterator.cellCol > colIterator.cols.totalCols {
|
|
|
|
colIterator.cols.totalCols = colIterator.cellCol
|
2021-02-05 22:52:31 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 17:53:22 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 00:02:11 +08:00
|
|
|
// rowXMLHandler parse the row XML element of the worksheet.
|
|
|
|
func (cols *Cols) rowXMLHandler(rowIterator *rowXMLIterator, xmlElement *xml.StartElement, decoder *xml.Decoder) {
|
|
|
|
if rowIterator.inElement == "c" {
|
|
|
|
rowIterator.cellCol++
|
|
|
|
for _, attr := range xmlElement.Attr {
|
|
|
|
if attr.Name.Local == "r" {
|
|
|
|
if rowIterator.cellCol, rowIterator.cellRow, rowIterator.err = CellNameToCoordinates(attr.Value); rowIterator.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blank := rowIterator.cellRow - len(rowIterator.cells)
|
|
|
|
for i := 1; i < blank; i++ {
|
|
|
|
rowIterator.cells = append(rowIterator.cells, "")
|
|
|
|
}
|
|
|
|
if rowIterator.cellCol == cols.curCol {
|
|
|
|
colCell := xlsxC{}
|
|
|
|
_ = decoder.DecodeElement(&colCell, xmlElement)
|
|
|
|
val, _ := colCell.getValueFrom(cols.f, cols.sst, cols.rawCellValue)
|
|
|
|
rowIterator.cells = append(rowIterator.cells, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 00:02:47 +08:00
|
|
|
// Cols returns a columns iterator, used for streaming reading data for a
|
2022-09-11 00:04:04 +08:00
|
|
|
// worksheet with a large data. This function is concurrency safe. For
|
|
|
|
// example:
|
2020-06-16 17:53:22 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// cols, err := f.Cols("Sheet1")
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// for cols.Next() {
|
|
|
|
// col, err := cols.Rows()
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// }
|
|
|
|
// for _, rowCell := range col {
|
|
|
|
// fmt.Print(rowCell, "\t")
|
|
|
|
// }
|
|
|
|
// fmt.Println()
|
|
|
|
// }
|
2020-06-16 17:53:22 +08:00
|
|
|
func (f *File) Cols(sheet string) (*Cols, error) {
|
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
|
|
|
if err := checkSheetName(sheet); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-18 00:21:34 +08:00
|
|
|
name, ok := f.getSheetXMLPath(sheet)
|
2020-06-16 17:53:22 +08:00
|
|
|
if !ok {
|
|
|
|
return nil, ErrSheetNotExist{sheet}
|
|
|
|
}
|
2023-04-24 00:02:13 +08:00
|
|
|
if worksheet, ok := f.Sheet.Load(name); ok && worksheet != nil {
|
|
|
|
ws := worksheet.(*xlsxWorksheet)
|
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
|
|
|
output, _ := xml.Marshal(ws)
|
2020-07-18 15:15:16 +08:00
|
|
|
f.saveFileList(name, f.replaceNameSpaceBytes(name, output))
|
2020-06-16 17:53:22 +08:00
|
|
|
}
|
2021-02-05 22:52:31 +08:00
|
|
|
var colIterator columnXMLIterator
|
2021-09-18 23:20:24 +08:00
|
|
|
colIterator.cols.sheetXML = f.readBytes(name)
|
2021-02-05 22:52:31 +08:00
|
|
|
decoder := f.xmlNewDecoder(bytes.NewReader(colIterator.cols.sheetXML))
|
2020-06-16 17:53:22 +08:00
|
|
|
for {
|
|
|
|
token, _ := decoder.Token()
|
|
|
|
if token == nil {
|
|
|
|
break
|
|
|
|
}
|
2021-02-05 22:52:31 +08:00
|
|
|
switch xmlElement := token.(type) {
|
2020-06-16 17:53:22 +08:00
|
|
|
case xml.StartElement:
|
2021-02-05 22:52:31 +08:00
|
|
|
columnXMLHandler(&colIterator, &xmlElement)
|
|
|
|
if colIterator.err != nil {
|
|
|
|
return &colIterator.cols, colIterator.err
|
2020-06-22 00:05:19 +08:00
|
|
|
}
|
2021-02-05 22:52:31 +08:00
|
|
|
case xml.EndElement:
|
|
|
|
if xmlElement.Name.Local == "sheetData" {
|
|
|
|
colIterator.cols.f = f
|
This closes #1425, breaking changes for sheet name (#1426)
- Checking and return error for invalid sheet name instead of trim invalid characters
- Add error return for the 4 functions: `DeleteSheet`, `GetSheetIndex`, `GetSheetVisible` and `SetSheetName`
- Export new error 4 constants: `ErrSheetNameBlank`, `ErrSheetNameInvalid`, `ErrSheetNameLength` and `ErrSheetNameSingleQuote`
- Rename exported error constant `ErrExistsWorksheet` to `ErrExistsSheet`
- Update unit tests for 90 functions: `AddChart`, `AddChartSheet`, `AddComment`, `AddDataValidation`, `AddPicture`, `AddPictureFromBytes`, `AddPivotTable`, `AddShape`, `AddSparkline`, `AddTable`, `AutoFilter`, `CalcCellValue`, `Cols`, `DeleteChart`, `DeleteComment`, `DeleteDataValidation`, `DeletePicture`, `DeleteSheet`, `DuplicateRow`, `DuplicateRowTo`, `GetCellFormula`, `GetCellHyperLink`, `GetCellRichText`, `GetCellStyle`, `GetCellType`, `GetCellValue`, `GetColOutlineLevel`, `GetCols`, `GetColStyle`, `GetColVisible`, `GetColWidth`, `GetConditionalFormats`, `GetDataValidations`, `GetMergeCells`, `GetPageLayout`, `GetPageMargins`, `GetPicture`, `GetRowHeight`, `GetRowOutlineLevel`, `GetRows`, `GetRowVisible`, `GetSheetIndex`, `GetSheetProps`, `GetSheetVisible`, `GroupSheets`, `InsertCol`, `InsertPageBreak`, `InsertRows`, `MergeCell`, `NewSheet`, `NewStreamWriter`, `ProtectSheet`, `RemoveCol`, `RemovePageBreak`, `RemoveRow`, `Rows`, `SearchSheet`, `SetCellBool`, `SetCellDefault`, `SetCellFloat`, `SetCellFormula`, `SetCellHyperLink`, `SetCellInt`, `SetCellRichText`, `SetCellStr`, `SetCellStyle`, `SetCellValue`, `SetColOutlineLevel`, `SetColStyle`, `SetColVisible`, `SetColWidth`, `SetConditionalFormat`, `SetHeaderFooter`, `SetPageLayout`, `SetPageMargins`, `SetPanes`, `SetRowHeight`, `SetRowOutlineLevel`, `SetRowStyle`, `SetRowVisible`, `SetSheetBackground`, `SetSheetBackgroundFromBytes`, `SetSheetCol`, `SetSheetName`, `SetSheetProps`, `SetSheetRow`, `SetSheetVisible`, `UnmergeCell`, `UnprotectSheet` and
`UnsetConditionalFormat`
- Update documentation of the set style functions
Co-authored-by: guoweikuang <weikuang.guo@shopee.com>
2022-12-23 00:54:40 +08:00
|
|
|
colIterator.cols.sheet = sheet
|
2021-02-05 22:52:31 +08:00
|
|
|
return &colIterator.cols, nil
|
2020-06-16 17:53:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 22:52:31 +08:00
|
|
|
return &colIterator.cols, nil
|
2020-06-16 17:53:22 +08:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:03:29 +08:00
|
|
|
// GetColVisible provides a function to get visible of a single column by given
|
2022-09-11 00:04:04 +08:00
|
|
|
// worksheet name and column name. This function is concurrency safe. For
|
|
|
|
// example, get visible state of column D in Sheet1:
|
2017-06-15 11:03:29 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// visible, err := f.GetColVisible("Sheet1", "D")
|
2019-03-23 20:08:06 +08:00
|
|
|
func (f *File) GetColVisible(sheet, col string) (bool, error) {
|
|
|
|
colNum, err := ColumnNameToNumber(col)
|
|
|
|
if err != nil {
|
2022-03-24 00:19:30 +08:00
|
|
|
return true, err
|
2019-03-23 20:08:06 +08:00
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Lock()
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-04-15 11:22:57 +08:00
|
|
|
if err != nil {
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Unlock()
|
2019-04-15 11:22:57 +08:00
|
|
|
return false, err
|
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Unlock()
|
2023-04-24 00:02:13 +08:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.Cols == nil {
|
2022-03-24 00:19:30 +08:00
|
|
|
return true, err
|
2017-06-15 11:03:29 +08:00
|
|
|
}
|
2022-03-24 00:19:30 +08:00
|
|
|
visible := true
|
2020-11-10 23:48:09 +08:00
|
|
|
for c := range ws.Cols.Col {
|
|
|
|
colData := &ws.Cols.Col[c]
|
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 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
|
2022-09-11 00:04:04 +08:00
|
|
|
// name, columns range and visibility. This function is concurrency safe.
|
2020-01-22 06:42:44 +08:00
|
|
|
//
|
|
|
|
// For example hide column D on Sheet1:
|
2017-06-15 11:03:29 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err := f.SetColVisible("Sheet1", "D", false)
|
2017-06-15 11:03:29 +08:00
|
|
|
//
|
2020-02-07 00:25:01 +08:00
|
|
|
// Hide the columns from D to F (included):
|
2020-01-22 06:42:44 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err := f.SetColVisible("Sheet1", "D:F", false)
|
2020-01-22 06:42:44 +08:00
|
|
|
func (f *File) SetColVisible(sheet, columns string, visible bool) error {
|
2022-11-11 01:50:07 +08:00
|
|
|
min, max, err := f.parseColRange(columns)
|
2019-03-23 20:08:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-04-15 11:22:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-24 00:02:13 +08:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2020-01-22 06:42:44 +08:00
|
|
|
colData := xlsxCol{
|
2022-11-11 01:50:07 +08:00
|
|
|
Min: min,
|
|
|
|
Max: max,
|
2023-02-02 22:02:32 +08:00
|
|
|
Width: float64Ptr(defaultColWidth),
|
2020-02-07 00:25:01 +08:00
|
|
|
Hidden: !visible,
|
2020-01-22 06:42:44 +08:00
|
|
|
CustomWidth: true,
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.Cols == nil {
|
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)
|
2020-11-10 23:48:09 +08:00
|
|
|
ws.Cols = &cols
|
2020-02-07 00:25:01 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
ws.Cols.Col = flatCols(colData, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
|
2020-02-07 00:25:01 +08:00
|
|
|
fc.BestFit = c.BestFit
|
|
|
|
fc.Collapsed = c.Collapsed
|
|
|
|
fc.CustomWidth = c.CustomWidth
|
|
|
|
fc.OutlineLevel = c.OutlineLevel
|
|
|
|
fc.Phonetic = c.Phonetic
|
|
|
|
fc.Style = c.Style
|
|
|
|
fc.Width = c.Width
|
|
|
|
return fc
|
|
|
|
})
|
2020-01-22 06:42:44 +08:00
|
|
|
return nil
|
2017-06-15 11:03:29 +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
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// level, err := f.GetColOutlineLevel("Sheet1", "D")
|
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
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-04-15 11:22:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.Cols == nil {
|
2019-03-23 20:08:06 +08:00
|
|
|
return level, err
|
2018-05-08 02:32:20 +08:00
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
for c := range ws.Cols.Col {
|
|
|
|
colData := &ws.Cols.Col[c]
|
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 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
|
|
|
}
|
|
|
|
|
2021-03-05 00:40:37 +08:00
|
|
|
// parseColRange parse and convert column range with column name to the column number.
|
2022-11-11 01:50:07 +08:00
|
|
|
func (f *File) parseColRange(columns string) (min, max int, err error) {
|
2021-03-05 00:40:37 +08:00
|
|
|
colsTab := strings.Split(columns, ":")
|
2022-11-11 01:50:07 +08:00
|
|
|
min, err = ColumnNameToNumber(colsTab[0])
|
2021-03-05 00:40:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-11-11 01:50:07 +08:00
|
|
|
max = min
|
2021-03-05 00:40:37 +08:00
|
|
|
if len(colsTab) == 2 {
|
2022-11-11 01:50:07 +08:00
|
|
|
if max, err = ColumnNameToNumber(colsTab[1]); err != nil {
|
2021-03-05 00:40:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 01:50:07 +08:00
|
|
|
if max < min {
|
|
|
|
min, max = max, min
|
2021-03-05 00:40:37 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err := f.SetColOutlineLevel("Sheet1", "D", 2)
|
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 {
|
2021-05-10 00:09:24 +08:00
|
|
|
return ErrOutlineLevel
|
2019-09-24 21:53:19 +08:00
|
|
|
}
|
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,
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-04-15 11:22:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.Cols == nil {
|
2018-05-08 02:32:20 +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)
|
2020-11-10 23:48:09 +08:00
|
|
|
ws.Cols = &cols
|
2019-03-23 20:08:06 +08:00
|
|
|
return err
|
2018-05-08 02:32:20 +08:00
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
ws.Cols.Col = flatCols(colData, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
|
2020-02-07 00:25:01 +08:00
|
|
|
fc.BestFit = c.BestFit
|
|
|
|
fc.Collapsed = c.Collapsed
|
|
|
|
fc.CustomWidth = c.CustomWidth
|
|
|
|
fc.Hidden = c.Hidden
|
|
|
|
fc.Phonetic = c.Phonetic
|
|
|
|
fc.Style = c.Style
|
|
|
|
fc.Width = c.Width
|
|
|
|
return fc
|
|
|
|
})
|
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
|
2022-09-11 00:04:04 +08:00
|
|
|
// name, columns range and style ID. This function is concurrency safe. Note
|
|
|
|
// that this will overwrite the existing styles for the columns, it won't
|
|
|
|
// append or merge style with existing styles.
|
2019-05-16 13:36:50 +08:00
|
|
|
//
|
|
|
|
// For example set style of column H on Sheet1:
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err = f.SetColStyle("Sheet1", "H", style)
|
2019-05-16 13:36:50 +08:00
|
|
|
//
|
|
|
|
// Set style of columns C:F on Sheet1:
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err = f.SetColStyle("Sheet1", "C:F", style)
|
2019-05-16 13:36:50 +08:00
|
|
|
func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
2022-11-11 01:50:07 +08:00
|
|
|
min, max, err := f.parseColRange(columns)
|
2019-05-16 13:36:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Lock()
|
2022-11-12 00:02:11 +08:00
|
|
|
s, err := f.stylesReader()
|
|
|
|
if err != nil {
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Unlock()
|
2022-11-12 00:02:11 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
f.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.mu.Unlock()
|
2023-04-24 00:02:13 +08:00
|
|
|
s.mu.Lock()
|
2022-09-01 00:41:52 +08:00
|
|
|
if styleID < 0 || s.CellXfs == nil || len(s.CellXfs.Xf) <= styleID {
|
2023-04-24 00:02:13 +08:00
|
|
|
s.mu.Unlock()
|
2022-09-01 00:41:52 +08:00
|
|
|
return newInvalidStyleID(styleID)
|
|
|
|
}
|
2023-04-24 00:02:13 +08:00
|
|
|
s.mu.Unlock()
|
|
|
|
ws.mu.Lock()
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.Cols == nil {
|
|
|
|
ws.Cols = &xlsxCols{}
|
2019-05-16 13:36:50 +08:00
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
ws.Cols.Col = flatCols(xlsxCol{
|
2022-11-11 01:50:07 +08:00
|
|
|
Min: min,
|
|
|
|
Max: max,
|
2023-02-02 22:02:32 +08:00
|
|
|
Width: float64Ptr(defaultColWidth),
|
2020-02-07 00:25:01 +08:00
|
|
|
Style: styleID,
|
2020-11-10 23:48:09 +08:00
|
|
|
}, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
|
2020-02-07 00:25:01 +08:00
|
|
|
fc.BestFit = c.BestFit
|
|
|
|
fc.Collapsed = c.Collapsed
|
|
|
|
fc.CustomWidth = c.CustomWidth
|
|
|
|
fc.Hidden = c.Hidden
|
|
|
|
fc.OutlineLevel = c.OutlineLevel
|
|
|
|
fc.Phonetic = c.Phonetic
|
|
|
|
fc.Width = c.Width
|
|
|
|
return fc
|
|
|
|
})
|
2023-04-24 00:02:13 +08:00
|
|
|
ws.mu.Unlock()
|
2021-07-08 00:52:07 +08:00
|
|
|
if rows := len(ws.SheetData.Row); rows > 0 {
|
2022-11-11 01:50:07 +08:00
|
|
|
for col := min; col <= max; col++ {
|
2021-07-08 00:52:07 +08:00
|
|
|
from, _ := CoordinatesToCellName(col, 1)
|
|
|
|
to, _ := CoordinatesToCellName(col, rows)
|
2021-07-29 00:03:57 +08:00
|
|
|
err = f.SetCellStyle(sheet, from, to, styleID)
|
2021-07-08 00:52:07 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-29 00:03:57 +08:00
|
|
|
return err
|
2019-05-16 13:36:50 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// SetColWidth provides a function to set the width of a single column or
|
2022-09-11 00:04:04 +08:00
|
|
|
// multiple columns. This function is concurrency safe. For example:
|
2017-01-18 14:47:23 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err := f.SetColWidth("Sheet1", "A", "H", 20)
|
2022-03-24 00:19:30 +08:00
|
|
|
func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error {
|
2022-11-11 01:50:07 +08:00
|
|
|
min, max, err := f.parseColRange(startCol + ":" + endCol)
|
2019-03-23 20:08:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-18 22:20:58 +08:00
|
|
|
if width > MaxColumnWidth {
|
2021-05-10 00:09:24 +08:00
|
|
|
return ErrColumnWidth
|
2020-09-18 22:20:58 +08:00
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Lock()
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-04-15 11:22:57 +08:00
|
|
|
if err != nil {
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Unlock()
|
2019-04-15 11:22:57 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Unlock()
|
2023-04-24 00:02:13 +08:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2017-01-18 14:47:23 +08:00
|
|
|
col := xlsxCol{
|
|
|
|
Min: min,
|
|
|
|
Max: max,
|
2023-02-02 22:02:32 +08:00
|
|
|
Width: float64Ptr(width),
|
2017-01-18 14:47:23 +08:00
|
|
|
CustomWidth: true,
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.Cols == nil {
|
2017-01-18 14:47:23 +08:00
|
|
|
cols := xlsxCols{}
|
|
|
|
cols.Col = append(cols.Col, col)
|
2020-11-10 23:48:09 +08:00
|
|
|
ws.Cols = &cols
|
2020-02-07 00:25:01 +08:00
|
|
|
return err
|
2017-01-18 14:47:23 +08:00
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
ws.Cols.Col = flatCols(col, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
|
2020-02-07 00:25:01 +08:00
|
|
|
fc.BestFit = c.BestFit
|
|
|
|
fc.Collapsed = c.Collapsed
|
|
|
|
fc.Hidden = c.Hidden
|
|
|
|
fc.OutlineLevel = c.OutlineLevel
|
|
|
|
fc.Phonetic = c.Phonetic
|
|
|
|
fc.Style = c.Style
|
|
|
|
return fc
|
|
|
|
})
|
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
|
|
|
|
2020-02-07 00:25:01 +08:00
|
|
|
// flatCols provides a method for the column's operation functions to flatten
|
|
|
|
// and check the worksheet columns.
|
|
|
|
func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol) []xlsxCol {
|
2022-03-24 00:19:30 +08:00
|
|
|
var fc []xlsxCol
|
2020-02-07 00:25:01 +08:00
|
|
|
for i := col.Min; i <= col.Max; i++ {
|
|
|
|
c := deepcopy.Copy(col).(xlsxCol)
|
|
|
|
c.Min, c.Max = i, i
|
|
|
|
fc = append(fc, c)
|
|
|
|
}
|
|
|
|
inFlat := func(colID int, cols []xlsxCol) (int, bool) {
|
|
|
|
for idx, c := range cols {
|
|
|
|
if c.Max == colID && c.Min == colID {
|
|
|
|
return idx, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, false
|
|
|
|
}
|
|
|
|
for _, column := range cols {
|
|
|
|
for i := column.Min; i <= column.Max; i++ {
|
|
|
|
if idx, ok := inFlat(i, fc); ok {
|
|
|
|
fc[idx] = replacer(fc[idx], column)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c := deepcopy.Copy(column).(xlsxCol)
|
|
|
|
c.Min, c.Max = i, i
|
|
|
|
fc = append(fc, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fc
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// +------------+------------+
|
|
|
|
// | A | B |
|
|
|
|
// +-----+------------+------------+
|
|
|
|
// | |(x1,y1) | |
|
|
|
|
// | 1 |(A1)._______|______ |
|
|
|
|
// | | | | |
|
|
|
|
// | | | | |
|
|
|
|
// +-----+----| OBJECT |-----+
|
|
|
|
// | | | | |
|
|
|
|
// | 2 | |______________. |
|
|
|
|
// | | | (B2)|
|
|
|
|
// | | | (x2,y2)|
|
|
|
|
// +-----+------------+------------+
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
2022-09-28 00:04:17 +08:00
|
|
|
// Example of an object that covers some range reference 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:
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// colStart, rowStart, colEnd, rowEnd, x1, y1, x2, y2.
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// colStart # Col containing upper left corner of object.
|
|
|
|
// x1 # Distance to left side of object.
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// rowStart # Row containing top left corner of object.
|
|
|
|
// y1 # Distance to top of object.
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// colEnd # Col containing lower right corner of object.
|
|
|
|
// x2 # Distance to right side of object.
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// rowEnd # Row containing bottom right corner of object.
|
|
|
|
// y2 # Distance to bottom of object.
|
2017-01-22 16:16:03 +08:00
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// width # Width of object frame.
|
|
|
|
// height # Height of object frame.
|
2020-10-18 00:01:33 +08:00
|
|
|
func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int) {
|
2017-01-22 16:16:03 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-01-27 22:37:32 +08:00
|
|
|
// Initialized end cell to the same as the start cell.
|
|
|
|
colEnd, rowEnd := col, 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.
|
2021-06-13 14:38:01 +08:00
|
|
|
for height >= f.getRowHeight(sheet, rowEnd+1) {
|
2019-04-04 16:26:26 +08:00
|
|
|
rowEnd++
|
2021-06-13 14:38:01 +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
|
2020-10-18 00:01:33 +08:00
|
|
|
return col, row, 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
|
2021-06-29 22:26:55 +08:00
|
|
|
// sheet name and column number.
|
2017-06-29 11:14:33 +08:00
|
|
|
func (f *File) getColWidth(sheet string, col int) int {
|
2021-08-13 01:32:44 +08:00
|
|
|
ws, _ := f.workSheetReader(sheet)
|
2023-04-24 00:02:13 +08:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2021-08-13 01:32:44 +08:00
|
|
|
if ws.Cols != nil {
|
2017-01-22 16:16:03 +08:00
|
|
|
var width float64
|
2021-08-13 01:32:44 +08:00
|
|
|
for _, v := range ws.Cols.Col {
|
2023-02-02 22:02:32 +08:00
|
|
|
if v.Min <= col && col <= v.Max && v.Width != nil {
|
|
|
|
width = *v.Width
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if width != 0 {
|
|
|
|
return int(convertColWidthToPixels(width))
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 22:37:32 +08:00
|
|
|
// Optimization 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
|
|
|
}
|
|
|
|
|
2022-09-07 00:18:16 +08:00
|
|
|
// GetColStyle provides a function to get column style ID by given worksheet
|
2022-09-11 00:04:04 +08:00
|
|
|
// name and column name. This function is concurrency safe.
|
2022-09-07 00:18:16 +08:00
|
|
|
func (f *File) GetColStyle(sheet, col string) (int, error) {
|
|
|
|
var styleID int
|
|
|
|
colNum, err := ColumnNameToNumber(col)
|
|
|
|
if err != nil {
|
|
|
|
return styleID, err
|
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Lock()
|
2022-09-07 00:18:16 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Unlock()
|
2022-09-07 00:18:16 +08:00
|
|
|
return styleID, err
|
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Unlock()
|
2023-04-24 00:02:13 +08:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2022-09-07 00:18:16 +08:00
|
|
|
if ws.Cols != nil {
|
|
|
|
for _, v := range ws.Cols.Col {
|
|
|
|
if v.Min <= colNum && colNum <= v.Max {
|
|
|
|
styleID = v.Style
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return styleID, err
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// GetColWidth provides a function to get column width by given worksheet name
|
2022-09-11 00:04:04 +08:00
|
|
|
// and column name. This function is concurrency safe.
|
2019-03-23 20:08:06 +08:00
|
|
|
func (f *File) GetColWidth(sheet, col string) (float64, error) {
|
|
|
|
colNum, err := ColumnNameToNumber(col)
|
|
|
|
if err != nil {
|
2021-03-05 00:40:37 +08:00
|
|
|
return defaultColWidth, err
|
2019-03-23 20:08:06 +08:00
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Lock()
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-04-15 11:22:57 +08:00
|
|
|
if err != nil {
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Unlock()
|
2021-03-05 00:40:37 +08:00
|
|
|
return defaultColWidth, err
|
2019-04-15 11:22:57 +08:00
|
|
|
}
|
2023-04-25 08:44:41 +08:00
|
|
|
f.mu.Unlock()
|
2023-04-24 00:02:13 +08:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2020-11-10 23:48:09 +08:00
|
|
|
if ws.Cols != nil {
|
2017-06-29 11:14:33 +08:00
|
|
|
var width float64
|
2020-11-10 23:48:09 +08:00
|
|
|
for _, v := range ws.Cols.Col {
|
2023-02-02 22:02:32 +08:00
|
|
|
if v.Min <= colNum && colNum <= v.Max && v.Width != nil {
|
|
|
|
width = *v.Width
|
2017-06-29 11:14:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if width != 0 {
|
2019-03-23 20:08:06 +08:00
|
|
|
return width, err
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 22:37:32 +08:00
|
|
|
// Optimization for when the column widths haven't changed.
|
2021-03-05 00:40:37 +08:00
|
|
|
return defaultColWidth, err
|
2017-01-22 16:16:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-31 00:02:48 +08:00
|
|
|
// InsertCols provides a function to insert new columns before the given column
|
|
|
|
// name and number of columns. For example, create two columns before column
|
|
|
|
// C in Sheet1:
|
2017-07-24 10:26:02 +08:00
|
|
|
//
|
2022-08-31 00:02:48 +08:00
|
|
|
// err := f.InsertCols("Sheet1", "C", 2)
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
func (f *File) InsertCols(sheet, col string, n int) 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
|
|
|
}
|
2022-08-31 00:02:48 +08:00
|
|
|
if n < 1 || n > MaxColumns {
|
|
|
|
return ErrColumnNumber
|
|
|
|
}
|
|
|
|
return f.adjustHelper(sheet, columns, num, n)
|
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
|
|
|
//
|
2022-08-13 11:21:59 +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
|
|
|
}
|
|
|
|
|
2020-11-10 23:48:09 +08:00
|
|
|
ws, err := f.workSheetReader(sheet)
|
2019-04-15 11:22:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
for rowIdx := range ws.SheetData.Row {
|
|
|
|
rowData := &ws.SheetData.Row[rowIdx]
|
2019-03-21 18:44:30 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-27 22:37:32 +08:00
|
|
|
// convertColWidthToPixels provides function to convert the width of a cell
|
2018-08-06 10:21:24 +08:00
|
|
|
// 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)
|
|
|
|
}
|