fix issue #417 -- creating print titles

#47 Merged at 4f2aea1
Repository
Deleted repository
Branch
johaywood/fix-issue-417-creating-print-titles-1422968353858 (de4dcebe9dd9)
Repository
openpyxl
Branch
2.2
Author
  1. Josh Haywood
Reviewers
Description

fix issue #417 -- creating print titles

Comments (17)

  1. Josh Haywood author

    the writer needs to set localSheetId to the sheet index, not the sheet name to correctly set the scope of a named range. This should correct the issue of the scope always defaulting to workbook instead of the proper worksheet when creating print titles.

    1. CharlieC

      Not quite. The unit test should test independently of the underlying method that a NamedRange with the right values has been created so you should either create a NamedRange using the relevant module, or, and this is preferable simply check the attributes of the object created.

    2. CharlieC

      Please let me know if there is anything in the docs that is unclear. I like to think of testing as little bit like jumping into cold water: nervous trepidation and some thrashing and then suddenly things don't seem so bad.

      1. Josh Haywood author

        I'm sure it probably has more to do with my lack of experience than the docs. Lots of googling going on to figure out how to use tox, etc. But I suppose that's the best way to learn.

        1. CharlieC

          Pretty much. On a unix system you can just do tox and it will run the tests for all the configured environments. Takes a while the first time it runs because lxml has to get installed. But it picks up a lot of bugs on different system before we try and release anything.

          Feel free to ask any questions on the mailing list.

  2. Josh Haywood author

    py.test on the worksheet and workbook modules currently gives no errors, but I'm getting permission errors for writing to the temp folder during the xml portion of the test. Not sure why. Getting tox working properly is another story. I've posted to the group about the issues.

    1. CharlieC

      I get this failure

      _______________________________________________________________________ test_print_titles ________________________________________________________________________
      
      Workbook = <class 'openpyxl.workbook.workbook.Workbook'>
      
          def test_print_titles(Workbook):
              wb = Workbook()
              ws1 = wb.create_sheet()
              ws2 = wb.create_sheet()
              ws1.add_print_title(2)
              ws2.add_print_title(3, rows_or_cols='cols')
      
              def mystr(nr):
                  return ','.join(['%s!%s' % (sheet.title, name) for sheet, name in nr.destinations])
      
              actual_named_ranges = set([(nr.name, nr.scope, mystr(nr)) for nr in wb.get_named_ranges()])
              expected_named_ranges = set([('_xlnm.Print_Titles', ws1, 'Sheet1!$1:$2'),
                                           ('_xlnm.Print_Titles', ws2, 'Sheet2!$A:$C')])
      >       assert(actual_named_ranges == expected_named_ranges)
      E       assert set([('_xlnm....eet2!$A:$C')]) == set([('_xlnm.P...eet1!$1:$2')])
      E         Extra items in the left set:
      E         ('_xlnm.Print_Titles', 0, 'Sheet2!$A:$C')
      E         ('_xlnm.Print_Titles', 0, 'Sheet1!$1:$2')
      E         Extra items in the right set:
      E         ('_xlnm.Print_Titles', <Worksheet "Sheet2">, 'Sheet2!$A:$C')
      E         ('_xlnm.Print_Titles', <Worksheet "Sheet1">, 'Sheet1!$1:$2')
      E         Use -v to get the full diff
      
      Workbook   = <class 'openpyxl.workbook.workbook.Workbook'>
      actual_named_ranges = set([('_xlnm.Print_Titles', 0, 'Sheet1!$1:$2'), ('_xlnm.Print_Titles', 0, 'Sheet2!$A:$C')])
      expected_named_ranges = set([('_xlnm.Print_Titles', <Worksheet "Sheet2">, 'Sheet2!$A:$C'), ('_xlnm.Print_Titles', <Worksheet "Sheet1">, 'Sheet1!$1:$2')])
      mystr      = <function test_print_titles.<locals>.mystr at 0x1047b1950>
      wb         = <openpyxl.workbook.workbook.Workbook object at 0x104a09518>
      ws1        = <Worksheet "Sheet1">
      ws2        = <Worksheet "Sheet2">
      
      ../test_named_range.py:198: AssertionError
      ==================================================================== short test summary info =====================================================================
      FAIL openpyxl/workbook/names/tests/test_named_range.py::test_print_titles
      
      1. Josh Haywood author

        ah, looks like you didn't get my last commit. I updated worksheet.add_print_titles because using ._current_sheet_index always returns index 0 unless it's explicitly updated. Then I corrected the above error to expect the scope to be the index of the sheet rather than the name.

  3. CharlieC

    @Josh Haywood Are you making progress? I'd like to include this in a 2.2 release.

    If you merge in the upstream changes then the weird failures in xmlfile should go away. This is down to the behaviour of NamedTemporaryFile on Windows and is a known bug for which there is no still no resolution apart from manually removing any temporary files. See http://bugs.python.org/issue14243 for the details.

    1. Josh Haywood author

      The changes below to test_named_ranges.py will fix the failure you were getting. My work has picked up recently and I haven't had much of a chance to work on getting tox running properly.