11import { getPivotTooBigErrorMessage } from "../components/translations_terms" ;
22import { PIVOT_MAX_NUMBER_OF_CELLS } from "../constants" ;
3- import { getFullReference , splitReference } from "../helpers/" ;
4- import { toCartesian , toXC } from "../helpers/coordinates" ;
3+ import { getFullReference } from "../helpers/" ;
4+ import { toXC } from "../helpers/coordinates" ;
55import { isFormula , range } from "../helpers/misc" ;
66import {
77 addAlignFormatToPivotHeader ,
88 getPivotStyleFromFnArgs ,
99} from "../helpers/pivot/pivot_helpers" ;
10- import { toZone } from "../helpers/zones" ;
1110import { _t } from "../translation" ;
1211import {
1312 CellErrorType ,
@@ -16,7 +15,7 @@ import {
1615 NotAvailableError ,
1716} from "../types/errors" ;
1817import { AddFunctionDescription } from "../types/functions" ;
19- import { Arg , FunctionResultObject , Matrix , Maybe , UID , Zone } from "../types/misc" ;
18+ import { Arg , FunctionResultObject , Matrix , Maybe , Zone } from "../types/misc" ;
2019import { arg } from "./arguments" ;
2120import { expectNumberGreaterThanOrEqualToOne } from "./helper_assert" ;
2221import {
@@ -28,8 +27,8 @@ import {
2827import {
2928 dichotomicSearch ,
3029 expectNumberRangeError ,
30+ expectReferenceError ,
3131 generateMatrix ,
32- isEvaluationError ,
3332 linearSearch ,
3433 LinearSearchMode ,
3534 strictToInteger ,
@@ -134,15 +133,15 @@ export const COLUMN = {
134133 description : _t ( "Column number of a specified cell." ) ,
135134 args : [
136135 arg (
137- "cell_reference (meta , range<meta >, default='this cell')" ,
136+ "cell_reference (any , range<any >, default='this cell')" ,
138137 _t (
139138 "The cell whose column number will be returned. Column A corresponds to 1. By default, the function use the cell in which the formula is entered."
140139 )
141140 ) ,
142141 ] ,
143- compute : function ( cellReference : Matrix < { value : string } > | undefined ) {
142+ compute : function ( cellReference : Arg ) {
144143 if ( cellReference === undefined ) {
145- if ( this . __originCellPosition ?. col === undefined ) {
144+ if ( this . __originCellPosition === undefined ) {
146145 return new EvaluationError (
147146 _t (
148147 "In this context, the function [[FUNCTION_NAME]] needs to have a cell or range in parameter."
@@ -151,20 +150,20 @@ export const COLUMN = {
151150 }
152151 return this . __originCellPosition . col + 1 ;
153152 }
154- if ( cellReference [ 0 ] [ 0 ] === undefined ) {
153+ const _cellReference = toMatrix ( cellReference ) ;
154+ const firstCell = _cellReference [ 0 ] [ 0 ] ;
155+ if ( firstCell === undefined ) {
156+ // to do: should work with ranges out of bounds
155157 return new EvaluationError ( _t ( "The range is out of bounds." ) ) ;
156158 }
157- if ( cellReference [ 0 ] [ 0 ] . value === CellErrorType . InvalidReference ) {
158- return cellReference [ 0 ] [ 0 ] ; // return the same error
159+ if ( firstCell . position === undefined ) {
160+ return new InvalidReferenceError ( expectReferenceError ) ;
159161 }
160- const left = this . getters . getRangeFromSheetXC (
161- this . getters . getActiveSheetId ( ) ,
162- cellReference [ 0 ] [ 0 ] . value
163- ) . zone . left ;
164- if ( cellReference . length === 1 ) {
162+ const left = firstCell . position . col ;
163+ if ( _cellReference . length === 1 ) {
165164 return left + 1 ;
166165 }
167- return generateMatrix ( cellReference . length , 1 , ( col , row ) => ( { value : left + col + 1 } ) ) ;
166+ return generateMatrix ( _cellReference . length , 1 , ( col ) => ( { value : left + col + 1 } ) ) ;
168167 } ,
169168 isExported : true ,
170169} satisfies AddFunctionDescription ;
@@ -528,13 +527,13 @@ export const ROW = {
528527 description : _t ( "Row number of a specified cell." ) ,
529528 args : [
530529 arg (
531- "cell_reference (meta , range<meta >, default='this cell')" ,
530+ "cell_reference (any , range<any >, default='this cell')" ,
532531 _t (
533532 "The cell whose row number will be returned. By default, this function uses the cell in which the formula is entered."
534533 )
535534 ) ,
536535 ] ,
537- compute : function ( cellReference : Matrix < { value : string } > | undefined ) {
536+ compute : function ( cellReference : Arg ) {
538537 if ( cellReference === undefined ) {
539538 if ( this . __originCellPosition ?. row === undefined ) {
540539 return new EvaluationError (
@@ -545,20 +544,20 @@ export const ROW = {
545544 }
546545 return this . __originCellPosition . row + 1 ;
547546 }
548- if ( cellReference [ 0 ] [ 0 ] === undefined ) {
547+ const _cellReference = toMatrix ( cellReference ) ;
548+ const firstCell = _cellReference [ 0 ] [ 0 ] ;
549+ if ( firstCell === undefined ) {
550+ // to do: should work with ranges out of bounds
549551 return new EvaluationError ( _t ( "The range is out of bounds." ) ) ;
550552 }
551- if ( cellReference [ 0 ] [ 0 ] . value === CellErrorType . InvalidReference ) {
552- return cellReference [ 0 ] [ 0 ] ; // return the same error
553+ if ( firstCell . position === undefined ) {
554+ return new InvalidReferenceError ( expectReferenceError ) ;
553555 }
554- const top = this . getters . getRangeFromSheetXC (
555- this . getters . getActiveSheetId ( ) ,
556- cellReference [ 0 ] [ 0 ] . value
557- ) . zone . top ;
558- if ( cellReference [ 0 ] . length === 1 ) {
556+ const top = firstCell . position . row ;
557+ if ( _cellReference [ 0 ] . length === 1 ) {
559558 return top + 1 ;
560559 }
561- return generateMatrix ( 1 , cellReference [ 0 ] . length , ( col , row ) => ( { value : top + row + 1 } ) ) ;
560+ return generateMatrix ( 1 , _cellReference [ 0 ] . length , ( col , row ) => ( { value : top + row + 1 } ) ) ;
562561 } ,
563562 isExported : true ,
564563} satisfies AddFunctionDescription ;
@@ -1006,7 +1005,7 @@ export const OFFSET = {
10061005 ) ,
10071006 args : [
10081007 arg (
1009- "cell_reference (meta , range<meta >)" ,
1008+ "cell_reference (any , range<any >)" ,
10101009 _t ( "The starting point from which to count the offset rows and columns." )
10111010 ) ,
10121011 arg ( "offset_rows (number)" , _t ( "The number of rows to offset by." ) ) ,
@@ -1021,26 +1020,29 @@ export const OFFSET = {
10211020 ) ,
10221021 ] ,
10231022 compute : function (
1024- cellReference : Matrix < { value : string } > ,
1023+ cellReference : Arg ,
10251024 offsetRows : Maybe < FunctionResultObject > ,
10261025 offsetColumns : Maybe < FunctionResultObject > ,
10271026 height : Maybe < FunctionResultObject > ,
10281027 width : Maybe < FunctionResultObject >
10291028 ) {
1030- if ( isEvaluationError ( cellReference [ 0 ] [ 0 ] . value ) ) {
1031- return cellReference [ 0 ] [ 0 ] ;
1029+ if ( cellReference === undefined ) {
1030+ return new InvalidReferenceError ( expectReferenceError ) ;
10321031 }
1032+ const _cellReference = toMatrix ( cellReference ) ;
1033+ const firstCell = _cellReference [ 0 ] [ 0 ] ;
10331034
1034- const ref0 = cellReference [ 0 ] [ 0 ] . value ;
1035- if ( ! ref0 ) {
1036- return new EvaluationError (
1037- "In this context, the function OFFSET needs to have a cell or range in parameter."
1038- ) ;
1035+ if ( firstCell === undefined ) {
1036+ // to do: should work with ranges out of bounds
1037+ return new EvaluationError ( _t ( "The range is out of bounds." ) ) ;
10391038 }
1040- const zone = toZone ( ref0 ) ;
10411039
1042- let offsetHeight = cellReference [ 0 ] . length ;
1043- let offsetWidth = cellReference . length ;
1040+ if ( firstCell . position === undefined ) {
1041+ return new InvalidReferenceError ( expectReferenceError ) ;
1042+ }
1043+
1044+ let offsetHeight = _cellReference [ 0 ] . length ;
1045+ let offsetWidth = _cellReference . length ;
10441046
10451047 if ( height ) {
10461048 const _height = toNumber ( height , this . locale ) ;
@@ -1062,11 +1064,6 @@ export const OFFSET = {
10621064 offsetWidth = _width ;
10631065 }
10641066
1065- const { sheetName } = splitReference ( ref0 ) ;
1066-
1067- const sheetId =
1068- ( sheetName && this . getters . getSheetIdByName ( sheetName ) ) || this . getters . getActiveSheetId ( ) ;
1069-
10701067 const _offsetRows = toNumber ( offsetRows , this . locale ) ;
10711068 const _offsetColumns = toNumber ( offsetColumns , this . locale ) ;
10721069
@@ -1075,8 +1072,8 @@ export const OFFSET = {
10751072 this . updateDependencies ?.( originPosition ) ;
10761073 }
10771074
1078- const startingCol = zone . left + _offsetColumns ;
1079- const startingRow = zone . top + _offsetRows ;
1075+ const startingCol = firstCell . position . col + _offsetColumns ;
1076+ const startingRow = firstCell . position . row + _offsetRows ;
10801077
10811078 if ( startingCol < 0 || startingRow < 0 ) {
10821079 return new InvalidReferenceError ( _t ( "OFFSET evaluates to an out of bounds range." ) ) ;
@@ -1089,6 +1086,7 @@ export const OFFSET = {
10891086 bottom : startingRow + offsetHeight - 1 ,
10901087 } ;
10911088
1089+ const sheetId = firstCell . position . sheetId ;
10921090 const range = this . getters . getRangeFromZone ( sheetId , dependencyZone ) ;
10931091 if ( range . invalidXc || range . invalidSheetName ) {
10941092 return new InvalidReferenceError ( ) ;
@@ -1246,13 +1244,12 @@ export const TAKE = {
12461244
12471245export const FORMULATEXT = {
12481246 description : _t ( "Returns a formula as a string." ) ,
1249- args : [ arg ( "cell_reference (meta)" , _t ( "A reference to a cell." ) ) ] ,
1250- compute : function ( cellReference : { value : string } ) {
1251- const { sheetName, xc } = splitReference ( cellReference . value ) ;
1252- const { col, row } = toCartesian ( xc ) ;
1253- const sheetId : UID =
1254- ( sheetName && this . getters . getSheetIdByName ( sheetName ) ) ?? this . __originSheetId ;
1255- const cell = this . getters . getCell ( { sheetId, col, row } ) ;
1247+ args : [ arg ( "cell_reference (any)" , _t ( "A reference to a cell." ) ) ] ,
1248+ compute : function ( cellReference : Maybe < FunctionResultObject > ) {
1249+ if ( cellReference ?. position === undefined ) {
1250+ return new InvalidReferenceError ( expectReferenceError ) ;
1251+ }
1252+ const cell = this . getters . getCell ( cellReference . position ) ;
12561253 if ( cell && isFormula ( cell . content ) ) {
12571254 return cell . content ;
12581255 } else {
0 commit comments