OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

NumberFormatting : AddKey

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
kaggb
General User
General User


Joined: 10 Dec 2008
Posts: 14

PostPosted: Wed Dec 10, 2008 2:43 pm    Post subject: NumberFormatting : AddKey Reply with quote

if i try to add a new key for a number format and te next time I try t find the id for that key it does not appear to find it so attempts to add it again but then throws an exception.

Any advice gratfefully received
Function getNumberFormat(iDoc as object, iFormat as string)

dim oFormatId as string
dim oLocale as new com.sun.star.lang.Locale

oFormatId = idoc.NumberFormats.querykey(iFormat,oLocale,True)

msgbox(iformat)
if oFormatid = -1 then
oFormatID = idoc.NumberFormats.addnew(iFormat,oLocale)
end if

getnumberFormat = oFormatId

end function
Back to top
View user's profile Send private message
vitcaro
OOo Advocate
OOo Advocate


Joined: 20 Feb 2007
Posts: 256
Location: Italy

PostPosted: Wed Dec 10, 2008 11:29 pm    Post subject: Reply with quote

You have to check the string representing your new format, and compare with the string really added to the numberformats by Oo. If your original string is not made in the required way, it is stored in the document in a modified string to be compatible. So, if after you check wheter this format is present, you will not find it, but at the same time you cannot add it a second time.
Back to top
View user's profile Send private message
kaggb
General User
General User


Joined: 10 Dec 2008
Posts: 14

PostPosted: Thu Dec 11, 2008 1:54 am    Post subject: Reply with quote

thank you for the advice. It appearred that instead of using [Red] I needed to use [RED].
Back to top
View user's profile Send private message
tuxyboy
General User
General User


Joined: 14 Jul 2010
Posts: 10

PostPosted: Mon Jul 26, 2010 12:23 am    Post subject: Reply with quote

Using the following code I'm stuck with custom numberformats. The problem is when checking the equality of the two string (numberformats) they look exactly the same, but the comaprenocase function still returns they are not equal.
Format I'm trying to use is:
str.Format(_T("# ##0,00# %s"), format);

locale is HUN, thousand separator is space, decimal separator is ",", so that's shouldn't be a problem.
I tried converting each string to LPCWSTR or LPCTSTR and then compare, they are still different.


Code:
BOOL COO_Calc_Automation::SetCellNumberFormat(int nCol, int nRow, CString strNumberFormat)
{
   BOOL res = FALSE;
   IDispatch *pdCell = GetCellByPosition(nCol, nRow);
   VARIANT result;
   VariantInit(&result);
   
   if (pdCell!=NULL && TRUE==GetNumberFormats() && TRUE==GetLocaleInfo())
   {
      long lID = GetNumberFormatID(strNumberFormat);
      if (lID<0)      // no match, create new
      {
         if (FALSE == (res = CreateNumberFormat(strNumberFormat, lID)))
            return res;
      }
      ClearAllArgs();
      AddArgumentInt4(NULL, 0, lID);
      res = Invoke (DISPATCH_PROPERTYPUT,
                 pdCell,                  // call the com.sun.star.beans.XPropertySet interface
                 L"NumberFormat",             // API call
                 NULL);                       // [out] (any) returns Property value
   }

   return res;
}

BOOL COO_Calc_Automation::CreateNumberFormat(CString strNumberFormat, long &lNumberFormat)
{
   VARIANT result;
   VariantInit (&result);
   BOOL res = FALSE;

   ClearAllArgs();
   AddArgumentDispatch(NULL, DISPARG_NOFREEVARIANT, m_pdispLocale);      // [in] (object) nLocale
   AddArgumentCString(NULL, 0, strNumberFormat);      // [in] (string) numberFormat to look for

   if (TRUE == (res = Invoke (DISPATCH_METHOD,
              m_pdispNumberFormats,            // call com.sun.star.util.XNumberFormatsSupplier interface
              L"addNew",                // API call
              &result)))                     // [out] (int) method returns the NumberFormat key
   {
      lNumberFormat = result.lVal;
   }
   
   // Check valid form of numberformat really added to avoid exception
   if (lNumberFormat>-1)
   {
      ClearAllArgs();
      VariantInit (&result);
      AddArgumentInt4(NULL, 0, lNumberFormat);
      if (TRUE == ( Invoke (DISPATCH_METHOD,
                 m_pdispNumberFormats,            // call com.sun.star.util.XNumberFormatsSupplier interface
                 L"getByKey",                // API call
                 &result)))                     // [out] (int) method returns the NumberFormat key
      {
         IDispatch *pProp = result.pdispVal;
         CString realFormat;
         VariantInit (&result);
         ClearAllArgs();
         if (TRUE==Invoke(DISPATCH_PROPERTYGET, pProp, L"FormatString", &result))
         {
                                realFormat = result.bstrVal;
            if (realFormat.CompareNoCase((LPCTSTR)strNumberFormat)!=0)
               MessageBox(NULL, _T("Numberformat added is not in valid form, check!!!"), "Failed", MB_OK | MB_ICONSTOP);
            realFormat.GetLength();
            strNumberFormat.GetLength();
         }
      }
   }

   return res;
}
Back to top
View user's profile Send private message
tuxyboy
General User
General User


Joined: 14 Jul 2010
Posts: 10

PostPosted: Mon Aug 02, 2010 3:37 am    Post subject: Reply with quote

Nobody has any idea?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group