sc/source/filter/inc/addressconverter.hxx | 37 ------- sc/source/filter/oox/addressconverter.cxx | 156 +----------------------------- sc/source/filter/oox/scenariobuffer.cxx | 3 sc/source/filter/oox/sheetdatacontext.cxx | 3 4 files changed, 14 insertions(+), 185 deletions(-)
New commits: commit a50b2d11ed6066a655278305f1fc880d3a5fe911 Author: Justin Luth <justin.l...@collabora.com> AuthorDate: Wed Apr 24 14:23:22 2024 -0400 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Tue Apr 30 08:48:11 2024 +0200 xlsx import: parse short-hand version of address Since I did this for the range, I might as well try to entirely remove parseOoxAddress2d. This allows me to remove parseOoxAddress2d. No point in having duplicate functions to turn a string into an ScAddress. [I assume this was a left-over from when this code was in /oox and didn't have access to sc methods.] Earlier patchsets checked that the new method and the old method returned the same values for all existing unit tests. Change-Id: Ic45eaf53417b0d8afad7b49959014162549653ec Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166606 Reviewed-by: Justin Luth <jl...@mail.com> Tested-by: Jenkins Reviewed-by: Miklos Vajna <vmik...@collabora.com> diff --git a/sc/source/filter/inc/addressconverter.hxx b/sc/source/filter/inc/addressconverter.hxx index 034bcbacaf39..41fe38cd0efd 100644 --- a/sc/source/filter/inc/addressconverter.hxx +++ b/sc/source/filter/inc/addressconverter.hxx @@ -97,34 +97,6 @@ class AddressConverter final : public WorkbookHelper public: explicit AddressConverter( const WorkbookHelper& rHelper ); - /** Tries to parse the passed string for a 2d cell address in A1 notation. - - This function accepts all strings that match the regular expression - "[a-zA-Z]{1,6}0*[1-9][0-9]{0,8}" (without quotes), i.e. 1 to 6 letters - for the column index (translated to 0-based column indexes from 0 to - 321,272,405), and 1 to 9 digits for the 1-based row index (translated - to 0-based row indexes from 0 to 999,999,998). The row number part may - contain leading zeros, they will be ignored. It is up to the caller to - handle cell addresses outside of a specific valid range (e.g. the - entire spreadsheet). - - @param ornColumn (out-parameter) Returns the converted column index. - @param ornRow (out-parameter) returns the converted row index. - @param rString The string containing the cell address. - @param nStart Start index of string part in rString to be parsed. - @param nLength Length of string part in rString to be parsed. - - @return true = Parsed string was valid, returned values can be used. - */ - static bool parseOoxAddress2d( - sal_Int32& ornColumn, sal_Int32& ornRow, - std::u16string_view aString, - sal_Int32 nStart = 0, - sal_Int32 nLength = SAL_MAX_INT32 ); - - static bool parseOoxAddress2d( - sal_Int32& ornColumn, sal_Int32& ornRow, std::string_view pStr ); - /** Returns the biggest valid cell address in the own Calc document. */ const ScAddress& getMaxApiAddress() const { return maMaxApiPos; } @@ -188,10 +160,7 @@ public: static bool convertToCellAddressUnchecked( ScAddress& orAddress, const OUString& rString, - sal_Int16 nSheet ); - - static bool convertToCellAddressUnchecked( - ScAddress& orAddress, std::string_view pStr, sal_Int16 nSheet ); + sal_Int16 nSheet, const ScDocument& rDoc); /** Tries to convert the passed string to a single cell address. @@ -208,10 +177,6 @@ public: sal_Int16 nSheet, bool bTrackOverflow ); - bool convertToCellAddress( - ScAddress& rAddress, - std::string_view pStr, sal_Int16 nSheet, bool bTrackOverflow ); - /** Returns a valid cell address by moving it into allowed dimensions. @param rString Cell address string in A1 notation. diff --git a/sc/source/filter/oox/addressconverter.cxx b/sc/source/filter/oox/addressconverter.cxx index 4109ef3c8833..98a8dae3b625 100644 --- a/sc/source/filter/oox/addressconverter.cxx +++ b/sc/source/filter/oox/addressconverter.cxx @@ -80,123 +80,6 @@ AddressConverter::AddressConverter( const WorkbookHelper& rHelper ) : initializeMaxPos( OOX_MAXTAB, OOX_MAXCOL, OOX_MAXROW ); } -bool AddressConverter::parseOoxAddress2d( - sal_Int32& ornColumn, sal_Int32& ornRow, - std::u16string_view aString, sal_Int32 nStart, sal_Int32 nLength ) -{ - ornColumn = ornRow = 0; - if( (nStart < 0) || (nStart >= sal_Int32(aString.size())) || (nLength < 2) ) - return false; - - const sal_Unicode* pcChar = aString.data() + nStart; - const sal_Unicode* pcEndChar = pcChar + ::std::min( nLength, sal_Int32(aString.size() - nStart) ); - - enum { STATE_COL, STATE_ROW } eState = STATE_COL; - while( pcChar < pcEndChar ) - { - sal_Unicode cChar = *pcChar; - switch( eState ) - { - case STATE_COL: - { - if( ('a' <= cChar) && (cChar <= 'z') ) - cChar = (cChar - 'a') + 'A'; - if( ('A' <= cChar) && (cChar <= 'Z') ) - { - /* Return, if 1-based column index is already 6 characters - long (12356631 is column index for column AAAAAA). */ - if( ornColumn >= 12356631 ) - return false; - ornColumn = (ornColumn * 26) + (cChar - 'A' + 1); - } - else if( ornColumn > 0 ) - { - --pcChar; - eState = STATE_ROW; - } - else - return false; - } - break; - - case STATE_ROW: - { - if( ('0' <= cChar) && (cChar <= '9') ) - { - // return, if 1-based row is already 9 digits long - if( ornRow >= 100000000 ) - return false; - ornRow = (ornRow * 10) + (cChar - '0'); - } - else - return false; - } - break; - } - ++pcChar; - } - - --ornColumn; - --ornRow; - return (ornColumn >= 0) && (ornRow >= 0); -} - -bool AddressConverter::parseOoxAddress2d( sal_Int32& ornColumn, sal_Int32& ornRow, std::string_view aStr ) -{ - ornColumn = ornRow = 0; - - enum { STATE_COL, STATE_ROW } eState = STATE_COL; - - const char* pStr = aStr.data(); - while (pStr != aStr.data() + aStr.size()) - { - char cChar = *pStr; - switch( eState ) - { - case STATE_COL: - { - if( ('a' <= cChar) && (cChar <= 'z') ) - cChar = (cChar - 'a') + 'A'; - if( ('A' <= cChar) && (cChar <= 'Z') ) - { - /* Return, if 1-based column index is already 6 characters - long (12356631 is column index for column AAAAAA). */ - if( ornColumn >= 12356631 ) - return false; - ornColumn = (ornColumn * 26) + (cChar - 'A' + 1); - } - else if( ornColumn > 0 ) - { - --pStr; - eState = STATE_ROW; - } - else - return false; - } - break; - - case STATE_ROW: - { - if( ('0' <= cChar) && (cChar <= '9') ) - { - // return, if 1-based row is already 9 digits long - if( ornRow >= 100000000 ) - return false; - ornRow = (ornRow * 10) + (cChar - '0'); - } - else - return false; - } - break; - } - ++pStr; - } - - --ornColumn; - --ornRow; - return (ornColumn >= 0) && (ornRow >= 0); -} - bool AddressConverter::checkCol( sal_Int32 nCol, bool bTrackOverflow ) { bool bValid = (0 <= nCol) && ( nCol <= maMaxPos.Col() ); @@ -230,49 +113,28 @@ bool AddressConverter::checkCellAddress( const ScAddress& rAddress, bool bTrackO } bool AddressConverter::convertToCellAddressUnchecked( ScAddress& orAddress, - const OUString& rString, sal_Int16 nSheet ) + const OUString& rString, sal_Int16 nSheet, const ScDocument& rDoc) { orAddress.SetTab(nSheet); - sal_Int32 nCol = 0; - sal_Int32 nRow = 0; - bool bRes = parseOoxAddress2d( nCol, nRow, rString ); - orAddress.SetRow(nRow); - orAddress.SetCol(nCol); + orAddress.SetRow(0); + orAddress.SetCol(0); - return bRes; -} - -bool AddressConverter::convertToCellAddressUnchecked( - ScAddress& orAddress, std::string_view pStr, sal_Int16 nSheet ) -{ - orAddress.SetTab(nSheet); - sal_Int32 nCol = 0; - sal_Int32 nRow = 0; - bool bRes = parseOoxAddress2d(nCol, nRow, pStr); - orAddress.SetRow(nRow); - orAddress.SetCol(nCol); + if (rString.isEmpty()) + return false; - return bRes; + return (orAddress.Parse(rString, rDoc, formula::FormulaGrammar::CONV_XL_OOX) + & ScRefFlags::VALID) + == ScRefFlags::VALID; } bool AddressConverter::convertToCellAddress( ScAddress& orAddress, const OUString& rString, sal_Int16 nSheet, bool bTrackOverflow ) { return - convertToCellAddressUnchecked( orAddress, rString, nSheet ) && + convertToCellAddressUnchecked(orAddress, rString, nSheet, getScDocument()) && checkCellAddress( orAddress, bTrackOverflow ); } -bool AddressConverter::convertToCellAddress( - ScAddress& rAddress, - std::string_view pStr, sal_Int16 nSheet, bool bTrackOverflow ) -{ - if (!convertToCellAddressUnchecked(rAddress, pStr, nSheet)) - return false; - - return checkCellAddress(rAddress, bTrackOverflow); -} - ScAddress AddressConverter::createValidCellAddress( const OUString& rString, sal_Int16 nSheet, bool bTrackOverflow ) { diff --git a/sc/source/filter/oox/scenariobuffer.cxx b/sc/source/filter/oox/scenariobuffer.cxx index 44ad52814680..1fbf64cc1ff4 100644 --- a/sc/source/filter/oox/scenariobuffer.cxx +++ b/sc/source/filter/oox/scenariobuffer.cxx @@ -73,7 +73,8 @@ void Scenario::importScenario( const AttributeList& rAttribs ) void Scenario::importInputCells( const AttributeList& rAttribs ) { ScenarioCellModel aModel; - AddressConverter::convertToCellAddressUnchecked( aModel.maPos, rAttribs.getString( XML_r, OUString() ), mnSheet ); + AddressConverter::convertToCellAddressUnchecked( + aModel.maPos, rAttribs.getString(XML_r, OUString()), mnSheet, getScDocument()); aModel.maValue = rAttribs.getXString( XML_val, OUString() ); aModel.mnNumFmtId = rAttribs.getInteger( XML_numFmtId, 0 ); aModel.mbDeleted = rAttribs.getBool( XML_deleted, false ); diff --git a/sc/source/filter/oox/sheetdatacontext.cxx b/sc/source/filter/oox/sheetdatacontext.cxx index 19ad0a2a3465..a6374c577a26 100644 --- a/sc/source/filter/oox/sheetdatacontext.cxx +++ b/sc/source/filter/oox/sheetdatacontext.cxx @@ -332,7 +332,8 @@ bool SheetDataContext::importCell( const AttributeList& rAttribs ) } else { - bValid = mrAddressConv.convertToCellAddress(maCellData.maCellAddr, p, mnSheet, true); + bValid = mrAddressConv.convertToCellAddress(maCellData.maCellAddr, OUString::fromUtf8(p), + mnSheet, true); mnCol = maCellData.maCellAddr.Col(); }