Hi k.misha,

On Thu, Apr 18, 2013 at 11:00:00PM +0300, k.misha wrote:
> Hi!
> 
>  
> 
> I have a problem using this code:
> 
>  
> 
> #define S_CURRENCY 106 // DOESN'T WORK =(
> 
> #define S_PERCENT_SHORT 10 // 123%
> 
> #define S_PERCENT 11 //123.00%
> 
> #define S_DEFAULT 0
> 
>  
> 
>  
> 
>       Reference< XSpreadsheet > rSpSheet (rSheet, UNO_QUERY);
> 
>       Reference< XCell > rCell = rSpSheet->getCellByPosition(0, 0);
> 
>       Reference< XPropertySet > xPropSet(rCell, UNO_QUERY);
> 
>  
> 
>  
> xPropSet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("NumberForma
> t")), makeAny(S_CURRENCY));
> 
>  
> 
>  
> 
> All other properties(S_PERCENT_SHORT,S_PERCENT,S_DEFAULT) works
> greatly!  Only currency doesn't work =( What I'm doing wrong?

These constants are bad, you should use the number formats API:

http://wiki.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Number_Formats
"Number formats are defined on the document level. A document displaying
formatted values has a collection of number formats, each with a unique
index key within that document. Identical formats are not necessarily
represented by the same index key in different documents."

Note the "not necessarily". This means, you have to use 
http://www.openoffice.org/api/docs/common/ref/com/sun/star/util/XNumberFormats.html
http://www.openoffice.org/api/docs/common/ref/com/sun/star/util/XNumberFormatTypes.html
as explained here
http://wiki.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Managing_Number_Formats

See the code attached for an example.


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina
#**************************************************************
#
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.
#
#**************************************************************

# On *nix can be overridden with
# make PRJ=$OO_SDK_HOME
PRJ=../../../..
SETTINGS=$(PRJ)/settings

include $(SETTINGS)/settings.mk
include $(SETTINGS)/std.mk
include $(SETTINGS)/dk.mk

# Define non-platform/compiler specific settings
APP_NAME=NumberFormats
MAINTARGET=$(APP_NAME)_Example

#this should be in odk/settings/std.mk
OUT_DEPLOY=$(OUT)/deploy

OUT_APP_INC=$(OUT_INC)/$(APP_NAME)
OUT_APP_GEN=$(OUT_MISC)/$(APP_NAME)
OUT_APP_OBJ=$(OUT_OBJ)/$(APP_NAME)
OUT_APP_LIB=$(SHAREDLIB_OUT)/$(APP_NAME)
OUT_APP_BIN=$(OUT_BIN)/$(APP_NAME)
OUT_APP_DEPLOY=$(OUT_DEPLOY)/$(APP_NAME)

CXXFILES = main.cxx

OBJFILES = $(patsubst %.cxx,$(OUT_APP_OBJ)/%.$(OBJ_EXT),$(CXXFILES))


ifneq "$(DEBUG)" ""
        CC_DEFINES+=-DOSL_DEBUG_LEVEL=3
endif

# Targets
.PHONY: ALL
ALL : \
        $(MAINTARGET)

include $(SETTINGS)/stdtarget.mk

$(OUT_APP_OBJ)/%.$(OBJ_EXT) : %.cxx $(SDKTYPEFLAG)
        -$(MKDIR) $(subst /,$(PS),$(@D))
        $(CC) $(CC_FLAGS) $(STL_INCLUDES) $(CC_INCLUDES) -I$(OUT_APP_INC) 
$(CC_DEFINES) $(CC_OUTPUT_SWITCH)$(subst /,$(PS),$@) $<

$(OUT_APP_BIN)/_$(APP_NAME)$(EXE_EXT) : $(OBJFILES)
        -$(MKDIR) $(subst /,$(PS),$(@D))
        -$(MKDIR) $(subst /,$(PS),$(OUT_APP_GEN))
ifeq "$(OS)" "WIN"
        $(LINK) $(EXE_LINK_FLAGS) /OUT:$@ /MAP:$(OUT_APP_GEN)/$(basename 
$(@F)).map \
                $(OBJFILES) $(CPPUHELPERLIB) $(CPPULIB) $(SALHELPERLIB) 
$(SALLIB) $(STLPORTLIB)
else
        $(LINK) $(EXE_LINK_FLAGS) $(LINK_LIBS) -o $@ $(OBJFILES) \
                $(CPPUHELPERLIB) $(CPPULIB) $(SALHELPERLIB) $(SALLIB) 
$(STLPORTLIB) $(STDC++LIB) $(CPPUHELPERDYLIB) $(CPPUDYLIB) $(SALHELPERDYLIB) 
$(SALDYLIB)
ifeq "$(OS)" "MACOSX"
        $(INSTALL_NAME_URELIBS_BIN)  $@
endif
endif

$(OUT_APP_BIN)/$(APP_NAME)$(EXE_EXT) : $(OUT_APP_BIN)/_$(APP_NAME)$(EXE_EXT)
        -$(MKDIR) $(subst /,$(PS),$(@D))
        $(COPY) $(subst /,$(PS),$(BIN_DIR)/unoapploader$(EXE_EXT)) $(subst 
/,$(PS),$@)
# workaround for touch problem under Windows with full qualified paths
        make -t $@

$(MAINTARGET) : $(OUT_APP_BIN)/$(APP_NAME)$(EXE_EXT)
        @echo 
--------------------------------------------------------------------------------
        @echo Please use the following command to execute the example!
        @echo -
        @echo $(MAKE) $(APP_NAME).run
        @echo 
--------------------------------------------------------------------------------


$(APP_NAME).run: $(OUT_APP_BIN)/$(APP_NAME)$(EXE_EXT)
        cd $(subst /,$(PS),$(OUT_APP_BIN)) && $(basename $@)

.PHONY: clean
clean :
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_INC))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_GEN))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_OBJ))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_LIB))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_BIN))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_DEPLOY))
/**************************************************************
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 *************************************************************/

