# Path: pyDEA/core/data_processing/xlsx_workbook.py
# class XlsxWorkbook(object):
#     ''' This class wraps openpyxl workbook so it can be used by
#         FileWriter class.
# 
#         Attributes:
#             workbook (openpyxl workbook): workbook.
#             nb_sheets (int): number of added work sheets.
#     '''
#     def __init__(self):
#         self.workbook = Workbook()
#         self.nb_sheets = 0
# 
#     def add_sheet(self, sheet_name):
#         ''' Adds one sheet to the workbook.
# 
#             Args:
#                 sheet_name (str): name of the work sheet.
# 
#             Returns:
#                 XlsxSheet: created work sheet.
#         '''
#         self.nb_sheets += 1
#         if self.nb_sheets == 1:
#             worksheet = self.workbook.active
#         else:
#             worksheet = self.workbook.create_sheet()
#         worksheet.title = sheet_name
#         return XlsxSheet(worksheet)
# 
#     def save(self, file_name):
#         ''' Saves workbook to a given file.
# 
#             Args:
#                 file_name (str): name of the file where workbook should be
#                     saved.
#         '''
#         self.workbook.save(file_name)
# 
# class XlsxSheet(object):
#     ''' This class wraps openpyxl work sheet so that it can be used by FileWriter
#         class.
# 
#         Attributes:
#             name (str): work sheet name.
#             worksheet (openpyxl work sheet): worksheet to wrap.
# 
#         Args:
#             worksheet (openpyxl work sheet): worksheet to wrap.
#     '''
#     def __init__(self, worksheet):
#         self.worksheet = worksheet
# 
#     @property
#     def name(self):
#         return self.worksheet.title
# 
#     @name.setter
#     def name(self, value):
#         self.worksheet.title = value
# 
#     def write(self, row, col, value):
#         ''' Writes given value to a cell with specified row and column.
# 
#             Args:
#                 row (int): row index.
#                 col (int): column index.
#                 value (object): value to write.
#         '''
#         # +1 because openpyxl needs rows and cols to start from 1, not from 0
#         cell = self.worksheet.cell(row=row+1, column=col+1)
#         cell.value = value


from openpyxl import load_workbook
from pyDEA.core.data_processing.xlsx_workbook import XlsxWorkbook, XlsxSheet
from pyDEA.core.data_processing.solution_text_writer import OneCsvWriter
import os
            categories (list of str): the first line that will be written
                to file, it must contain category names and might also
                contain name of DMUs as the first element.
            data (list of list of int, float or str): list with data,
                for example

                >> data = [['A', 1, 2, 3], ['B', 2, "a", 5], ['C', 0]]
            sheet_name (str, optional): name of the sheet where the data
                will be written, if the existing sheet name is specified,
                data will be overwritten on this sheet, defaults to "Data".

        Raises:
            ValueError: if sheet_name is empty string or None, or any
                empty value for xlsx files.
    '''
    if data_file.endswith('.xlsx') and not sheet_name:
        raise ValueError('Sheet name is not specified')

    if os.path.isfile(data_file):
        work_book = create_workbook(data_file)
        if data_file.endswith('.xlsx'):
            # copy data from all sheets except the one the user modified if any
            # copy old file and clear sheet to be overwritten with new data
            work_book.workbook = load_workbook(data_file, data_only = True)
            #due to data_only the formulas are replaced by numbers, but otherwise 
            #the file would get useless for pyDEA until opening it again in Excel and recalculating the equations
            work_book.nb_sheets = len(work_book.workbook.sheetnames)
            if sheet_name in work_book.workbook.sheetnames:
                ws = work_book.workbook[sheet_name]
                ws.delete_rows(1, amount = ws.max_row)

# Path: pyDEA/core/data_processing/xlsx_workbook.py
# class XlsxWorkbook(object):
#     ''' This class wraps openpyxl workbook so it can be used by
#         FileWriter class.
# 
#         Attributes:
#             workbook (openpyxl workbook): workbook.
#             nb_sheets (int): number of added work sheets.
#     '''
#     def __init__(self):
#         self.workbook = Workbook()
#         self.nb_sheets = 0
# 
#     def add_sheet(self, sheet_name):
#         ''' Adds one sheet to the workbook.
# 
#             Args:
#                 sheet_name (str): name of the work sheet.
# 
#             Returns:
#                 XlsxSheet: created work sheet.
#         '''
#         self.nb_sheets += 1
#         if self.nb_sheets == 1:
#             worksheet = self.workbook.active
#         else:
#             worksheet = self.workbook.create_sheet()
#         worksheet.title = sheet_name
#         return XlsxSheet(worksheet)
# 
#     def save(self, file_name):
#         ''' Saves workbook to a given file.
# 
#             Args:
#                 file_name (str): name of the file where workbook should be
#                     saved.
#         '''
#         self.workbook.save(file_name)
# 
# class XlsxSheet(object):
#     ''' This class wraps openpyxl work sheet so that it can be used by FileWriter
#         class.
# 
#         Attributes:
#             name (str): work sheet name.
#             worksheet (openpyxl work sheet): worksheet to wrap.
# 
#         Args:
#             worksheet (openpyxl work sheet): worksheet to wrap.
#     '''
#     def __init__(self, worksheet):
#         self.worksheet = worksheet
# 
#     @property
#     def name(self):
#         return self.worksheet.title
# 
#     @name.setter
#     def name(self, value):
#         self.worksheet.title = value
# 
#     def write(self, row, col, value):
#         ''' Writes given value to a cell with specified row and column.
# 
#             Args:
#                 row (int): row index.
#                 col (int): column index.
#                 value (object): value to write.
#         '''
#         # +1 because openpyxl needs rows and cols to start from 1, not from 0
#         cell = self.worksheet.cell(row=row+1, column=col+1)
#         cell.value = value


# Path: pyDEA/core/data_processing/save_data_to_file.py
from openpyxl import load_workbook
from pyDEA.core.data_processing.xlsx_workbook import XlsxWorkbook, XlsxSheet
from pyDEA.core.data_processing.solution_text_writer import OneCsvWriter
import os

            categories (list of str): the first line that will be written
                to file, it must contain category names and might also
                contain name of DMUs as the first element.
            data (list of list of int, float or str): list with data,
                for example

                >> data = [['A', 1, 2, 3], ['B', 2, "a", 5], ['C', 0]]
            sheet_name (str, optional): name of the sheet where the data
                will be written, if the existing sheet name is specified,
                data will be overwritten on this sheet, defaults to "Data".

        Raises:
            ValueError: if sheet_name is empty string or None, or any
                empty value for xlsx files.
    '''
    if data_file.endswith('.xlsx') and not sheet_name:
        raise ValueError('Sheet name is not specified')

    if os.path.isfile(data_file):
        work_book = create_workbook(data_file)
        if data_file.endswith('.xlsx'):
            # copy data from all sheets except the one the user modified if any
            # copy old file and clear sheet to be overwritten with new data
            work_book.workbook = load_workbook(data_file, data_only = True)
            #due to data_only the formulas are replaced by numbers, but otherwise 
            #the file would get useless for pyDEA until opening it again in Excel and recalculating the equations
            work_book.nb_sheets = len(work_book.workbook.sheetnames)
            if sheet_name in work_book.workbook.sheetnames:
                ws = work_book.workbook[sheet_name]
                ws.delete_rows(1, amount = ws.max_row)

                work_sheet = XlsxSheet(ws)