Group by batches and show remainder

I have a situation like this:
1.ods (19.9 KB)
How can i group the b column in batches of 50 and show the remainder after each batch, and use the remainder for the next batch? I need to do that on another column, not to overwrite the data.

from the first 4 rows of data from you example:

Day N boxes
01.03.26 69
02.03.26 90
03.03.26 20
04.03.26 110

this:

Day N boxes
01.03.26 50
01.03.26 19
02.03.26 50
02.03.26 40
03.03.26 20
04.03.26 50
04.03.26 50
04.03.26 10

?? ⇒

def batches(size=50):
    """
    select the source and run me
    """
    doc = XSCRIPTCONTEXT.getDocument()
    header, *data = doc.CurrentSelection.DataArray
    out = [header]
    for date, value in data:
        repeats, value = divmod(value, size)
        out.extend( [ (date, size) ] * int(repeats))
        out.append( (date, value) )
    doc.Sheets['Sheet1'][f'D1:E{len(out)}'].DataArray = out

Hi @emy1, suggestion using formula and arranged horizontally.

emyl_133828.ods (21,5,KB)

@schiavinatto: youre sure?

C2 D2 E2 F2 G2 H2
=MIN(50;B2) =IF(C2=50;MIN(50;$B2-SUM($C2:C2));"") =IF(D2=50;MIN(50;$B2-SUM($C2:D2));"") =IF(E2=50;MIN(50;$B2-SUM($C2:E2));"") =IF(F2=50;MIN(50;$B2-SUM($C2:F2));"") =IF(G2=50;MIN(50;$B2-SUM($C2:G2));"")

???

compare your doc with
emyl_133828_2.ods (22.8 KB)

1 Like