This question is a follow up of this question: Combinations in Calc
Now that @Karolus has explained how to create combinations with python macro, I’m trying to use it to make combinations with three columns. Actually, it’s still two columns and the third column is always the same. Here is an example.
This is in the document at the start:
A B C
Abc 12:34 FIXED
Def 56:78 FIXED
Ghi 90:12 FIXED
Jkl 34:56 FIXED
...
Desired outcome is this:
A B C D E
Abc 12:34 Def 56:78 FIXED
Abc 12:34 Ghi 90:12 FIXED
Abc 12:34 Jkl 34:56 FIXED
Def 56:78 Ghi 90:12 FIXED
Def 56:78 Jkl 34:56 FIXED
Ghi 90:12 Jkl 34:56 FIXED
Here is @Karolus’ solution for two columns:
from itertools import combinations
def combinations_of_two(*_):
"""
combinations of 2 rows from Current Selection
output to the columns right
"""
doc = XSCRIPTCONTEXT.getDocument()
sel = doc.CurrentSelection
data = sel.DataArray
sheet = sel.Spreadsheet
out = tuple( a + b for a, b in combinations( data, 2 ) )
positions = (2, 1, 5, len(out))
target = sheet.getCellRangeByPosition(*positions)
target.setDataArray(out)
How can this be adapted to always add the third column? I tried with changing both occurences of “2” to “3”, but it seems that it’s not referring to the number of columns.
EDIT: I have attached an example of the data I’m working with and how it would be outputted in the ideal case. There are many sets of data and ideally it would notice when there are empty rows and then create another combination.