Ask Your Question
0

create keyboard shortcut to insert a row and copy the line above

asked 2017-05-19 15:52:10 +0100

pdeman gravatar image

updated 2017-05-19 16:13:48 +0100

Hi,

I am trying to do a keyboard shortcut that insert a row in libreoffice calc (I have don't that using tools->customize->keyboard) but I'ld like that the shortcut copy as well the line above (or below, I can deal with both). I don't want to insert a blank line, but to insert a copy of the line above (and if it's possible that it's copying only the two first column of the line above and not the whole line, but I don't think it's possible).

I tried to do a macro, but it always copy the cells that I copied during the recording. (args2(0).Value = "$A$4-$B$4". it doesn't adapt the where I click.

Regards, Pierre

edit retag flag offensive close merge delete

Comments

Recorded macros are never "sensitive" in the sense you expected. Read the code and you see the reason.

Any "awarenes" of a macro for selections or the content of controls requires explicit coding.

Lupp gravatar imageLupp ( 2017-05-19 19:29:28 +0100 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2017-05-19 18:58:35 +0100

Lupp gravatar image

updated 2017-05-19 19:39:59 +0100

Surely there isn't a command for this compound task that you simply can assign to a keyboard shortcut. You first have to create a routine for the purpose. Doing this you need to explicitly specify where the rows shall be inserted.

I made a demo inserting as many rows as currently are selected above the selected range and filling the new rows with the content of as many rows from above the inserted ones.
Considering the specifics it might be preferable to insert below and to take the contents from the rows selected in advance.
Simply adapt the routine as needed.

Please note: If you want to use the Sub, you should copy it from the example document to a module of your 'Standard' library of BASIC macros.

(Edit:) To ease the comparison I add the code of the Sub contained in the above mentioned demo. You will see that the Sub is clearly "row-oriented" and uses the current selection only to define the range of rows for insertion and for the source (complete rows again) for the subsequent filling.

Sub doInsertAndFillRows
REM Thanks to the teachers, in specific to Andrew Pitonyak
REM This Sub was tested under LibO V 5.3.3.
REM It was assigned to Ctrl+Alt+P for the tests.
    Dim theDoc   As Object,  theFrame As Object, theSel     As Object
    Dim theSheet  As Object, theDispH As Object, interimSel As Object
    Dim sR As Long, eR As Long

theDoc    = ThisComponent
theFrame  = theDoc.CurrentController.Frame
If NOT theDoc.SupportsService("com.sun.star.sheet.SpreadsheetDocument") Then
    Exit Sub
End If

theSel    = theDoc.CurrentSelection
If NOT theSel.SupportsService("com.sun.star.sheet.SheetCellRange") Then
    Exit Sub
End If

theSheet  = theDoc.Sheets(theSel.RangeAddress.Sheet)
sR        = theSel.RangeAddress.StartRow
eR        = theSel.RangeAddress.EndRow 
If (sR*2 - eR - 1) < 0 Then
    Exit Sub
End If

theDispH  = CreateUnoService("com.sun.star.frame.DispatchHelper")
theDispH.ExecuteDispatch(theFrame, ".uno:InsertRowsBefore", "", 0, Array())

interimSel= theSheet.GetCellRangeByPosition(0, sR*2 - eR - 1, 1023, sR - 1) '(left, top, right, bottom)
theDoc.CurrentController.Select(interimSel)

theDispH.ExecuteDispatch(theFrame, ".uno:Copy", "", 0, Array())

interimSel = theSheet.GetCellRangeByPosition(0, sR, 1023, eR)
theDoc.CurrentController.Select(interimSel)

theDispH.ExecuteDispatch(theFrame, ".uno:Paste", "", 0, Array())

End Sub
edit flag offensive delete link more
0

answered 2017-05-19 19:18:27 +0100

librebel gravatar image

Hello @pdeman, perhaps you could use the following macro for your purpose:

Sub CopyRangeInsertAfter()
REM Copies the cells of the currently selected Range into a new Range inserted below the selection.
    Dim oSheet, oSelection, oSource, oTarget
    oSheet = ThisComponent.CurrentController.ActiveSheet
    oSelection = ThisComponent.getCurrentSelection()
    oSource = oSelection.getRangeAddress()
    oSheet.Rows.insertByIndex( oSource.EndRow + 1, 1 + oSource.EndRow - oSource.StartRow )
    oTarget = oSheet.getCellByPosition( oSource.StartColumn, oSource.EndRow + 1 ).CellAddress
    oSheet.copyRange( oTarget, oSource )
End Sub
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-05-19 15:52:10 +0100

Seen: 66 times

Last updated: May 19