Browse Source

Lines work

Akash Singh 3 years ago
parent
commit
166ece04c7
3 changed files with 30 additions and 6 deletions
  1. 25 3
      icopy2xls/__init__.py
  2. 5 1
      ui.py
  3. 0 2
      uimain.py

+ 25 - 3
icopy2xls/__init__.py

@@ -49,7 +49,7 @@ class Mover:
         self.source_sheet = source_sheet
         self.destination_file_path = destination_file_path
         self.destination_sheet = destination_sheet
-        self.lines = lines
+        self.lines = self.process_lines(lines)
 
     def move(self, filters={}, add_missing_columns=False):
         """
@@ -71,8 +71,11 @@ class Mover:
             # again opening destination file as it is updated with the source file columns
             destination = self.read_xlsx(self.destination_file_path, self.destination_sheet)
 
-        end = self.lines or source.nrows  # if number of rows to be considered from source is not predefined the take all
-        new_data = [source.row(row) for row in range(1, end)]  # create a new list of all data and remove the filtered data
+        end = self.lines  # if number of rows to be considered from source is not predefined the take all
+        if not end:
+            for i in range(source.nrows):
+                end.append(i)
+        new_data = [source.row(row) for row in end]  # create a new list of all data and remove the filtered data
         remove_data = []  # rows not matching filter are stored here which is used later to remove data from new_data
         for filter in filters:  # iterate through the dictionary of filter and
             ind = [x.value for x in source.row(0)].index(filter)  # getting index of filter header then use the same index to check data
@@ -116,6 +119,25 @@ class Mover:
                 destination_wb.write(1, col, headers)
         destination_wb.save()
 
+    def process_lines(self, lines):
+        line_lis = []
+        lis = [x.strip() for x in lines.split(',')]
+        for l in lis:
+            if "-" in l:
+                start = int(l.split("-")[0].strip())
+                end = int(l.split("-")[1].strip())
+                for i in range(int(start), int(end)+1):
+                    if i not in line_lis:
+                        line_lis.append(i)
+            elif l.strip().isnumeric():
+                i = int(l.strip())
+                if i not in line_lis:
+                    line_lis.append(i)
+            else:
+                raise BaseException('Lines structure is wrong. For multiple values "," is used & for range "-" is used'\
+                                    'please verify it again!')
+        return line_lis
+
 
 def parse_json(path):
     """

+ 5 - 1
ui.py

@@ -245,10 +245,14 @@ class Ui_MainWindow(QtCore.QObject):
         sizePolicy.setVerticalStretch(0)
         sizePolicy.setHeightForWidth(self.linesInput.sizePolicy().hasHeightForWidth())
         self.linesInput.setSizePolicy(sizePolicy)
-        self.linesInput.setValidator(QtGui.QIntValidator())
+        reg_ex = QtCore.QRegExp(r"([0-9]+(-|,)?)*")
+        input_validator = QtGui.QRegExpValidator(reg_ex, self.linesInput)
+        self.linesInput.setValidator(input_validator)
         self.linesInput.setStyleSheet("background-color: rgb(255, 255, 255);")
         self.linesInput.setMinimumSize(250, 0)
         self.linesInput.setMaximumSize(500, 16777215)
+        self.linesInput.setToolTip('Only integer values are accepted along with "-" & "," \nwhich are used for range & ' \
+                                   'multiple values respectively')
         self.horizontalLayout_5.addWidget(self.linesInput)
         self.addMissingCheckBox = QtWidgets.QCheckBox()
         font = QtGui.QFont()

+ 0 - 2
uimain.py

@@ -257,8 +257,6 @@ class MainWindow(Ui_MainWindow):
         else:
             self.statusbar.showMessage("Running...", 3000)
             lines = self.linesInput.text()
-            if lines:
-                lines = int(lines)
             mover = Mover(source, source_sheet, destination, destination_sheet, lines)
             mover.move(self.filters, self.addMissingCheckBox.isChecked())
             self.statusbar.showMessage("Completed...", 3000)