#include <cppuhelper/bootstrap.hxx>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/frame/FrameSearchFlag.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/lang/Locale.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/table/XCell.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/util/NumberFormat.hpp>
#include <com/sun/star/util/XNumberFormatTypes.hpp>
#include <com/sun/star/util/XNumberFormatsSupplier.hpp>

#include <iostream>

namespace css = ::com::sun::star;
using rtl::OUString;

static sal_Int32
GetNumberFormat(
    const OUString sFormat,
    const css::lang::Locale &aLocale,
    const css::uno::Reference< css::util::XNumberFormats > &rxNumberFormats )
{
    sal_Int32 nFormat( 0 );

    try
    {
        nFormat = rxNumberFormats->queryKey( sFormat, aLocale, sal_False );
        if ( nFormat == -1 )
            nFormat = rxNumberFormats->addNew( sFormat, aLocale );
    }
    catch ( const css::uno::Exception &e )
    {
        std::cerr << "Caught an exception: "
                  << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 
).getStr()
                  << '\n';
    }

    return nFormat;
}

static sal_Int32
InsertIntoCell(
    const css::uno::Reference< css::table::XCell > &rxCell,
    double nValue,
    sal_Int32 nNumFormat )
{
    try
    {
        rxCell->setValue( nValue );
        css::uno::Reference< css::beans::XPropertySet > xCellProps( rxCell, 
css::uno::UNO_QUERY_THROW );
        xCellProps->setPropertyValue(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "NumberFormat" ) ),
            css::uno::makeAny( nNumFormat )      );
    }
    catch ( const css::uno::Exception &e )
    {
        std::cerr << "Caught an exception: "
                  << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 
).getStr()
                  << '\n';
    }
}

static sal_Int32
InsertIntoCell(
    const css::uno::Reference< css::table::XCell > &rxCell,
    const OUString &rFormula,
    sal_Int32 nNumFormat )
{
    try
    {
        rxCell->setFormula( rFormula );
        css::uno::Reference< css::beans::XPropertySet > xCellProps( rxCell, 
css::uno::UNO_QUERY_THROW );
        xCellProps->setPropertyValue(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "NumberFormat" ) ),
            css::uno::makeAny( nNumFormat )      );
    }
    catch ( const css::uno::Exception &e )
    {
        std::cerr << "Caught an exception: "
                  << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 
).getStr()
                  << '\n';
    }
}

