CellCustomAtributes

From Apache OpenOffice Wiki
Jump to: navigation, search

I created a spreadsheet with a user defined attribute like this....


>>> import Danny.OOo.OOoLib 
>>> from Danny.OOo.OOoLib import * 
>>> oDoc = StarDesktop.loadComponentFromURL( "private:factory/scalc", "_blank", 0, Array() ) 
>>> oSheet = oDoc.getSheets().getByIndex( 0 ) 
>>> oCell = oSheet.getCellByPosition( 2, 5 ) 
>>> oUserDefinedAttributes = oShape.UserDefinedAttributes 
 
Traceback (most recent call last): 
  File "<pyshell#5>", line 1, in -toplevel- 
    oUserDefinedAttributes = oShape.UserDefinedAttributes 
NameError: name 'oShape' is not defined 
>>> oUserDefinedAttributes = oCell.UserDefinedAttributes 
>>> oUserDefinedAttributes.hasByName( "MeowMix" ) 
False 
>>> oMyAttribute = createUnoStruct( "com.sun.star.xml.AttributeData" ) 
>>> oMyAttribute.Type = "CDATA" 
>>> oMyAttribute.Value = "Giddy Gat" 
>>> oUserDefinedAttributes.insertByName( "MeowMix", oMyAttribute ) 
>>> oCell.UserDefinedAttributes = oUserDefinedAttributes 
>>> oDoc.storeToURL( convertToURL( "C:\\Documents and Settings\\dbrewer\\Desktop\Test.sxc" ), Array() ) 
>>> oDoc.close( True ) 
>>>


Please note that it is IMPORTANT to store the collection of user attributes back to the property like so....

oCell.UserDefinedAttributes = oUserDefinedAttributes

in the example above.

Then, using a new python session, I reloaded the document and verified that it had my custom attribute....
>>> from Danny.OOo.OOoLib import * 
>>> oDoc = StarDesktop.loadComponentFromURL( convertToURL( "C:\\Documents and Settings\\dbrewer\\Desktop\Test.sxc" ), "_blank", 0, Array() ) 
>>> oSheet = oDoc.getSheets().getByIndex( 0 ) 
>>> oCell = oSheet.getCellByPosition( 2, 5 ) 
>>> oUserDefinedAttributes = oCell.UserDefinedAttributes 
>>> oUserDefinedAttributes.hasByName( "MeowMix" ) 
True 
>>> oMyAttribute = oUserDefinedAttributes.getByName( "MeowMix" ) 
>>> oMyAttribute.Value 
u'Giddy Gat' 
>>> oDoc.close( True ) 
>>>
Personal tools