70 lines
7.6 KiB
Go
Raw Normal View History

2020-08-23 14:15:53 +00:00
//
// Copyright 2020 FoxyUtils ehf. All rights reserved.
//
// This is a commercial product and requires a license to operate.
// A trial license can be obtained at https://unidoc.io
//
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
//
// Use of this source code is governed by the UniDoc End User License Agreement
// terms that can be accessed at https://unidoc.io/eula/
2024-07-28 07:48:26 +00:00
package reference ;import (_c "errors";_bc "fmt";_ab "github.com/unidoc/unioffice/spreadsheet/update";_b "regexp";_a "strconv";_cc "strings";);
// ColumnToIndex maps a column to a zero based index (e.g. A = 0, B = 1, AA = 26)
func ColumnToIndex (col string )uint32 {col =_cc .ToUpper (col );_ce :=uint32 (0);for _ ,_ca :=range col {_ce *=26;_ce +=uint32 (_ca -'A'+1);};return _ce -1;};
2024-05-22 00:21:11 +00:00
2024-06-20 13:33:45 +00:00
// ParseColumnReference parses a column reference of the form 'Sheet1!A' and splits it
// into sheet name and column segments.
2024-07-28 07:48:26 +00:00
func ParseColumnReference (s string )(ColumnReference ,error ){s =_cc .TrimSpace (s );if len (s )< 1{return ColumnReference {},_c .New ("\u0063\u006f\u006c\u0075\u006d\u006e \u0072\u0065\u0066\u0065\u0072\u0065\u006e\u0063\u0065\u0020\u006d\u0075\u0073\u0074\u0020\u0068\u0061\u0076\u0065\u0020a\u0074\u0020\u006c\u0065\u0061\u0073\u0074\u0020\u006f\u006e\u0065\u0020\u0063\u0068a\u0072a\u0063\u0074\u0065\u0072");
};_gb :=ColumnReference {};_fd ,_gca ,_abb :=_ge (s );if _abb !=nil {return ColumnReference {},_abb ;};if _fd !=""{_gb .SheetName =_fd ;};if _gca [0]=='$'{_gb .AbsoluteColumn =true ;_gca =_gca [1:];};if !_cfe .MatchString (_gca ){return ColumnReference {},_c .New ("\u0063\u006f\u006c\u0075\u006dn\u0020\u0072\u0065\u0066\u0065\u0072\u0065\u006e\u0063\u0065\u0020\u006d\u0075s\u0074\u0020\u0062\u0065\u0020\u0062\u0065\u0074\u0077\u0065\u0065\u006e\u0020\u0041\u0020\u0061\u006e\u0064\u0020\u005a\u005a");
};_gb .Column =_gca ;_gb .ColumnIdx =ColumnToIndex (_gb .Column );return _gb ,nil ;};
2024-06-20 13:33:45 +00:00
2024-07-28 07:48:26 +00:00
// Update updates reference to point one of the neighboring columns with respect to the update type after removing a row/column.
func (_ac *ColumnReference )Update (updateType _ab .UpdateAction )*ColumnReference {switch updateType {case _ab .UpdateActionRemoveColumn :_ff :=_ac ;_ff .ColumnIdx =_ac .ColumnIdx -1;_ff .Column =IndexToColumn (_ff .ColumnIdx );return _ff ;default:return _ac ;
};};
2024-05-22 00:21:11 +00:00
// ColumnReference is a parsed reference to a column. Input is of the form 'A',
// '$C', etc.
2024-06-20 13:33:45 +00:00
type ColumnReference struct{ColumnIdx uint32 ;Column string ;AbsoluteColumn bool ;SheetName string ;};
2024-05-22 00:21:11 +00:00
// ParseColumnRangeReference splits a range reference of the form "A:B" into its
// components.
2024-07-28 07:48:26 +00:00
func ParseColumnRangeReference (s string )(_fc ,_bfg ColumnReference ,_fdf error ){_bba :="";_abd :=_cc .Split (s ,"\u0021");if len (_abd )==2{_bba =_abd [0];s =_abd [1];};_cba :=_cc .Split (s ,"\u003a");if len (_cba )!=2{return ColumnReference {},ColumnReference {},_c .New ("i\u006ev\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006eg\u0065\u0020\u0066\u006frm\u0061\u0074");
};if _bba !=""{_cba [0]=_bba +"\u0021"+_cba [0];_cba [1]=_bba +"\u0021"+_cba [1];};_ae ,_fdf :=ParseColumnReference (_cba [0]);if _fdf !=nil {return ColumnReference {},ColumnReference {},_fdf ;};_db ,_fdf :=ParseColumnReference (_cba [1]);if _fdf !=nil {return ColumnReference {},ColumnReference {},_fdf ;
};return _ae ,_db ,nil ;};func _ge (_dc string )(string ,string ,error ){_cfee :="";_bfa :=_cc .LastIndex (_dc ,"\u0021");if _bfa > -1{_cfee =_dc [:_bfa ];_dc =_dc [_bfa +1:];if _cfee ==""{return "","",_c .New ("\u0049n\u0076a\u006c\u0069\u0064\u0020\u0073h\u0065\u0065t\u0020\u006e\u0061\u006d\u0065");
};};return _cfee ,_dc ,nil ;};var _cfe =_b .MustCompile ("^\u005b\u0061\u002d\u007aA-\u005a]\u0028\u005b\u0061\u002d\u007aA\u002d\u005a\u005d\u003f\u0029\u0024");
2024-05-04 18:48:06 +00:00
2024-07-28 07:48:26 +00:00
// Update updates reference to point one of the neighboring cells with respect to the update type after removing a row/column.
func (_cf *CellReference )Update (updateType _ab .UpdateAction )*CellReference {switch updateType {case _ab .UpdateActionRemoveColumn :_dd :=_cf ;_dd .ColumnIdx =_cf .ColumnIdx -1;_dd .Column =IndexToColumn (_dd .ColumnIdx );return _dd ;default:return _cf ;
};};
2024-02-17 03:37:07 +00:00
// CellReference is a parsed reference to a cell. Input is of the form 'A1',
// '$C$2', etc.
2024-05-04 18:48:06 +00:00
type CellReference struct{RowIdx uint32 ;ColumnIdx uint32 ;Column string ;AbsoluteColumn bool ;AbsoluteRow bool ;SheetName string ;};
2024-02-17 03:37:07 +00:00
2024-07-28 07:48:26 +00:00
// IndexToColumn maps a column number to a column name (e.g. 0 = A, 1 = B, 26 = AA)
func IndexToColumn (col uint32 )string {var _e [64+1]byte ;_cb :=len (_e );_cec :=col ;const _cd =26;for _cec >=_cd {_cb --;_ba :=_cec /_cd ;_e [_cb ]=byte ('A'+uint (_cec -_ba *_cd ));_cec =_ba -1;};_cb --;_e [_cb ]=byte ('A'+uint (_cec ));return string (_e [_cb :]);
};
// ParseCellReference parses a cell reference of the form 'A10' and splits it
// into column/row segments.
func ParseCellReference (s string )(CellReference ,error ){s =_cc .TrimSpace (s );if len (s )< 2{return CellReference {},_c .New ("\u0063\u0065\u006c\u006c\u0020\u0072\u0065\u0066e\u0072\u0065\u006ece\u0020\u006d\u0075\u0073\u0074\u0020h\u0061\u0076\u0065\u0020\u0061\u0074\u0020\u006c\u0065\u0061\u0073\u0074\u0020\u0074\u0077o\u0020\u0063\u0068\u0061\u0072\u0061\u0063\u0074e\u0072\u0073");
};_gc :=CellReference {};_cg ,_f ,_cga :=_ge (s );if _cga !=nil {return CellReference {},_cga ;};if _cg !=""{_gc .SheetName =_cg ;};if s [0]=='$'{_gc .AbsoluteColumn =true ;_f =_f [1:];};_fg :=-1;_bcb :for _fe :=0;_fe < len (_f );_fe ++{switch {case _f [_fe ]>='0'&&_f [_fe ]<='9'||_f [_fe ]=='$':_fg =_fe ;
break _bcb ;};};switch _fg {case 0:return CellReference {},_bc .Errorf ("\u006e\u006f\u0020\u006cet\u0074\u0065\u0072\u0020\u0070\u0072\u0065\u0066\u0069\u0078\u0020\u0069\u006e\u0020%\u0073",_f );case -1:return CellReference {},_bc .Errorf ("\u006eo\u0020d\u0069\u0067\u0069\u0074\u0073\u0020\u0069\u006e\u0020\u0025\u0073",_f );
};_gc .Column =_f [0:_fg ];if _f [_fg ]=='$'{_gc .AbsoluteRow =true ;_fg ++;};_gc .ColumnIdx =ColumnToIndex (_gc .Column );_bf ,_cga :=_a .ParseUint (_f [_fg :],10,32);if _cga !=nil {return CellReference {},_bc .Errorf ("e\u0072\u0072\u006f\u0072 p\u0061r\u0073\u0069\u006e\u0067\u0020r\u006f\u0077\u003a\u0020\u0025\u0073",_cga );
};if _bf ==0{return CellReference {},_bc .Errorf ("\u0065\u0072\u0072\u006f\u0072\u0020\u0070\u0061\u0072\u0073i\u006e\u0067\u0020\u0072\u006f\u0077\u003a \u0063\u0061\u006e\u006e\u006f\u0074\u0020\u0062\u0065\u0020\u0030");};_gc .RowIdx =uint32 (_bf );
return _gc ,nil ;};
2023-12-28 15:53:06 +00:00
2024-06-20 13:33:45 +00:00
// ParseRangeReference splits a range reference of the form "A1:B5" into its
// components.
2024-07-28 07:48:26 +00:00
func ParseRangeReference (s string )(_bfaa ,_gbd CellReference ,_eg error ){_bfac ,_ceb ,_eg :=_ge (s );if _eg !=nil {return CellReference {},CellReference {},_eg ;};_aa :=_cc .Split (_ceb ,"\u003a");if len (_aa )!=2{return CellReference {},CellReference {},_c .New ("i\u006ev\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006eg\u0065\u0020\u0066\u006frm\u0061\u0074");
};if _bfac !=""{_aa [0]=_bfac +"\u0021"+_aa [0];_aa [1]=_bfac +"\u0021"+_aa [1];};_ag ,_eg :=ParseCellReference (_aa [0]);if _eg !=nil {return CellReference {},CellReference {},_eg ;};_ef ,_eg :=ParseCellReference (_aa [1]);if _eg !=nil {return CellReference {},CellReference {},_eg ;
};return _ag ,_ef ,nil ;};
2023-06-10 07:12:37 +00:00
2024-06-20 13:33:45 +00:00
// String returns a string representation of CellReference.
2024-07-28 07:48:26 +00:00
func (_bb CellReference )String ()string {_g :=make ([]byte ,0,4);if _bb .AbsoluteColumn {_g =append (_g ,'$');};_g =append (_g ,_bb .Column ...);if _bb .AbsoluteRow {_g =append (_g ,'$');};_g =_a .AppendInt (_g ,int64 (_bb .RowIdx ),10);return string (_g );
2024-06-20 13:33:45 +00:00
};
2024-07-28 07:48:26 +00:00
// String returns a string representation of ColumnReference.
func (_bcf ColumnReference )String ()string {_gf :=make ([]byte ,0,4);if _bcf .AbsoluteColumn {_gf =append (_gf ,'$');};_gf =append (_gf ,_bcf .Column ...);return string (_gf );};