int main( void )
{
    try
    {
        css::uno::Reference< css::uno::XComponentContext > xContext( 
cppu::bootstrap() );

        css::uno::Reference< css::frame::XComponentLoader  > xLoader(
            xContext->getServiceManager()->createInstanceWithContext(
                OUString( RTL_CONSTASCII_USTRINGPARAM(
                              "com.sun.star.frame.Desktop" ) ), xContext ),
            css::uno::UNO_QUERY_THROW );

        css::uno::Reference < css::sheet::XSpreadsheetDocument > xDoc(
            xLoader->loadComponentFromURL(
                OUString( RTL_CONSTASCII_USTRINGPARAM( "private:factory/scalc" 
) ),
                OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ),
                css::frame::FrameSearchFlag::ALL,
                css::uno::Sequence < css::beans::PropertyValue >() ),
            css::uno::UNO_QUERY_THROW );

        css::uno::Reference< css::beans::XPropertySet > xDocProps(
            xDoc, css::uno::UNO_QUERY_THROW );
        css::uno::Reference< css::util::XNumberFormatsSupplier > 
xNumberFormatsSupplier(
            xDoc, css::uno::UNO_QUERY_THROW );

        css::lang::Locale aLocale;
        xDocProps->getPropertyValue(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "CharLocale" ) ) ) >>= 
aLocale;

        css::uno::Reference< css::util::XNumberFormats > xNumberFormats(
            xNumberFormatsSupplier->getNumberFormats() );

        OUString sCustom1( RTL_CONSTASCII_USTRINGPARAM( 
"#,#00.00%;[RED]-#,#00.00%" ) ),
                 sCustom2( RTL_CONSTASCII_USTRINGPARAM( "0.00%" ) );

        sal_Int32 nCustom1( GetNumberFormat( sCustom1, aLocale, xNumberFormats 
) );
        sal_Int32 nCustom2( GetNumberFormat( sCustom2, aLocale, xNumberFormats 
) );

        css::uno::Reference< css::util::XNumberFormatTypes > xNumberFormatTypes(
            xNumberFormats, css::uno::UNO_QUERY_THROW );

        sal_Int32 nCurrency = xNumberFormatTypes->getStandardFormat(
                                  css::util::NumberFormat::CURRENCY, aLocale );
        sal_Int32 nNumber = xNumberFormatTypes->getStandardFormat(
                                css::util::NumberFormat::NUMBER, aLocale );
        sal_Int32 nSci = xNumberFormatTypes->getStandardFormat(
                             css::util::NumberFormat::SCIENTIFIC, aLocale );
        sal_Int32 nPercent = xNumberFormatTypes->getStandardFormat(
                                 css::util::NumberFormat::PERCENT, aLocale );

        css::uno::Reference< css::sheet::XSpreadsheets > xSpreadsheets( 
xDoc->getSheets() );
        OSL_ENSURE( xSpreadsheets.is(), "No XSpreadsheets!" );

        if ( !xSpreadsheets->hasElements() )
        {
            xSpreadsheets->insertNewByName(
                OUString( RTL_CONSTASCII_USTRINGPARAM( "Sheet 1" ) ), 0 );
        }

        css::uno::Reference< css::container::XIndexAccess > xIndexAccess(
            xSpreadsheets, css::uno::UNO_QUERY_THROW );
        css::uno::Reference< css::sheet::XSpreadsheet > xSheet(
            xIndexAccess->getByIndex( 0 ), css::uno::UNO_QUERY_THROW );

        sal_Int32 nColumn( 0 ), nRow( 0 );
        double nVal( 123.456 );

        InsertIntoCell( xSheet->getCellByPosition( nColumn, nRow++ ),
                        nVal,
                        nCustom1 );
        InsertIntoCell( xSheet->getCellByPosition( nColumn, nRow++ ),
                        -nVal, nCustom1 );
        InsertIntoCell( xSheet->getCellByPosition( nColumn, nRow++ ),
                        OUString( RTL_CONSTASCII_USTRINGPARAM( "=SUM(A1;A2)" ) 
),
                        nCustom2 );
        InsertIntoCell( xSheet->getCellByPosition( nColumn, nRow++ ),
                        nVal,
                        nCurrency );
        InsertIntoCell( xSheet->getCellByPosition( nColumn, nRow++ ),
                        nVal,
                        nNumber );
        InsertIntoCell( xSheet->getCellByPosition( nColumn, nRow++ ),
                        nVal,
                        nSci );
        InsertIntoCell( xSheet->getCellByPosition( nColumn, nRow++ ),
                        nVal,
                        nPercent );

    }
    catch ( const css::uno::Exception &e )
    {
        std::cerr << "Caught an exception: "
                  << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 
).getStr()
                  << '\n';
    }

    return 0;
}

Attachment: pgpJkcmDcPSnN.pgp
Description: PGP signature

Reply via email to