2019-07-25 19:43:46 +03:00
// Copyright 2017 FoxyUtils ehf. All rights reserved.
2017-08-28 20:56:18 -05:00
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
Functions2 (#348)
* MATCH, IFS, MAXA, MINA
* OFFSET fixed
* ISBLANK, ISERR, ISERROR, ISEVEN ,ISFORMULA, ISNONTEXT, ISNUMBER, ISODD, ISTEXT
* ISLEAPYEAR, ISLOGICAL, ISNA, ISREF
* FIND, FINDB
* SEARCH, SEARCHB
* CONCAT, CONCATENATE
* YEAR, YEARFRAC
* CONCAT is fixed, now TRUE and FALSE are concatenated instead of 1 and 0 in case of boolean results
* NOW, TODAY, TIME, TIMEVALUE
* DATE
* DATEDIF
2019-11-21 02:21:00 +03:00
// commercial license can be purchased on https://unidoc.io.
2017-08-28 20:56:18 -05:00
package spreadsheet
import (
2020-02-11 22:47:08 +03:00
"errors"
2017-09-06 14:53:48 -04:00
"fmt"
2017-10-01 19:16:11 -05:00
"log"
2017-09-07 12:07:59 -04:00
"sort"
2017-09-07 07:23:30 -04:00
"strings"
2017-09-06 14:53:48 -04:00
2019-05-04 11:18:06 +03:00
"github.com/unidoc/unioffice/spreadsheet/formula"
"github.com/unidoc/unioffice/spreadsheet/reference"
2020-02-11 22:47:08 +03:00
"github.com/unidoc/unioffice/spreadsheet/update"
2017-09-14 18:59:07 -05:00
2019-05-04 11:18:06 +03:00
"github.com/unidoc/unioffice"
"github.com/unidoc/unioffice/common"
"github.com/unidoc/unioffice/schema/soo/sml"
"github.com/unidoc/unioffice/vmldrawing"
2017-08-28 20:56:18 -05:00
)
// Sheet is a single sheet within a workbook.
type Sheet struct {
2017-09-06 22:00:32 -04:00
w * Workbook
cts * sml . CT_Sheet
x * sml . Worksheet
2017-08-28 20:56:18 -05:00
}
2020-03-25 16:40:25 +03:00
// MaxColumnIdx returns the max used column of the sheet.
func ( s Sheet ) MaxColumnIdx ( ) uint32 {
maxColumnIdx := uint32 ( 0 )
for _ , r := range s . Rows ( ) {
cells := r . x . C
if len ( cells ) > 0 {
lastCell := cells [ len ( cells ) - 1 ]
ref , _ := reference . ParseCellReference ( * lastCell . RAttr )
if maxColumnIdx < ref . ColumnIdx {
maxColumnIdx = ref . ColumnIdx
}
}
}
return maxColumnIdx
}
2017-09-28 17:05:38 -05:00
func ( s Sheet ) IsValid ( ) bool {
return s . x != nil
}
2017-09-07 14:57:04 -05:00
// X returns the inner wrapped XML type.
func ( s Sheet ) X ( ) * sml . Worksheet {
return s . x
}
2017-09-06 16:30:52 -04:00
// Row will return a row with a given row number, creating a new row if
2017-09-06 14:53:48 -04:00
// necessary.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) Row ( rowNum uint32 ) Row {
2017-09-06 14:53:48 -04:00
// see if the row exists
2017-09-06 22:00:32 -04:00
for _ , r := range s . x . SheetData . Row {
2017-09-06 14:53:48 -04:00
if r . RAttr != nil && * r . RAttr == rowNum {
2020-03-25 16:40:25 +03:00
return Row { s . w , s , r }
2017-09-06 14:53:48 -04:00
}
}
// create a new row
return s . AddNumberedRow ( rowNum )
}
2017-09-06 16:30:52 -04:00
// Cell creates or returns a cell given a cell reference of the form 'A10'
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) Cell ( cellRef string ) Cell {
2017-10-01 12:10:05 -05:00
cref , err := reference . ParseCellReference ( cellRef )
2017-09-06 16:30:52 -04:00
if err != nil {
2019-05-04 13:54:29 +00:00
unioffice . Log ( "error parsing cell reference: %s" , err )
2017-09-06 16:30:52 -04:00
return s . AddRow ( ) . AddCell ( )
}
2017-10-01 12:10:05 -05:00
return s . Row ( cref . RowIdx ) . Cell ( cref . Column )
2017-09-06 16:30:52 -04:00
}
2017-09-06 14:53:48 -04:00
// AddNumberedRow adds a row with a given row number. If you reuse a row number
// the resulting file will fail validation and fail to open in Office programs. Use
2017-09-06 16:30:52 -04:00
// Row instead which creates a new row or returns an existing row.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) AddNumberedRow ( rowNum uint32 ) Row {
2017-09-03 11:05:27 -05:00
r := sml . NewCT_Row ( )
2019-05-04 13:54:29 +00:00
r . RAttr = unioffice . Uint32 ( rowNum )
2017-09-06 22:00:32 -04:00
s . x . SheetData . Row = append ( s . x . SheetData . Row , r )
2017-09-07 12:07:59 -04:00
// Excel wants the rows to be sorted
sort . Slice ( s . x . SheetData . Row , func ( i , j int ) bool {
l := s . x . SheetData . Row [ i ] . RAttr
r := s . x . SheetData . Row [ j ] . RAttr
if l == nil {
return true
}
if r == nil {
return true
}
return * l < * r
} )
2020-03-25 16:40:25 +03:00
return Row { s . w , s , r }
2017-08-28 20:56:18 -05:00
}
2017-09-15 16:44:42 -05:00
// addNumberedRowFast is a fast path that can be used when adding consecutive
// rows and not skipping any.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) addNumberedRowFast ( rowNum uint32 ) Row {
2017-09-15 16:44:42 -05:00
r := sml . NewCT_Row ( )
2019-05-04 13:54:29 +00:00
r . RAttr = unioffice . Uint32 ( rowNum )
2017-09-15 16:44:42 -05:00
s . x . SheetData . Row = append ( s . x . SheetData . Row , r )
2020-03-25 16:40:25 +03:00
return Row { s . w , s , r }
2017-09-15 16:44:42 -05:00
}
2017-09-06 16:30:52 -04:00
// AddRow adds a new row to a sheet. You can mix this with numbered rows,
// however it will get confusing. You should prefer to use either automatically
// numbered rows with AddRow or manually numbered rows with Row/AddNumberedRow
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) AddRow ( ) Row {
2017-09-06 14:53:48 -04:00
maxRowID := uint32 ( 0 )
2017-09-15 16:44:42 -05:00
numRows := uint32 ( len ( s . x . SheetData . Row ) )
// fast path, adding consecutive rows
if numRows > 0 && s . x . SheetData . Row [ numRows - 1 ] . RAttr != nil && * s . x . SheetData . Row [ numRows - 1 ] . RAttr == numRows {
return s . addNumberedRowFast ( numRows + 1 )
}
2017-09-06 14:53:48 -04:00
// find the max row number
2017-09-06 22:00:32 -04:00
for _ , r := range s . x . SheetData . Row {
2017-09-06 14:53:48 -04:00
if r . RAttr != nil && * r . RAttr > maxRowID {
maxRowID = * r . RAttr
}
}
return s . AddNumberedRow ( maxRowID + 1 )
}
2018-09-27 11:37:48 -05:00
// InsertRow inserts a new row into a spreadsheet at a particular row number. This
// row will now be the row number specified, and any rows after it will be renumbed.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) InsertRow ( rowNum int ) Row {
2018-09-27 11:37:48 -05:00
rIdx := uint32 ( rowNum )
// Renumber every row after the row we're inserting
for _ , r := range s . Rows ( ) {
if r . x . RAttr != nil && * r . x . RAttr >= rIdx {
* r . x . RAttr ++
// and we also need to recompute cell references
for _ , c := range r . Cells ( ) {
cref , err := reference . ParseCellReference ( c . Reference ( ) )
if err != nil {
continue
}
cref . RowIdx ++
2019-05-04 13:54:29 +00:00
c . x . RAttr = unioffice . String ( cref . String ( ) )
2018-09-27 11:37:48 -05:00
}
}
}
2018-10-12 15:23:37 -05:00
// check for merged cells, and try to intelligently adjust them
// issue #212
for _ , mc := range s . MergedCells ( ) {
from , to , err := reference . ParseRangeReference ( mc . Reference ( ) )
if err != nil {
continue
}
if int ( from . RowIdx ) >= rowNum {
from . RowIdx ++
}
if int ( to . RowIdx ) >= rowNum {
to . RowIdx ++
}
ref := fmt . Sprintf ( "%s:%s" , from , to )
mc . SetReference ( ref )
}
2018-09-27 11:37:48 -05:00
// finally AddNumberedRow will add and re-sort rows
return s . AddNumberedRow ( rIdx )
}
2017-09-02 14:54:17 -05:00
// Name returns the sheet name
func ( s Sheet ) Name ( ) string {
2017-09-06 22:00:32 -04:00
return s . cts . NameAttr
2017-09-02 14:54:17 -05:00
}
// SetName sets the sheet name.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) SetName ( name string ) {
2017-09-06 22:00:32 -04:00
s . cts . NameAttr = name
2017-09-02 14:54:17 -05:00
}
2017-08-28 20:56:18 -05:00
// Validate validates the sheet, returning an error if it is found to be invalid.
func ( s Sheet ) Validate ( ) error {
2017-09-21 20:33:22 -05:00
validators := [ ] func ( ) error {
s . validateRowCellNumbers ,
s . validateMergedCells ,
s . validateSheetNames ,
}
for _ , v := range validators {
if err := v ( ) ; err != nil {
return err
}
}
2020-03-25 16:40:25 +03:00
if err := s . x . Validate ( ) ; err != nil {
2017-09-21 20:33:22 -05:00
return err
}
return s . x . Validate ( )
}
// validateSheetNames returns an error if any sheet names are too long
func ( s Sheet ) validateSheetNames ( ) error {
if len ( s . Name ( ) ) > 31 {
return fmt . Errorf ( "sheet name '%s' has %d characters, max length is 31" , s . Name ( ) , len ( s . Name ( ) ) )
}
return nil
}
2017-09-06 14:53:48 -04:00
2017-09-21 20:33:22 -05:00
// validateRowCellNumbers returns an error if any row numbers or cell numbers
// within a row are reused
func ( s Sheet ) validateRowCellNumbers ( ) error {
2017-09-07 15:39:35 -05:00
// check for re-used row numbers
2017-09-06 14:53:48 -04:00
usedRows := map [ uint32 ] struct { } { }
2017-09-06 22:00:32 -04:00
for _ , r := range s . x . SheetData . Row {
2017-09-06 14:53:48 -04:00
if r . RAttr != nil {
if _ , reusedRow := usedRows [ * r . RAttr ] ; reusedRow {
return fmt . Errorf ( "'%s' reused row %d" , s . Name ( ) , * r . RAttr )
}
usedRows [ * r . RAttr ] = struct { } { }
}
2017-09-07 15:39:35 -05:00
// or re-used column labels within a row
2017-09-06 14:53:48 -04:00
usedCells := map [ string ] struct { } { }
for _ , c := range r . C {
if c . RAttr == nil {
continue
}
2017-09-06 16:30:52 -04:00
2017-09-06 14:53:48 -04:00
if _ , reusedCell := usedCells [ * c . RAttr ] ; reusedCell {
return fmt . Errorf ( "'%s' reused cell %s" , s . Name ( ) , * c . RAttr )
}
usedCells [ * c . RAttr ] = struct { } { }
}
}
2017-09-21 20:33:22 -05:00
return nil
}
// validateMergedCells returns an error if merged cells overlap
func ( s Sheet ) validateMergedCells ( ) error {
mergedCells := map [ uint64 ] struct { } { }
for _ , mc := range s . MergedCells ( ) {
2017-10-01 12:10:05 -05:00
from , to , err := reference . ParseRangeReference ( mc . Reference ( ) )
2017-09-21 20:33:22 -05:00
if err != nil {
return fmt . Errorf ( "sheet name '%s' has invalid merged cell reference %s" , s . Name ( ) , mc . Reference ( ) )
}
2017-10-01 12:10:05 -05:00
for r := from . RowIdx ; r <= to . RowIdx ; r ++ {
for c := from . ColumnIdx ; c <= to . ColumnIdx ; c ++ {
2017-09-21 20:33:22 -05:00
idx := uint64 ( r ) << 32 | uint64 ( c )
if _ , ok := mergedCells [ idx ] ; ok {
return fmt . Errorf ( "sheet name '%s' has overlapping merged cell range" , s . Name ( ) )
}
mergedCells [ idx ] = struct { } { }
}
}
2017-09-07 15:39:35 -05:00
2017-09-06 14:53:48 -04:00
}
2017-09-21 20:33:22 -05:00
return nil
2017-08-28 20:56:18 -05:00
}
// ValidateWithPath validates the sheet passing path informaton for a better
// error message
func ( s Sheet ) ValidateWithPath ( path string ) error {
2020-03-25 16:40:25 +03:00
return s . x . ValidateWithPath ( path )
2017-08-28 20:56:18 -05:00
}
// Rows returns all of the rows in a sheet.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) Rows ( ) [ ] Row {
2017-08-28 20:56:18 -05:00
ret := [ ] Row { }
2017-09-06 22:00:32 -04:00
for _ , r := range s . x . SheetData . Row {
2020-03-25 16:40:25 +03:00
ret = append ( ret , Row { s . w , s , r } )
2017-08-28 20:56:18 -05:00
}
return ret
}
2017-09-03 18:39:35 -05:00
// SetDrawing sets the worksheet drawing. A worksheet can have a reference to a
// single drawing, but the drawing can have many charts.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) SetDrawing ( d Drawing ) {
2017-09-03 18:39:35 -05:00
var rel common . Relationships
for i , wks := range s . w . xws {
2017-09-06 22:00:32 -04:00
if wks == s . x {
2017-09-03 18:39:35 -05:00
rel = s . w . xwsRels [ i ]
break
}
}
2017-09-28 17:05:38 -05:00
2017-09-03 18:39:35 -05:00
// add relationship from drawing to the sheet
var drawingID string
for i , dr := range d . wb . drawings {
if dr == d . x {
2019-05-04 13:54:29 +00:00
rel := rel . AddAutoRelationship ( unioffice . DocTypeSpreadsheet , unioffice . WorksheetType , i + 1 , unioffice . DrawingType )
2017-09-03 18:39:35 -05:00
drawingID = rel . ID ( )
break
}
}
2017-09-06 22:00:32 -04:00
s . x . Drawing = sml . NewCT_Drawing ( )
s . x . Drawing . IdAttr = drawingID
2017-09-03 18:39:35 -05:00
}
2017-09-06 22:13:08 -04:00
// AddHyperlink adds a hyperlink to a sheet. Adding the hyperlink to the sheet
// and setting it on a cell is more efficient than setting hyperlinks directly
// on a cell.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) AddHyperlink ( url string ) common . Hyperlink {
2017-09-06 22:13:08 -04:00
// store the relationships so we don't need to do a lookup here?
for i , ws := range s . w . xws {
if ws == s . x {
// add a hyperlink relationship in the worksheet relationships file
return s . w . xwsRels [ i ] . AddHyperlink ( url )
}
}
// should never occur
return common . Hyperlink { }
}
2017-09-07 07:23:30 -04:00
// RangeReference converts a range reference of the form 'A1:A5' to 'Sheet
// 1'!$A$1:$A$5 . Renaming a sheet after calculating a range reference will
// invalidate the reference.
func ( s Sheet ) RangeReference ( n string ) string {
sp := strings . Split ( n , ":" )
2017-10-01 12:10:05 -05:00
cref , _ := reference . ParseCellReference ( sp [ 0 ] )
from := fmt . Sprintf ( "$%s$%d" , cref . Column , cref . RowIdx )
2017-09-07 07:23:30 -04:00
if len ( sp ) == 1 {
return fmt . Sprintf ( ` '%s'!%s ` , s . Name ( ) , from )
}
2017-10-01 12:10:05 -05:00
tref , _ := reference . ParseCellReference ( sp [ 1 ] )
to := fmt . Sprintf ( "$%s$%d" , tref . Column , tref . RowIdx )
2017-09-07 07:23:30 -04:00
return fmt . Sprintf ( ` '%s'!%s:%s ` , s . Name ( ) , from , to )
}
2017-09-07 14:57:04 -05:00
const autoFilterName = "_xlnm._FilterDatabase"
// ClearAutoFilter removes the autofilters from the sheet.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) ClearAutoFilter ( ) {
2017-09-07 14:57:04 -05:00
s . x . AutoFilter = nil
sn := "'" + s . Name ( ) + "'!"
// see if we have a defined auto filter name for the sheet
for _ , dn := range s . w . DefinedNames ( ) {
if dn . Name ( ) == autoFilterName {
if strings . HasPrefix ( dn . Content ( ) , sn ) {
s . w . RemoveDefinedName ( dn )
break
}
}
}
}
// SetAutoFilter creates autofilters on the sheet. These are the automatic
// filters that are common for a header row. The RangeRef should be of the form
// "A1:C5" and cover the entire range of cells to be filtered, not just the
// header. SetAutoFilter replaces any existing auto filter on the sheet.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) SetAutoFilter ( rangeRef string ) {
2017-09-07 14:57:04 -05:00
// this should have no $ in it
rangeRef = strings . Replace ( rangeRef , "$" , "" , - 1 )
s . x . AutoFilter = sml . NewCT_AutoFilter ( )
2019-05-04 13:54:29 +00:00
s . x . AutoFilter . RefAttr = unioffice . String ( rangeRef )
2017-09-07 14:57:04 -05:00
sn := "'" + s . Name ( ) + "'!"
var sdn DefinedName
// see if we already have a defined auto filter name for the sheet
for _ , dn := range s . w . DefinedNames ( ) {
if dn . Name ( ) == autoFilterName {
if strings . HasPrefix ( dn . Content ( ) , sn ) {
sdn = dn
// name must match, but make sure rangeRef matches as well
sdn . SetContent ( s . RangeReference ( rangeRef ) )
break
}
}
}
// no existing name found, so add a new one
if sdn . X ( ) == nil {
sdn = s . w . AddDefinedName ( autoFilterName , s . RangeReference ( rangeRef ) )
}
for i , ws := range s . w . xws {
if ws == s . x {
sdn . SetLocalSheetID ( uint32 ( i ) )
}
}
}
2017-09-07 17:13:03 -05:00
// AddMergedCells merges cells within a sheet.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) AddMergedCells ( fromRef , toRef string ) MergedCell {
2017-09-07 17:13:03 -05:00
// TODO: we might need to actually create the merged cells if they don't
// exist, but it appears to work fine on both Excel and LibreOffice just
// creating the merged region
if s . x . MergeCells == nil {
s . x . MergeCells = sml . NewCT_MergeCells ( )
}
merge := sml . NewCT_MergeCell ( )
merge . RefAttr = fmt . Sprintf ( "%s:%s" , fromRef , toRef )
s . x . MergeCells . MergeCell = append ( s . x . MergeCells . MergeCell , merge )
2019-05-04 13:54:29 +00:00
s . x . MergeCells . CountAttr = unioffice . Uint32 ( uint32 ( len ( s . x . MergeCells . MergeCell ) ) )
2020-03-25 16:40:25 +03:00
return MergedCell { s . w , s , merge }
2017-09-07 17:13:03 -05:00
}
// MergedCells returns the merged cell regions within the sheet.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) MergedCells ( ) [ ] MergedCell {
2017-09-07 17:13:03 -05:00
if s . x . MergeCells == nil {
return nil
}
ret := [ ] MergedCell { }
for _ , c := range s . x . MergeCells . MergeCell {
2020-03-25 16:40:25 +03:00
ret = append ( ret , MergedCell { s . w , s , c } )
2017-09-07 17:13:03 -05:00
}
return ret
}
// RemoveMergedCell removes merging from a cell range within a sheet. The cells
// that made up the merged cell remain, but are no lon merged.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) RemoveMergedCell ( mc MergedCell ) {
2017-09-07 17:13:03 -05:00
for i , c := range s . x . MergeCells . MergeCell {
if c == mc . X ( ) {
copy ( s . x . MergeCells . MergeCell [ i : ] , s . x . MergeCells . MergeCell [ i + 1 : ] )
s . x . MergeCells . MergeCell [ len ( s . x . MergeCells . MergeCell ) - 1 ] = nil
s . x . MergeCells . MergeCell = s . x . MergeCells . MergeCell [ : len ( s . x . MergeCells . MergeCell ) - 1 ]
}
}
}
2017-09-09 07:28:49 -05:00
2017-09-18 22:36:53 -04:00
func ( s Sheet ) ExtentsIndex ( ) ( string , uint32 , string , uint32 ) {
2017-09-09 07:28:49 -05:00
var minRow , maxRow , minCol , maxCol uint32 = 1 , 1 , 0 , 0
for _ , r := range s . Rows ( ) {
if r . RowNumber ( ) < minRow {
minRow = r . RowNumber ( )
} else if r . RowNumber ( ) > maxRow {
maxRow = r . RowNumber ( )
}
for _ , c := range r . Cells ( ) {
2017-10-01 12:10:05 -05:00
cref , err := reference . ParseCellReference ( c . Reference ( ) )
2017-09-09 07:28:49 -05:00
if err == nil {
// column index is zero based here
2017-10-01 12:10:05 -05:00
if cref . ColumnIdx < minCol {
minCol = cref . ColumnIdx
} else if cref . ColumnIdx > maxCol {
maxCol = cref . ColumnIdx
2017-09-09 07:28:49 -05:00
}
}
}
}
2017-10-01 11:32:03 -05:00
return reference . IndexToColumn ( minCol ) , minRow , reference . IndexToColumn ( maxCol ) , maxRow
2017-09-18 22:36:53 -04:00
}
2017-09-09 07:28:49 -05:00
2017-09-18 22:36:53 -04:00
// Extents returns the sheet extents in the form "A1:B15". This requires
// scanning the entire sheet.
func ( s Sheet ) Extents ( ) string {
sc , sr , ec , er := s . ExtentsIndex ( )
return fmt . Sprintf ( "%s%d:%s%d" , sc , sr , ec , er )
2017-09-09 07:28:49 -05:00
}
2017-09-09 09:25:34 -05:00
2017-09-09 10:43:24 -05:00
// AddConditionalFormatting adds conditional formatting to the sheet.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) AddConditionalFormatting ( cellRanges [ ] string ) ConditionalFormatting {
2017-09-09 09:25:34 -05:00
cfmt := sml . NewCT_ConditionalFormatting ( )
2017-09-09 10:43:24 -05:00
s . x . ConditionalFormatting = append ( s . x . ConditionalFormatting , cfmt )
2017-09-09 09:25:34 -05:00
// TODO: fix generator so this is not a pointer to a slice
slc := make ( sml . ST_Sqref , 0 , 0 )
cfmt . SqrefAttr = & slc
for _ , r := range cellRanges {
* cfmt . SqrefAttr = append ( * cfmt . SqrefAttr , r )
}
return ConditionalFormatting { cfmt }
}
2017-09-09 10:43:24 -05:00
// Column returns or creates a column that with a given index (1-N). Columns
// can span multiple column indices, this method will return the column that
// applies to a column index if it exists or create a new column that only
// applies to the index passed in otherwise.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) Column ( idx uint32 ) Column {
2017-09-09 10:43:24 -05:00
// scan for any existing column that covers this index
for _ , colSet := range s . x . Cols {
for _ , col := range colSet . Col {
if idx >= col . MinAttr && idx <= col . MaxAttr {
return Column { col }
}
}
}
// does a column set exist?
var colSet * sml . CT_Cols
if len ( s . x . Cols ) == 0 {
colSet = sml . NewCT_Cols ( )
s . x . Cols = append ( s . x . Cols , colSet )
} else {
colSet = s . x . Cols [ 0 ]
}
// create our new column
col := sml . NewCT_Col ( )
col . MinAttr = idx
col . MaxAttr = idx
colSet . Col = append ( colSet . Col , col )
return Column { col }
}
2017-09-08 21:25:01 -05:00
// Comments returns the comments for a sheet.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) Comments ( ) Comments {
2017-09-08 21:25:01 -05:00
for i , wks := range s . w . xws {
if wks == s . x {
if s . w . comments [ i ] == nil {
s . w . comments [ i ] = sml . NewComments ( )
2019-05-04 13:54:29 +00:00
s . w . xwsRels [ i ] . AddAutoRelationship ( unioffice . DocTypeSpreadsheet , unioffice . WorksheetType , i + 1 , unioffice . CommentsType )
s . w . ContentTypes . AddOverride ( unioffice . AbsoluteFilename ( unioffice . DocTypeSpreadsheet , unioffice . CommentsType , i + 1 ) , unioffice . CommentsContentType )
2017-09-08 21:25:01 -05:00
}
if len ( s . w . vmlDrawings ) == 0 {
s . w . vmlDrawings = append ( s . w . vmlDrawings , vmldrawing . NewCommentDrawing ( ) )
2019-05-04 13:54:29 +00:00
vmlID := s . w . xwsRels [ i ] . AddAutoRelationship ( unioffice . DocTypeSpreadsheet , unioffice . WorksheetType , 1 , unioffice . VMLDrawingType )
2017-09-08 21:25:01 -05:00
if s . x . LegacyDrawing == nil {
s . x . LegacyDrawing = sml . NewCT_LegacyDrawing ( )
}
s . x . LegacyDrawing . IdAttr = vmlID . ID ( )
}
return Comments { s . w , s . w . comments [ i ] }
}
}
2019-05-04 13:54:29 +00:00
unioffice . Log ( "attempted to access comments for non-existent sheet" )
2017-09-08 21:25:01 -05:00
// should never occur
return Comments { }
}
2017-09-10 16:06:03 -05:00
// SetBorder is a helper function for creating borders across multiple cells. In
// the OOXML spreadsheet format, a border applies to a single cell. To draw a
// 'boxed' border around multiple cells, you need to apply different styles to
// the cells on the top,left,right,bottom and four corners. This function
// breaks apart a single border into its components and applies it to cells as
// needed to give the effect of a border applying to multiple cells.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) SetBorder ( cellRange string , border Border ) error {
2017-10-01 12:10:05 -05:00
from , to , err := reference . ParseRangeReference ( cellRange )
2017-09-21 20:37:14 -05:00
if err != nil {
return err
}
2017-09-10 16:06:03 -05:00
topLeftStyle := s . w . StyleSheet . AddCellStyle ( )
topLeftBorder := s . w . StyleSheet . AddBorder ( )
topLeftStyle . SetBorder ( topLeftBorder )
topLeftBorder . x . Top = border . x . Top
topLeftBorder . x . Left = border . x . Left
topRightStyle := s . w . StyleSheet . AddCellStyle ( )
topRightBorder := s . w . StyleSheet . AddBorder ( )
topRightStyle . SetBorder ( topRightBorder )
topRightBorder . x . Top = border . x . Top
topRightBorder . x . Right = border . x . Right
topStyle := s . w . StyleSheet . AddCellStyle ( )
topBorder := s . w . StyleSheet . AddBorder ( )
topStyle . SetBorder ( topBorder )
topBorder . x . Top = border . x . Top
leftStyle := s . w . StyleSheet . AddCellStyle ( )
leftBorder := s . w . StyleSheet . AddBorder ( )
leftStyle . SetBorder ( leftBorder )
leftBorder . x . Left = border . x . Left
rightStyle := s . w . StyleSheet . AddCellStyle ( )
rightBorder := s . w . StyleSheet . AddBorder ( )
rightStyle . SetBorder ( rightBorder )
rightBorder . x . Right = border . x . Right
bottomStyle := s . w . StyleSheet . AddCellStyle ( )
bottomBorder := s . w . StyleSheet . AddBorder ( )
bottomStyle . SetBorder ( bottomBorder )
bottomBorder . x . Bottom = border . x . Bottom
bottomLeftStyle := s . w . StyleSheet . AddCellStyle ( )
bottomLeftBorder := s . w . StyleSheet . AddBorder ( )
bottomLeftStyle . SetBorder ( bottomLeftBorder )
bottomLeftBorder . x . Bottom = border . x . Bottom
bottomLeftBorder . x . Left = border . x . Left
bottomRightStyle := s . w . StyleSheet . AddCellStyle ( )
bottomRightBorder := s . w . StyleSheet . AddBorder ( )
bottomRightStyle . SetBorder ( bottomRightBorder )
bottomRightBorder . x . Bottom = border . x . Bottom
bottomRightBorder . x . Right = border . x . Right
2017-10-01 12:10:05 -05:00
tlRowIdx := from . RowIdx
tlColIdx := from . ColumnIdx
brRowIdx := to . RowIdx
brColIdx := to . ColumnIdx
2017-09-10 16:06:03 -05:00
for row := tlRowIdx ; row <= brRowIdx ; row ++ {
for col := tlColIdx ; col <= brColIdx ; col ++ {
2017-10-01 11:32:03 -05:00
ref := fmt . Sprintf ( "%s%d" , reference . IndexToColumn ( col ) , row )
2017-09-10 16:06:03 -05:00
switch {
// top corners
case row == tlRowIdx && col == tlColIdx :
s . Cell ( ref ) . SetStyle ( topLeftStyle )
case row == tlRowIdx && col == brColIdx :
s . Cell ( ref ) . SetStyle ( topRightStyle )
// bottom corners
case row == brRowIdx && col == tlColIdx :
s . Cell ( ref ) . SetStyle ( bottomLeftStyle )
case row == brRowIdx && col == brColIdx :
s . Cell ( ref ) . SetStyle ( bottomRightStyle )
// four sides that aren't the corners
case row == tlRowIdx :
s . Cell ( ref ) . SetStyle ( topStyle )
case row == brRowIdx :
s . Cell ( ref ) . SetStyle ( bottomStyle )
case col == tlColIdx :
s . Cell ( ref ) . SetStyle ( leftStyle )
case col == brColIdx :
s . Cell ( ref ) . SetStyle ( rightStyle )
}
}
}
2017-09-21 20:37:14 -05:00
return nil
2017-09-10 16:06:03 -05:00
}
2017-09-10 17:05:42 -05:00
// AddDataValidation adds a data validation rule to a sheet.
2020-03-25 16:40:25 +03:00
func ( s * Sheet ) AddDataValidation ( ) DataValidation {
2017-09-10 17:05:42 -05:00
if s . x . DataValidations == nil {
s . x . DataValidations = sml . NewCT_DataValidations ( )
}
dv := sml . NewCT_DataValidation ( )
2019-05-04 13:54:29 +00:00
dv . ShowErrorMessageAttr = unioffice . Bool ( true )
2017-09-10 17:05:42 -05:00
s . x . DataValidations . DataValidation = append ( s . x . DataValidations . DataValidation , dv )
2019-05-04 13:54:29 +00:00
s . x . DataValidations . CountAttr = unioffice . Uint32 ( uint32 ( len ( s . x . DataValidations . DataValidation ) ) )
2017-09-10 17:05:42 -05:00
return DataValidation { dv }
}
2017-09-10 17:15:44 -05:00
// ClearCachedFormulaResults clears any computed formula values that are stored
// in the sheet. This may be required if you modify cells that are used as a
// formula input to force the formulas to be recomputed the next time the sheet
// is opened in Excel.
func ( s * Sheet ) ClearCachedFormulaResults ( ) {
for _ , r := range s . Rows ( ) {
for _ , c := range r . Cells ( ) {
if c . X ( ) . F != nil {
c . X ( ) . V = nil
}
}
}
}
2017-09-10 18:58:04 -05:00
2017-09-17 14:49:25 -05:00
// RecalculateFormulas re-computes any computed formula values that are stored
// in the sheet. As gooxml formula support is still new and not all functins are
// supported, if formula execution fails either due to a parse error or missing
// function, or erorr in the result (even if expected) the cached value will be
// left empty allowing Excel to recompute it on load.
func ( s * Sheet ) RecalculateFormulas ( ) {
ev := formula . NewEvaluator ( )
ctx := s . FormulaContext ( )
for _ , r := range s . Rows ( ) {
for _ , c := range r . Cells ( ) {
if c . X ( ) . F != nil {
formStr := c . X ( ) . F . Content
2017-10-01 19:16:11 -05:00
// if the formula is shared, but the content is empty, then it's
// a cell that a shared formula is applied to so we can ignreo
// for now. The value will be calculated later and applied with
// setShared
if c . X ( ) . F . TAttr == sml . ST_CellFormulaTypeShared && len ( formStr ) == 0 {
continue
}
2017-09-17 14:49:25 -05:00
res := ev . Eval ( ctx , formStr ) . AsString ( )
if res . Type == formula . ResultTypeError {
2019-05-04 13:54:29 +00:00
unioffice . Log ( "error evaulating formula %s: %s" , formStr , res . ErrorMessage )
2017-09-17 14:49:25 -05:00
c . X ( ) . V = nil
} else {
if res . Type == formula . ResultTypeNumber {
c . X ( ) . TAttr = sml . ST_CellTypeN
} else {
c . X ( ) . TAttr = sml . ST_CellTypeInlineStr
}
2019-05-04 13:54:29 +00:00
c . X ( ) . V = unioffice . String ( res . Value ( ) )
2017-09-21 19:11:49 -05:00
// the formula is of type array, so if the result is also an
// array we need to expand the array out into cells
2020-01-29 13:43:43 +03:00
if c . X ( ) . F . TAttr == sml . ST_CellFormulaTypeArray {
if res . Type == formula . ResultTypeArray {
s . setArray ( c . Reference ( ) , res )
} else if res . Type == formula . ResultTypeList {
s . setList ( c . Reference ( ) , res )
}
2017-10-01 19:16:11 -05:00
} else if c . X ( ) . F . TAttr == sml . ST_CellFormulaTypeShared && c . X ( ) . F . RefAttr != nil {
from , to , err := reference . ParseRangeReference ( * c . X ( ) . F . RefAttr )
if err != nil {
log . Printf ( "error in shared formula reference: %s" , err )
continue
}
s . setShared ( c . Reference ( ) , from , to , formStr )
2017-09-21 19:11:49 -05:00
}
2017-09-17 14:49:25 -05:00
}
}
}
}
}
2017-10-01 19:16:11 -05:00
func ( s * Sheet ) setShared ( origin string , from , to reference . CellReference , formulaStr string ) {
ctx := s . FormulaContext ( )
ev := formula . NewEvaluator ( )
for r := from . RowIdx ; r <= to . RowIdx ; r ++ {
for c := from . ColumnIdx ; c <= to . ColumnIdx ; c ++ {
rowOffset := r - from . RowIdx
colOffset := c - from . ColumnIdx
// add an offset on the contex so that cell references will be
// offset during formula evaluation
ctx . SetOffset ( colOffset , rowOffset )
res := ev . Eval ( ctx , formulaStr )
cr := fmt . Sprintf ( "%s%d" , reference . IndexToColumn ( c ) , r )
c := s . Cell ( cr )
if res . Type == formula . ResultTypeNumber {
c . X ( ) . TAttr = sml . ST_CellTypeN
} else {
c . X ( ) . TAttr = sml . ST_CellTypeInlineStr
}
2019-05-04 13:54:29 +00:00
c . X ( ) . V = unioffice . String ( res . Value ( ) )
2017-10-01 19:16:11 -05:00
}
}
_ = ev
_ = ctx
}
2017-09-21 19:11:49 -05:00
// setArray expands an array into cached values starting at the origin which
// should be a cell reference of the type "A1". This is used when evaluating
// array type formulas.
2017-10-01 12:10:05 -05:00
func ( s * Sheet ) setArray ( origin string , arr formula . Result ) error {
cref , err := reference . ParseCellReference ( origin )
if err != nil {
return err
}
2017-09-21 19:11:49 -05:00
for ir , row := range arr . ValueArray {
2017-10-01 12:10:05 -05:00
sr := s . Row ( cref . RowIdx + uint32 ( ir ) )
2017-09-21 19:11:49 -05:00
for ic , val := range row {
2017-10-01 12:10:05 -05:00
cell := sr . Cell ( reference . IndexToColumn ( cref . ColumnIdx + uint32 ( ic ) ) )
2020-03-25 16:40:25 +03:00
if val . Type != formula . ResultTypeEmpty {
2020-01-29 13:43:43 +03:00
if val . IsBoolean {
cell . SetBool ( val . ValueNumber != 0 )
} else {
cell . SetCachedFormulaResult ( val . String ( ) )
}
}
}
}
return nil
}
// setList expands an array into cached values starting at the origin which
// should be a cell reference of the type "A1". This is used when evaluating
// list type formulas.
func ( s * Sheet ) setList ( origin string , list formula . Result ) error {
cref , err := reference . ParseCellReference ( origin )
if err != nil {
return err
}
sr := s . Row ( cref . RowIdx )
for ic , val := range list . ValueList {
cell := sr . Cell ( reference . IndexToColumn ( cref . ColumnIdx + uint32 ( ic ) ) )
2020-03-25 16:40:25 +03:00
if val . Type != formula . ResultTypeEmpty {
2020-01-29 13:43:43 +03:00
if val . IsBoolean {
cell . SetBool ( val . ValueNumber != 0 )
} else {
2019-12-05 16:03:20 +03:00
cell . SetCachedFormulaResult ( val . String ( ) )
}
2017-09-21 19:11:49 -05:00
}
}
2017-10-01 12:10:05 -05:00
return nil
2017-09-21 19:11:49 -05:00
}
2017-09-10 18:58:04 -05:00
// SheetViews returns the sheet views defined. This is where splits and frozen
// rows/cols are configured. Multiple sheet views are allowed, but I'm not
// aware of there being a use for more than a single sheet view.
func ( s * Sheet ) SheetViews ( ) [ ] SheetView {
if s . x . SheetViews == nil {
return nil
}
r := [ ] SheetView { }
for _ , sv := range s . x . SheetViews . SheetView {
r = append ( r , SheetView { sv } )
}
return r
}
// AddView adds a sheet view.
func ( s * Sheet ) AddView ( ) SheetView {
if s . x . SheetViews == nil {
s . x . SheetViews = sml . NewCT_SheetViews ( )
}
sv := sml . NewCT_SheetView ( )
s . x . SheetViews . SheetView = append ( s . x . SheetViews . SheetView , sv )
return SheetView { sv }
}
// ClearSheetViews clears the list of sheet views. This will clear the results
// of AddView() or SetFrozen.
func ( s * Sheet ) ClearSheetViews ( ) {
s . x . SheetViews = nil
}
// InitialView returns the first defined sheet view. If there are no views, one
// is created and returned.
func ( s * Sheet ) InitialView ( ) SheetView {
if s . x . SheetViews == nil || len ( s . x . SheetViews . SheetView ) == 0 {
return s . AddView ( )
}
return SheetView { s . x . SheetViews . SheetView [ 0 ] }
}
// SetFrozen removes any existing sheet views and creates a new single view with
// either the first row, first column or both frozen.
func ( s * Sheet ) SetFrozen ( firstRow , firstCol bool ) {
s . x . SheetViews = nil
v := s . AddView ( )
v . SetState ( sml . ST_PaneStateFrozen )
switch {
case firstRow && firstCol :
v . SetYSplit ( 1 )
v . SetXSplit ( 1 )
v . SetTopLeft ( "B2" )
case firstRow :
v . SetYSplit ( 1 )
v . SetTopLeft ( "A2" )
case firstCol :
v . SetXSplit ( 1 )
v . SetTopLeft ( "B1" )
}
}
2017-09-14 18:59:07 -05:00
2017-09-17 12:58:51 -05:00
// FormulaContext returns a formula evaluation context that can be used to
// evaluate formaulas.
2017-09-14 18:59:07 -05:00
func ( s * Sheet ) FormulaContext ( ) formula . Context {
return newEvalContext ( s )
}
2017-09-20 11:27:19 -04:00
// ClearProtection removes any protections applied to teh sheet.
func ( s * Sheet ) ClearProtection ( ) {
s . x . SheetProtection = nil
}
// Protection controls the protection on an individual sheet.
func ( s * Sheet ) Protection ( ) SheetProtection {
if s . x . SheetProtection == nil {
s . x . SheetProtection = sml . NewCT_SheetProtection ( )
}
return SheetProtection { s . x . SheetProtection }
}
2017-09-27 18:08:59 -05:00
// Sort sorts all of the rows within a sheet by the contents of a column. As the
// file format doesn't suppot indicating that a column should be sorted by the
// viewing/editing program, we actually need to reorder rows and change cell
// references during a sort. If the sheet contains formulas, you should call
// RecalculateFormulas() prior to sorting. The column is in the form "C" and
// specifies the column to sort by. The firstRow is a 1-based index and
// specifies the firstRow to include in the sort, allowing skipping over a
// header row.
func ( s * Sheet ) Sort ( column string , firstRow uint32 , order SortOrder ) {
sheetData := s . x . SheetData . Row
rows := s . Rows ( )
// figure out which row to start the sort at
for i , r := range rows {
if r . RowNumber ( ) == firstRow {
sheetData = s . x . SheetData . Row [ i : ]
break
}
}
// perform the sort
cmp := Comparer { Order : order }
sort . Slice ( sheetData , func ( i , j int ) bool {
return cmp . LessRows ( column ,
2020-03-25 16:40:25 +03:00
Row { s . w , s , sheetData [ i ] } ,
Row { s . w , s , sheetData [ j ] } )
2017-09-27 18:08:59 -05:00
} )
// since we probably moved some rows, we need to go and fix up their row
// number and cell references
for i , r := range s . Rows ( ) {
correctRowNumber := uint32 ( i + 1 )
if r . RowNumber ( ) != correctRowNumber {
r . renumberAs ( correctRowNumber )
}
}
}
2020-02-11 22:47:08 +03:00
// RemoveColumn removes column from the sheet and moves all columns to the right of the removed column one step left.
func ( s * Sheet ) RemoveColumn ( column string ) error {
cellsInFormulaArrays , err := s . getAllCellsInFormulaArraysForColumn ( )
if err != nil {
return err
}
columnIdx := reference . ColumnToIndex ( column )
for _ , row := range s . Rows ( ) {
ref := fmt . Sprintf ( "%s%d" , column , * row . X ( ) . RAttr )
if _ , ok := cellsInFormulaArrays [ ref ] ; ok {
return nil
}
}
for _ , row := range s . Rows ( ) {
cells := row . x . C
for ic , cell := range cells {
ref , err := reference . ParseCellReference ( * cell . RAttr )
if err != nil {
return err
}
if ref . ColumnIdx == columnIdx {
row . x . C = append ( cells [ : ic ] , s . slideCellsLeft ( cells [ ic + 1 : ] ) ... )
break
} else if ref . ColumnIdx > columnIdx {
row . x . C = append ( cells [ : ic ] , s . slideCellsLeft ( cells [ ic : ] ) ... )
break
}
}
}
err = s . updateAfterRemove ( columnIdx , update . UpdateActionRemoveColumn )
if err != nil {
return err
}
err = s . removeColumnFromNamedRanges ( columnIdx )
if err != nil {
return err
}
err = s . removeColumnFromMergedCells ( columnIdx )
if err != nil {
return err
}
for _ , sheet := range s . w . Sheets ( ) {
sheet . RecalculateFormulas ( )
}
return nil
}
func ( s * Sheet ) updateAfterRemove ( columnIdx uint32 , updateType update . UpdateAction ) error {
ownSheetName := s . Name ( )
q := & update . UpdateQuery {
2020-03-25 16:40:25 +03:00
UpdateType : updateType ,
ColumnIdx : columnIdx ,
2020-02-11 22:47:08 +03:00
SheetToUpdate : ownSheetName ,
}
for _ , sheet := range s . w . Sheets ( ) {
q . UpdateCurrentSheet = ownSheetName == sheet . Name ( )
for _ , r := range sheet . Rows ( ) {
for _ , c := range r . Cells ( ) {
if c . X ( ) . F != nil {
formStr := c . X ( ) . F . Content
expr := formula . ParseString ( formStr )
if expr == nil {
c . SetError ( "#REF!" )
} else {
newExpr := expr . Update ( q )
c . X ( ) . F . Content = fmt . Sprintf ( "=%s" , newExpr . String ( ) )
}
}
}
}
}
return nil
}
func ( s * Sheet ) slideCellsLeft ( cells [ ] * sml . CT_Cell ) [ ] * sml . CT_Cell {
for _ , cell := range cells {
ref , err := reference . ParseCellReference ( * cell . RAttr )
if err != nil {
return cells
}
newColumnIdx := ref . ColumnIdx - 1
newRefStr := reference . IndexToColumn ( newColumnIdx ) + fmt . Sprintf ( "%d" , ref . RowIdx )
cell . RAttr = & newRefStr
}
return cells
}
func ( s * Sheet ) removeColumnFromMergedCells ( columnIdx uint32 ) error {
if s . x . MergeCells == nil || s . x . MergeCells . MergeCell == nil {
return nil
}
newMergedCells := [ ] * sml . CT_MergeCell { }
for _ , mc := range s . MergedCells ( ) {
newRefStr := moveRangeLeft ( mc . Reference ( ) , columnIdx , true )
if newRefStr != "" {
mc . SetReference ( newRefStr )
newMergedCells = append ( newMergedCells , mc . X ( ) )
}
}
s . x . MergeCells . MergeCell = newMergedCells
return nil
}
func ( s * Sheet ) removeColumnFromNamedRanges ( columnIdx uint32 ) error {
for _ , dn := range s . w . DefinedNames ( ) {
name := dn . Name ( )
content := dn . Content ( )
sp := strings . Split ( content , "!" )
if len ( sp ) != 2 {
return errors . New ( "Incorrect named range:" + content )
}
sheetName := sp [ 0 ]
if s . Name ( ) == sheetName {
err := s . w . RemoveDefinedName ( dn )
if err != nil {
return err
}
newRefStr := moveRangeLeft ( sp [ 1 ] , columnIdx , true )
if newRefStr != "" {
newContent := sheetName + "!" + newRefStr
s . w . AddDefinedName ( name , newContent )
}
}
}
numTables := 0
if s . x . TableParts != nil && s . x . TableParts . TablePart != nil {
numTables = len ( s . x . TableParts . TablePart )
}
if numTables != 0 {
startFromTable := 0
for _ , sheet := range s . w . Sheets ( ) {
if sheet . Name ( ) == s . Name ( ) {
break
} else {
if sheet . x . TableParts != nil && sheet . x . TableParts . TablePart != nil {
startFromTable += len ( sheet . x . TableParts . TablePart )
}
}
}
2020-03-25 16:40:25 +03:00
sheetTables := s . w . tables [ startFromTable : startFromTable + numTables ]
2020-02-11 22:47:08 +03:00
for tblIndex , tbl := range sheetTables {
newTable := tbl
newTable . RefAttr = moveRangeLeft ( newTable . RefAttr , columnIdx , false )
2020-03-25 16:40:25 +03:00
s . w . tables [ startFromTable + tblIndex ] = newTable
2020-02-11 22:47:08 +03:00
}
}
return nil
}
func moveRangeLeft ( ref string , columnIdx uint32 , remove bool ) string {
fromCell , toCell , err := reference . ParseRangeReference ( ref )
if err == nil {
fromColIdx , toColIdx := fromCell . ColumnIdx , toCell . ColumnIdx
if columnIdx >= fromColIdx && columnIdx <= toColIdx {
if fromColIdx == toColIdx {
if remove {
return ""
} else {
return ref
}
} else {
newTo := toCell . Update ( update . UpdateActionRemoveColumn )
return fmt . Sprintf ( "%s:%s" , fromCell . String ( ) , newTo . String ( ) )
}
} else if columnIdx < fromColIdx {
newFrom := fromCell . Update ( update . UpdateActionRemoveColumn )
newTo := toCell . Update ( update . UpdateActionRemoveColumn )
return fmt . Sprintf ( "%s:%s" , newFrom . String ( ) , newTo . String ( ) )
}
} else {
fromColumn , toColumn , err := reference . ParseColumnRangeReference ( ref )
if err != nil {
return ""
}
fromColIdx , toColIdx := fromColumn . ColumnIdx , toColumn . ColumnIdx
if columnIdx >= fromColIdx && columnIdx <= toColIdx {
if fromColIdx == toColIdx {
if remove {
return ""
} else {
return ref
}
} else {
newTo := toColumn . Update ( update . UpdateActionRemoveColumn )
return fmt . Sprintf ( "%s:%s" , fromColumn . String ( ) , newTo . String ( ) )
}
} else if columnIdx < fromColIdx {
newFrom := fromColumn . Update ( update . UpdateActionRemoveColumn )
newTo := toColumn . Update ( update . UpdateActionRemoveColumn )
return fmt . Sprintf ( "%s:%s" , newFrom . String ( ) , newTo . String ( ) )
}
}
return ""
}
func ( s * Sheet ) getAllCellsInFormulaArraysForColumn ( ) ( map [ string ] bool , error ) {
return s . getAllCellsInFormulaArrays ( false )
}
// getAllCellsInFormulaArrays returns all cells of the sheet that are covered by formula arrays. It is a helper for checking when removing rows and columns and skips all arrays of length 1 column when removing columns and all arrays of length 1 row when removing rows.
func ( s * Sheet ) getAllCellsInFormulaArrays ( isRow bool ) ( map [ string ] bool , error ) {
ev := formula . NewEvaluator ( )
ctx := s . FormulaContext ( )
cellsInFormulaArrays := map [ string ] bool { }
for _ , r := range s . Rows ( ) {
for _ , c := range r . Cells ( ) {
if c . X ( ) . F != nil {
formStr := c . X ( ) . F . Content
if c . X ( ) . F . TAttr == sml . ST_CellFormulaTypeArray {
res := ev . Eval ( ctx , formStr ) . AsString ( )
if res . Type == formula . ResultTypeError {
unioffice . Log ( "error evaulating formula %s: %s" , formStr , res . ErrorMessage )
c . X ( ) . V = nil
}
if res . Type == formula . ResultTypeArray {
cref , err := reference . ParseCellReference ( c . Reference ( ) )
if err != nil {
return map [ string ] bool { } , err
}
if ( isRow && len ( res . ValueArray ) == 1 ) || ( ! isRow && len ( res . ValueArray [ 0 ] ) == 1 ) {
continue
}
for ir , row := range res . ValueArray {
rowIdx := cref . RowIdx + uint32 ( ir )
for ic := range row {
column := reference . IndexToColumn ( cref . ColumnIdx + uint32 ( ic ) )
cellsInFormulaArrays [ fmt . Sprintf ( "%s%d" , column , rowIdx ) ] = true
}
}
} else if res . Type == formula . ResultTypeList {
cref , err := reference . ParseCellReference ( c . Reference ( ) )
if err != nil {
return map [ string ] bool { } , err
}
if isRow || len ( res . ValueList ) == 1 {
continue
}
rowIdx := cref . RowIdx
for ic := range res . ValueList {
column := reference . IndexToColumn ( cref . ColumnIdx + uint32 ( ic ) )
cellsInFormulaArrays [ fmt . Sprintf ( "%s%d" , column , rowIdx ) ] = true
}
}
}
}
}
}
return cellsInFormulaArrays , nil
}