def plot_tabdef(self, tabdef: TabDef, sheet, x, y, head: TabHead = None):
        height_of_header = 3
        sheet.cell(row=y, column=x).value = tabdef.title or tabdef.name  ## fixme push to tabdef.
        reportingset = tabdef.reportingset
        tabhead_xlsx = TabHead()
        tabhead_xlsx.append(CodePlan(name='TOTAL', data=['TOTAL']))
        for head in tabdef.table_head:
            if isinstance(head, TabHead):
                for cp in head:
                    tabhead_xlsx.append(cp)
            elif isinstance(head, CodePlan):
                tabhead_xlsx.append(head)

        # plot code labels
        code_order = reportingset.split_main.key_order
        for code_count, code in enumerate(code_order):
            print(tabdef.reportingset.split_main.keys())
            code_label = tabdef.reportingset.split_main[str(int(code))]

            sheet.cell(column=x, row=y + height_of_header + code_count).value = code_label

        # plot headers
        head_offset = x + 1
        for head_count, head in enumerate(tabhead_xlsx):
            sheet.cell(row=y, column=head_offset).value = head.title
            for headcode in head.key_order:
                sheet.cell(row=y + 1, column=head_offset).value = head[headcode]
                for column_reporting in self.columns_reporting:
                    sheet.cell(row=y + 2, column=head_offset).value = column_reporting
                    for code_count, code in enumerate(code_order):
                        dataset_name = headcode if headcode == 'TOTAL' else (head.variables[0] + '_'+str(headcode))
                        dataset_relevant = reportingset[dataset_name]
                        try:
                            value_display = dataset_relevant[column_reporting][code]
                        except KeyError:
                            try:
                                value_display = dataset_relevant[column_reporting][str(code)]
                            except KeyError:
                                value_display = 0
                        sheet.cell(row=y + height_of_header + code_count, column=head_offset).value = value_display
                    head_offset += 1
        return len(code_order) + height_of_header
 def split_head(self, head, reportingColumnCount):
     if isinstance(head, CodePlan):  # TODO 2. Bedingung kaputt.
         head = TabHead(head)
     if isinstance(head[0], TabHead):
         return self.split_head(head[0])  # recursive
     elif isinstance(head[0], CodePlan):
         columns_count_max = 14  # TODO relocate!!
         code_count = 1  # Total
         for codeplan_head in head:
             code_count += len(codeplan_head.key_order)
         columns_count_original = code_count * reportingColumnCount
         if columns_count_original <= columns_count_max:  # # Nichts zu tun. Gesamtlänge der Codepläne unter maximumg
             return head
         else:
             newHeads = []
             if len(head) == 1:
                 head_new = TabHead()
                 newHeads.append(head_new)
                 i = 1
                 remainingColmuns = columns_count_max - reportingColumnCount
                 remainingCodes = len(head[0].key_order)
                 try:  # TODO: ausbessern!!
                     newCodePlan = CodePlan(title=head[0].title + " (%d)" % i, variables=head[0].variables)
                 except:
                     newCodePlan = CodePlan(title=head[0].title.encode('utf8') + " (%d)" % i,
                                            variables=head[0].variables)
                 for key in head[0].key_order:
                     remainingColmuns -= reportingColumnCount
                     newCodePlan.data = {}
                     newCodePlan.data[key] = head[0][key]
                     newCodePlan[key] = head[0][key]
                     newCodePlan.key_order.append(key)
                     remainingCodes -= 1
                     if remainingColmuns <= reportingColumnCount and remainingCodes > 0:
                         i += 1
                         remainingColmuns = columns_count_max - reportingColumnCount
                         head_new = TabHead()
                         newHeads.append(head_new)
                         head_new.append(newCodePlan)
                         newCodePlan = CodePlan(title=head[0].title + " (%d)" % i, variables=head[0].variables)
                 head_new.append(newCodePlan)
         return newHeads