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

Formatted cell-text Calc2Writer

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 7649
Location: Germany

PostPosted: Wed Nov 08, 2006 10:43 am    Post subject: Formatted cell-text Calc2Writer Reply with quote

http://www.oooforum.org/forum/viewtopic.phtml?t=45971 describes the problem.
This is how I try to solve the problem. It is extemely ugly code. May be this is because I have not written anything for Writer before. In fact I avoid all MSWordish text processors.
Code:

REM  *****  BASIC  *****
REM Copy cells of selected SheetCellRange into a new writer table, including formatting in cells.
REM Sheet-cells may have portions of formatted text, hyperlink-fields and line breaks.
REM Can anybody, having experience with module com.sun.star.text, clean up this extremely ugly code?
REM Or is this the way it has to be with Writer?
Sub CalcCellsFormattedToWriterCells
   sel = thisComponent.getCurrentController.getSelection
   on error goto errExit
   if sel.supportsService("com.sun.star.sheet.SheetCellRange") then
      doc = StarDesktop.loadComponentFromURL("private:factory/swriter","_default",0,Array())
      doc.lockcontrollers
      tbl = getNewWriterTable(doc,sel.getColumns.getCount,sel.getRows.getCount)
      transferCellsContents sel,tbl
      REM if msgbox ("close?",1)=1 then doc.close(false)
   else
      exit sub
   endif
errExit:
   doc.unlockcontrollers
End Sub
Sub transferCellsContents(range,tbl)
   addr = range.getRangeAddress
   addr.EndColumn = addr.EndColumn - addr.StartColumn
   addr.EndRow = addr.EndRow - addr.StartRow
   addr.StartColumn = 0
   addr.StartRow = 0
'   myxray range.getCellByPosition(c,r).getText
'   exit sub
   for r = 0 to addr.EndRow
      for c = 0 to addr.EndColumn
         sheetcell = range.getCellByPosition(c,r)
         sheetcelltext = sheetcell.getText
         cell = tbl.getCellByPosition(c,r)
         cursor = cell.createTextCursor
         celltext = cursor.getEnd
'         print "celltext.implementationname: "& celltext.implementationname
         eLines = sheetcelltext.createEnumeration
         while eLines.hasmoreelements
            para = eLines.nextElement
'            print "para.implementationname: "& para.implementationname
'            celltext.insertTextContent(cursor.getEnd,para,false)
            eRanges = para.createEnumeration
            while eRanges.hasMoreElements
               rg = eRanges.nextElement
'               print "rg.implementationname: "& rg.implementationname
               sType = rg.TextPortionType
'               myxray rg
'               prps = rg.getPropertySetInfo.getProperties()
               if sType = "Text" then
                  celltext.setString(rg.getString)
                  cloneProperties rg,celltext
               elseif sType = "TextField" then
'                  myxray rg
                  cursor.HyperlinkURL = rg.TextField.URL
                  cursor.HyperlinkName = rg.TextField.Representation
                  cursor.HyperlinkTarget = rg.TextField.TargetFrame
                  celltext.setString(rg.getString)
               endif
               cursor.collapseToEnd
'               print "cursor.implementationname: "& cursor.implementationname
               celltext = cursor.getEnd(true)
'               print "celltext.implementationname: "& celltext.implementationname
            wend
            if eLines.hasMoreElements then
               cell.insertControlCharacter(cursor,com.sun.star.text.ControlCharacter.LINE_BREAK,False)
               cursor.collapseToEnd
               celltext = cursor.getEnd(true)
            endif
         wend
      next
   next
End Sub
function getNewWriterTable(doc,cols, rows)
   tbl = doc.createInstance("com.sun.star.text.TextTable")
   tbl.initialize(rows,cols)
   xrange = doc.getText
   xrange.insertTextContent(xrange,tbl,true)
   getNewWriterTable = tbl
end function

REM (C) Stephan Wunderlich
REM Archived-At: <http://permalink.gmane.org/gmane.comp.openoffice.devel.api/14674>
function cloneProperties(original, clone)
On Error resume next
properties = original.getPropertySetInfo.getProperties
for i=0 to UBound(properties)
    aName = properties(i).Name
    aValue = original.getPropertyValue(aName)
    if (NOT isNull(aValue)) AND (NOT isEmpty(aValue)) then
        clone.setPropertyValue(aName,aValue)
    endif
next
end function

_________________
XUbuntu 9.04, OOo 3.1.1(Sun), Sun Java 1.5.0_06
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 Code Snippets 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