diff --git a/components/fpspreadsheet/fpsfunc.pas b/components/fpspreadsheet/fpsfunc.pas index da67fb6b8..5ab870ce3 100644 --- a/components/fpspreadsheet/fpsfunc.pas +++ b/components/fpspreadsheet/fpsfunc.pas @@ -18,7 +18,7 @@ implementation uses Math, lazutf8, StrUtils, DateUtils, - xlsconst, {%H-}fpsPatches, fpsUtils, fpsexprparser; + xlsconst, {%H-}fpsPatches, fpsUtils, fpsnumformat, fpsexprparser; {------------------------------------------------------------------------------} @@ -833,9 +833,24 @@ begin Result := StringResult(UTF8StringReplace(s, sOld, sNew, [rfReplaceAll])); end; +procedure fpsTEXT(var Result: TsExpressionResult; const Args: TsExprParameterArray); +// TEXT( value, format ) +// Returns a value converted to text with a specified format. +var + fmt: String; + value: double; +begin + value := ArgToFloat(Args[0]); + fmt := ArgToString(Args[1]); + if IsDateTimeFormat(fmt) then + Result := StringResult(FormatDateTime(fmt, value)) + else + Result := StringResult(Format(fmt, [value])); +end; + procedure fpsTRIM(var Result: TsExpressionResult; const Args: TsExprParameterArray); // TRIM( text ) -// returns a text value with the leading and trailing spaces removed +// Returns a text value with the leading and trailing spaces removed begin Result := StringResult(UTF8Trim(ArgToString(Args[0]))); end; @@ -862,6 +877,9 @@ begin else if TryStrToFloat(s, x, ExprFormatSettings) then Result := FloatResult(x) + else + if TryStrToDateTime(s, x) then + Result := FloatResult(x) else Result := ErrorResult(errWrongType); end; @@ -1630,6 +1648,7 @@ begin AddFunction(cat, 'REPT', 'S', 'SI', INT_EXCEL_SHEET_FUNC_REPT, @fpsREPT); AddFunction(cat, 'RIGHT', 'S', 'Si', INT_EXCEL_SHEET_FUNC_RIGHT, @fpsRIGHT); AddFunction(cat, 'SUBSTITUTE','S', 'SSSi', INT_EXCEL_SHEET_FUNC_SUBSTITUTE, @fpsSUBSTITUTE); + AddFunction(cat, 'TEXT', 'S', '?S', INT_EXCEL_SHEET_FUNC_TEXT, @fpsTEXT); AddFunction(cat, 'TRIM', 'S', 'S', INT_EXCEL_SHEET_FUNC_TRIM, @fpsTRIM); AddFunction(cat, 'UPPER', 'S', 'S', INT_EXCEL_SHEET_FUNC_UPPER, @fpsUPPER); AddFunction(cat, 'VALUE', 'F', 'S', INT_EXCEL_SHEET_FUNC_VALUE, @fpsVALUE); diff --git a/components/fpspreadsheet/fpspreadsheet.pas b/components/fpspreadsheet/fpspreadsheet.pas index 4106a38aa..0c2008523 100755 --- a/components/fpspreadsheet/fpspreadsheet.pas +++ b/components/fpspreadsheet/fpspreadsheet.pas @@ -6771,9 +6771,7 @@ end; (ODS seems to be a bit less restrictive, but if we follow Excel's convention we always have valid sheet names independent of the format. - @param AName Name to be checked. If the input name is already - used AName will be modified such that the sheet - name is unique. + @param AName Name to be checked. @param ReplaceDuplicateName If there exists already a sheet name equal to AName then a number is added to AName such that the name is unique.