|
@@ -24,6 +24,7 @@ class ChangeLog:
|
|
|
self.ignore_sheets.append(self.log_sheet_name)
|
|
|
self.case_senstive_ignore = case_sensitive_ignore
|
|
|
self.update_ignore_list()
|
|
|
+ self.headers_column = {}
|
|
|
|
|
|
def update_ignore_list(self):
|
|
|
# Making data inside igonre lists in lower only if case_sensitive_ignore is false, so that it will be later
|
|
@@ -48,9 +49,10 @@ class ChangeLog:
|
|
|
self.check_log_sheet(clone_sheets, clone)
|
|
|
sheets = self.get_sheets(source_sheets, clone_sheets)
|
|
|
logs = self.get_change_logs(sheets, source, clone)
|
|
|
- self.update_change_logs(logs, clone)
|
|
|
- self.update_clone_file(source, clone, source_sheets)
|
|
|
- clone.save(self.clone_file)
|
|
|
+ if logs: # if their is no update in the non ignored data than file won't be updated
|
|
|
+ self.update_change_logs(logs, clone)
|
|
|
+ self.update_clone_file(source, clone, source_sheets)
|
|
|
+ clone.save(self.clone_file)
|
|
|
clone.close()
|
|
|
source.close()
|
|
|
|
|
@@ -127,11 +129,12 @@ class ChangeLog:
|
|
|
# for change log. If the sheet already present in clone file then we delete it and create a new one with exact
|
|
|
# data from source
|
|
|
for sheet in source_sheets:
|
|
|
- try: # checking if the sheet exists in clone file, if it does than we first take its index than delete it
|
|
|
+ if sheet in clone.sheetnames: # checking if the sheet exists in clone file, if it does than we first take its index than delete it
|
|
|
clone_sht = clone.get_sheet_by_name(sheet)
|
|
|
ind = clone.index(clone_sht)
|
|
|
+ ignored_headers_data = self.get_ignored_data_from_clone(clone, sheet) # ignored data from clone file
|
|
|
clone.remove(clone_sht)
|
|
|
- except: # if clone file doesn't has that sheet than make the index containing variable to "False"
|
|
|
+ else: # if clone file doesn't has that sheet than make the index containing variable to "False"
|
|
|
ind = "False" # A string is taken instead of any empty value because the original index can contain 0
|
|
|
# which can cause conflict
|
|
|
if ind != "False": # if we have an index value than make a new sheet in that position
|
|
@@ -140,11 +143,20 @@ class ChangeLog:
|
|
|
clone.create_sheet(sheet, len(clone.sheetnames)-2)
|
|
|
source_sht = source.get_sheet_by_name(sheet)
|
|
|
clone_sht = clone.get_sheet_by_name(sheet)
|
|
|
+ skipped = 0
|
|
|
for row in range(1, source_sht.max_row+1): # looping through every row from source
|
|
|
for column in range(1, source_sht.max_column+1): # looping through every column from source
|
|
|
- clone_sht.cell(row, column).value = source_sht.cell(row, column).value #writing everything in clone
|
|
|
- if source_sht.cell(row, column).has_style: # if source cell has style than copy it to clone
|
|
|
- clone_sht.cell(row, column)._style = copy(source_sht.cell(row, column)._style)
|
|
|
+ if sheet in self.headers_column:
|
|
|
+ if column-1 in self.headers_column[sheet]:
|
|
|
+ if row == 1: # if first row then write header
|
|
|
+ clone_sht.cell(row, column).value = self.headers_column[sheet][column-1]
|
|
|
+ elif row > 1: # write data from the ignore data list, column-1 coz the header was index from 0 starting point
|
|
|
+ clone_sht.cell(row, column).value = ignored_headers_data[
|
|
|
+ self.headers_column[sheet][column-1]][row-2] # row-2 because here it starts from 2 and list starts from 0
|
|
|
+ continue
|
|
|
+ clone_sht.cell(row, column).value = source_sht.cell(row, column-skipped).value #writing everything in clone
|
|
|
+ if source_sht.cell(row, column-skipped).has_style: # if source cell has style than copy it to clone
|
|
|
+ clone_sht.cell(row, column-skipped)._style = copy(source_sht.cell(row, column-skipped)._style)
|
|
|
for idx, rd in source_sht.row_dimensions.items(): # copying width and height of rows and columns
|
|
|
clone_sht.row_dimensions[idx] = copy(rd)
|
|
|
|
|
@@ -183,6 +195,7 @@ class ChangeLog:
|
|
|
# columns. Logic is same like get_sheets method
|
|
|
source_headers = [cell.value for cell in source_sht[1]]
|
|
|
clone_headers = [cell.value for cell in clone_sht[1]]
|
|
|
+ self.headers_column[source_sht.title] = {}
|
|
|
headers = []
|
|
|
temp_headers = []
|
|
|
new_headers = []
|
|
@@ -192,8 +205,12 @@ class ChangeLog:
|
|
|
if self.case_senstive_ignore:
|
|
|
if header not in self.ignore_headers:
|
|
|
temp_headers.append(header)
|
|
|
+ else:
|
|
|
+ self.headers_column[source_sht.title][source_headers.index(header)] = header
|
|
|
elif header.lower() not in self.ignore_headers:
|
|
|
temp_headers.append(header)
|
|
|
+ else:
|
|
|
+ self.headers_column[source_sht.title][source_headers.index(header)] = header
|
|
|
else:
|
|
|
temp_headers.append(header)
|
|
|
|
|
@@ -215,3 +232,16 @@ class ChangeLog:
|
|
|
if header not in source_headers:
|
|
|
removed_headers.append(header)
|
|
|
return headers, new_headers, removed_headers
|
|
|
+
|
|
|
+ def get_ignored_data_from_clone(self, clone, sheet):
|
|
|
+ data = {}
|
|
|
+ sht = clone.get_sheet_by_name(sheet)
|
|
|
+ headers = [cell.value for cell in sht[1]]
|
|
|
+ if not sheet in self.headers_column:
|
|
|
+ return data
|
|
|
+ for header in self.headers_column[sheet]:
|
|
|
+ hd = self.headers_column[sheet][header] # getting header from class list storing index of ignored headers
|
|
|
+ data[hd] = []
|
|
|
+ for i in range(2, sht.max_row+1):
|
|
|
+ data[hd].append(sht.cell(i, headers.index(hd)+1).value)
|
|
|
+ return data
|