von SchmuckerJunker » Fr, 08.05.2015 15:12
Hallo,
vielen Dank für eure Antworten, dass hat mir sehr weiter geholfen. Ich habe in meiner Rechnung in jeder Zeile ganz rechts eine Formel in einer geschützten Zelle, die Anzahl * Preis berechnet. Da funktioniert das mit dem Einfügen anscheinend nicht automatisch. Daher habe ich mir in den letzten Tagen mal die Basic-Grundlagen angeschaut und ein Makro gebastelt, welches dynamisch Rechnungspositionen hinzufügt bzw. entfernt und auch darauf achtet, ob man sich in einer gültigen Positions-Zeile befindet:
Code: Alles auswählen
sub tab_rechnungs_position_row_add
Dim oTextTables As Variant
Dim oTextTable As Variant
Dim oCells As Variant
Dim oTableRows as object
Dim iTableInsertPosition as Integer
Dim oDocument as object
Dim oDispatcher as object
Dim args1(0) as new com.sun.star.beans.PropertyValue
Dim formula_string as String
oViewCursor = ThisComponent.CurrentController.getViewCursor
iTableInsertPosition = CInt(Mid(oViewCursor.Cell.CellName,2,10))
oTextTables = ThisComponent.getTextTables()
oTextTable = oTextTables.getByName("tab_rechnungs_positionen")
oTableRows = oTextTable.getRows
iTableLastPosition = oTableRows.getCount - 4
if (iTableInsertPosition<2) OR (iTableInsertPosition>iTableLastPosition) Then
Exit Sub
End If
oDocument = ThisComponent.CurrentController.Frame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oCells = oTextTable.getCellRangeByPosition(0,iTableInsertPosition-1,3,iTableInsertPosition-1)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:Copy", "", 0, Array())
oCells = oTextTable.getCellByPosition(0,iTableInsertPosition-1)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:InsertRows", "", 0, Array())
oCells = oTextTable.getCellRangeByPosition(0,iTableInsertPosition,3,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:UnsetCellsReadOnly", "", 0, Array())
oDispatcher.executeDispatch(oDocument, ".uno:Paste", "", 0, Array())
oCells = oTextTable.getCellByPosition(0,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:Delete", "", 0, Array())
oCells = oTextTable.getCellByPosition(2,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:Delete", "", 0, Array())
oCells = oTextTable.getCellByPosition(3,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:Protect", "", 0, Array())
oDispatcher.executeDispatch(oDocument, ".uno:UpdateAll", "", 0, Array())
oCells = oTextTable.getCellByPosition(0,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
end sub
sub tab_rechnungs_position_row_delete
Dim oTextTables As Variant
Dim oTextTable As Variant
Dim oCells As Variant
Dim oTableRows as object
Dim iTableInsertPosition as Integer
Dim oDocument as object
Dim oDispatcher as object
Dim args1(0) as new com.sun.star.beans.PropertyValue
Dim formula_string as String
oViewCursor = ThisComponent.CurrentController.getViewCursor
iTableInsertPosition = CInt(Mid(oViewCursor.Cell.CellName,2,10))
oTextTables = ThisComponent.getTextTables()
oTextTable = oTextTables.getByName("tab_rechnungs_positionen")
oTableRows = oTextTable.getRows
iTableLastPosition = oTableRows.getCount - 4
if (iTableInsertPosition<3) OR (iTableInsertPosition>iTableLastPosition) Then
Exit Sub
End If
oCells = oTextTable.getCellRangeByPosition(0,iTableInsertPosition-1,3,iTableInsertPosition-1)
ThisComponent.CurrentController.select(oCells)
oDocument = ThisComponent.CurrentController.Frame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oDispatcher.executeDispatch(oDocument, ".uno:UnsetCellsReadOnly", "", 0, Array())
oDispatcher.executeDispatch(oDocument, ".uno:DeleteRows", "", 0, Array())
oDispatcher.executeDispatch(oDocument, ".uno:UpdateAll", "", 0, Array())
oCells = oTextTable.getCellByPosition(0,iTableInsertPosition-2)
ThisComponent.CurrentController.select(oCells)
end sub
In Spalten-Index 0 ist die Anzahl, in Spalten-Index 1 ein String "x" (dient nur als Text), in Spalten-Index 2 der Preis und in Spalten-Index 3 die Zeilen-Formel (z.B. "=<A2>*<C2>"), die in neue Zeilen kopiert werden soll. Die Spalte mit der Aufählung wie in der Rechnung von EGO füge ich später noch hinzu, das ist eine gute Idee. Der Code funktioniert bei mir soweit sehr gut, ich hoffe nur ich habe das nicht viel zu kompliziert gelöst. Falls Ihr Fehler findet oder Verbesserungsvorschläge habt, greife ich die gerne auf.
Viele Grüße
SchmuckerJunker
Hallo,
vielen Dank für eure Antworten, dass hat mir sehr weiter geholfen. Ich habe in meiner Rechnung in jeder Zeile ganz rechts eine Formel in einer geschützten Zelle, die Anzahl * Preis berechnet. Da funktioniert das mit dem Einfügen anscheinend nicht automatisch. Daher habe ich mir in den letzten Tagen mal die Basic-Grundlagen angeschaut und ein Makro gebastelt, welches dynamisch Rechnungspositionen hinzufügt bzw. entfernt und auch darauf achtet, ob man sich in einer gültigen Positions-Zeile befindet:
[code]sub tab_rechnungs_position_row_add
Dim oTextTables As Variant
Dim oTextTable As Variant
Dim oCells As Variant
Dim oTableRows as object
Dim iTableInsertPosition as Integer
Dim oDocument as object
Dim oDispatcher as object
Dim args1(0) as new com.sun.star.beans.PropertyValue
Dim formula_string as String
oViewCursor = ThisComponent.CurrentController.getViewCursor
iTableInsertPosition = CInt(Mid(oViewCursor.Cell.CellName,2,10))
oTextTables = ThisComponent.getTextTables()
oTextTable = oTextTables.getByName("tab_rechnungs_positionen")
oTableRows = oTextTable.getRows
iTableLastPosition = oTableRows.getCount - 4
if (iTableInsertPosition<2) OR (iTableInsertPosition>iTableLastPosition) Then
Exit Sub
End If
oDocument = ThisComponent.CurrentController.Frame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oCells = oTextTable.getCellRangeByPosition(0,iTableInsertPosition-1,3,iTableInsertPosition-1)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:Copy", "", 0, Array())
oCells = oTextTable.getCellByPosition(0,iTableInsertPosition-1)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:InsertRows", "", 0, Array())
oCells = oTextTable.getCellRangeByPosition(0,iTableInsertPosition,3,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:UnsetCellsReadOnly", "", 0, Array())
oDispatcher.executeDispatch(oDocument, ".uno:Paste", "", 0, Array())
oCells = oTextTable.getCellByPosition(0,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:Delete", "", 0, Array())
oCells = oTextTable.getCellByPosition(2,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:Delete", "", 0, Array())
oCells = oTextTable.getCellByPosition(3,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
oDispatcher.executeDispatch(oDocument, ".uno:Protect", "", 0, Array())
oDispatcher.executeDispatch(oDocument, ".uno:UpdateAll", "", 0, Array())
oCells = oTextTable.getCellByPosition(0,iTableInsertPosition)
ThisComponent.CurrentController.select(oCells)
end sub
sub tab_rechnungs_position_row_delete
Dim oTextTables As Variant
Dim oTextTable As Variant
Dim oCells As Variant
Dim oTableRows as object
Dim iTableInsertPosition as Integer
Dim oDocument as object
Dim oDispatcher as object
Dim args1(0) as new com.sun.star.beans.PropertyValue
Dim formula_string as String
oViewCursor = ThisComponent.CurrentController.getViewCursor
iTableInsertPosition = CInt(Mid(oViewCursor.Cell.CellName,2,10))
oTextTables = ThisComponent.getTextTables()
oTextTable = oTextTables.getByName("tab_rechnungs_positionen")
oTableRows = oTextTable.getRows
iTableLastPosition = oTableRows.getCount - 4
if (iTableInsertPosition<3) OR (iTableInsertPosition>iTableLastPosition) Then
Exit Sub
End If
oCells = oTextTable.getCellRangeByPosition(0,iTableInsertPosition-1,3,iTableInsertPosition-1)
ThisComponent.CurrentController.select(oCells)
oDocument = ThisComponent.CurrentController.Frame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oDispatcher.executeDispatch(oDocument, ".uno:UnsetCellsReadOnly", "", 0, Array())
oDispatcher.executeDispatch(oDocument, ".uno:DeleteRows", "", 0, Array())
oDispatcher.executeDispatch(oDocument, ".uno:UpdateAll", "", 0, Array())
oCells = oTextTable.getCellByPosition(0,iTableInsertPosition-2)
ThisComponent.CurrentController.select(oCells)
end sub[/code]
In Spalten-Index 0 ist die Anzahl, in Spalten-Index 1 ein String "x" (dient nur als Text), in Spalten-Index 2 der Preis und in Spalten-Index 3 die Zeilen-Formel (z.B. "=<A2>*<C2>"), die in neue Zeilen kopiert werden soll. Die Spalte mit der Aufählung wie in der Rechnung von EGO füge ich später noch hinzu, das ist eine gute Idee. Der Code funktioniert bei mir soweit sehr gut, ich hoffe nur ich habe das nicht viel zu kompliziert gelöst. Falls Ihr Fehler findet oder Verbesserungsvorschläge habt, greife ich die gerne auf.
Viele Grüße
SchmuckerJunker