The subtotals currently only get a bold text style, if you want something else, request it in the comments.
And finally here the macro (addSubTotals) you’ll have to insert it into tools → macros → edit macros.
and bind it to for example a shortcut
rem helper
function getRowIdx(cellname as string)
tmp = “”
for i = 1 to len(cellname)
c = mid(cellname, i, 1)
select case c
case “0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”
tmp = tmp & c
end select
next
rem Print tmp
getRowIdx = cint(tmp)
end function
rem helper
function getColIdx(cellname as string)
tmp = “”
for i = 1 to len(cellname)
c = mid(cellname, i, 1)
select case c
case “0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”
case else
tmp = tmp & c
end select
next
rem Print tmp
getColIdx = tmp
end function
sub addSubTotals
oDoc = ThisComponent
oSelRange = oDoc.getCurrentSelection()
oSheet = oDoc.getCurrentController().ActiveSheet
oConv = oDoc.createInstance("com.sun.star.table.CellAddressConversion")
oConv.Address = oSelRange.getCellByPosition( 0, 0 ).getCellAddress()
row_offset = getRowIdx(oConv.UserInterfaceRepresentation)
start_col = getColIdx(oConv.UserInterfaceRepresentation)
oConv.Address = oSelRange.getCellByPosition( oSelRange.Columns.getCount() - 1, 0 ).getCellAddress()
end_col = getColIdx(oConv.UserInterfaceRepresentation)
rem Print start_col & "," & end_col
start=""
group=""
inserts = ""
formulas=""
groups=""
rem iterate through rows
for i = 1 To oSelRange.Rows.getCount() -1
oCell = oSelRange.getCellByPosition( 0, i )
if ( oCell.String <> "" ) then
if ( group <> "") then
rem get last cell of group in last column
oCell2 = oSelRange.getCellByPosition( oSelRange.Columns.getCount() - 1, i - 1 )
oConv.Address = oCell2.getCellAddress()
formulas = start & ":" & oConv.UserInterfaceRepresentation & "," & formulas
inserts = getRowIdx(oConv.UserInterfaceRepresentation) & "," & inserts
endif
rem save group
group=oCell.String
groups= group & "," & groups
rem get first cell of group in last column
oCell2 = oSelRange.getCellByPosition( oSelRange.Columns.getCount() - 1, i )
oConv.Address = oCell2.getCellAddress()
start = oConv.UserInterfaceRepresentation
endif
next
if ( group <> "") then
oCell2 = oSelRange.getCellByPosition( oSelRange.Columns.getCount() - 1, i-1 )
oConv.Address = oCell2.getCellAddress()
formulas = start & ":" & oConv.UserInterfaceRepresentation & "," & formulas
inserts = getRowIdx(oConv.UserInterfaceRepresentation) & "," & inserts
endif
a_ins = Split(inserts,",")
a_forms = Split(formulas,",")
a_groups = Split(groups,",")
rem TODO add grand total
oSheet.Rows.insertByIndex(a_ins(0),1)
cname = start_col & (cint(a_ins(0))+1)
oCell = oSheet.getCellRangeByName(cname)
oCell.String = "Grand Total"
oCell.CharUnderline = 1 rem 0-18 have meaning
oCell.CharWeight = 150
tmp="=0"
for i = lbound(a_ins) to ubound(a_ins) -1
tmp = tmp & "+SUM(" & a_forms(i) & ")"
next
cname = end_col & (cint(a_ins(0))+1)
oCell = oSheet.getCellRangeByName(cname)
oCell.setFormula(tmp)
oCell.CharWeight = 150
oCell.CharUnderline = 1 rem 0-18 have meaning
for i = lbound(a_ins) to ubound(a_ins) -1
oSheet.Rows.insertByIndex(a_ins(i),1)
rem bla = Split(a_forms(i), ":")
cname = start_col & (cint(a_ins(i))+1)
oCell = oSheet.getCellRangeByName(cname)
oCell.String = a_groups(i) & " Result"
oCell.CharUnderline = 1 rem 0-18 have meaning
oCell.CharWeight = 150
cname = end_col & (cint(a_ins(i))+1)
oCell = oSheet.getCellRangeByName(cname)
oCell.setFormula("=SUM(" & a_forms(i) & ")")
oCell.CharWeight = 150
oCell.CharUnderline = 1 rem 0-18 have meaning
next
end sub