ES/Desarrollo/Macros

From Apache OpenOffice Wiki
< ES
Revision as of 10:42, 26 December 2007 by Rey buzz (Talk | contribs)

Jump to: navigation, search

Programación en Macros

Ejemplo 1: Copiar Todas las celdas y pegarlas en una hoja nueva

  • Este ejemplo sirve para copiar el contenido de una planilla e insertarla en un segundo documento creado .


Author: Stephan Wunderlich [stephan.wunderlich@sun.com] [basic]


Sub CopySpreadsheet

 firstDoc = ThisComponent

En las siguientes 3 líneas le decimos a OpenOffice que seleccione en esta planilla

todas las celdas cree una nueva hoja y las copie
 selectSheetByName(firstDoc, "Sheet2")
 dispatchURL(firstDoc,".uno:SelectAll")
 dispatchURL(firstDoc,".uno:Copy")
 secondDoc = StarDesktop.loadComponentFromUrl("private:factory/scalc","_blank",0,dimArray())

Luego de copiar las celdas las insertamos en la nuevo hoja

 secondDoc.getSheets().insertNewByName("inserted",0)
 selectSheetByName(secondDoc, "inserted")
 dispatchURL(secondDoc,".uno:Paste")

End Sub


Sub selectSheetByName(document, sheetName)

End Sub

Al copiar y pegar las celdas creamos una función donde le pasamos como argumentos el documento y los parámetros que en este caso son las celdas copiadas y pegadas.

Sub dispatchURL(document, aURL)

 Dim noProps()
 Dim URL As New com.sun.star.util.URL
 frame = document.getCurrentController().getFrame()
 URL.Complete = aURL
 transf = createUnoService("com.sun.star.util.URLTransformer")
transf.parseStrict(URL)
 disp = frame.queryDispatch(URL, "", com.sun.star.frame.FrameSearchFlag.SELF _
        OR com.sun.star.frame.FrameSearchFlag.CHILDREN)
 disp.dispatch(URL, noProps())

End Sub


Ejemplo 2: Set an entire table for optimal column width

Sub SetTableOptimumWidth

 Dim oDispHelper 'Dispatch helper
 Dim oFrame      'Current window frame.
 Dim oTable      'First table in the document.
 Dim oVCursor    'The view cursor
 Dim s$
 oTable = ThisComponent.getTextTables().getByIndex(0)
 ThisComponent.getCurrentController().select(oTable)
 oVCursor = ThisComponent.getCurrentController().getViewCursor()
 oVCursor.gotoEnd(True)
 oVCursor.gotoEnd(True)
 oFrame = ThisComponent.CurrentController.Frame
 oDispHelper = createUnoService("com.sun.star.frame.DispatchHelper")
 s$ = ".uno:SetOptimalColumnWidth"
 oDispHelper.executeDispatch(oFrame, s, "", 0, Array())

End Sub

Personal tools