question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pageSetup fitToPage, fitToWidth, fitToHeight, scale confusing and documentation is incomplete.

See original GitHub issue

I have been trying to output pages that will print all columns/rows on a page. The package was not operating as I had expected.

When I reverse engineer the .xlsx file I found this data:

Fit to page Attribute(s) from XLSX Attribute(s) that work Comments
All columns scale=XX fitToHeight="0" fitToHeight="0"
All rows scale=XX fitToWidth="0" fitToWidth="0"
One page scale=XX fitToWidth="1" fitToHeight="1" fitToWidth="1" fitToHeight="1"

I think excel adds in the calculated scale for caching, if it doesn’t find the scale then it will simply add it for later saves. The scale setting is ignored in the presence of fitToPage.

As a test, I took the output of a file that had “fit all columns on one page” print setting, the results were: scale="70", fitToPageHeight="0"

I manually changed the scale setting to scale="89"and found that it loaded up in “fit all columns on one page” print mode. Then I saved the file. The value was changed back to scale="70". So I am assuming that scale is ignored and is overridden by fitToPage

I propose that the settings:

fitToPage
fitToWidth
fitToHeight
scale

be emitted in a consistent way with how excel will interpret them.

The fitToPage documentation was a little confusing to me:

name default description
fitToPage Whether to use fitToWidth and fitToHeight or scale settings. Default is based on presence of these settings in the pageSetup object - if both are present, scale wins (i.e. default will be false)

This kind of implies:

  • fitToPage is calculated depending on the values of fitToWidth and fitToHeight.
  • scale overrides fitToPage settings
  • there is no default value

Looking through the code and testing it I do not think either of those statements are true.

Proposal code to add to worksheet-xform.js

    const PS = model.pageSetup;
    if (PS.fitToPage) {
      // if there are page fit constraints, ignore the scale
      PS.scale = undefined;
      if (PS.fitToWidth !== undefined || PS.fitToHeight !== undefined) {
        if (PS.fitToWidth === 0 && PS.fitToHeight === 0) {
          // 0 means unconstrained, this is essentially 100% scale
          // ignore the fitToPage settings because they will imply a weird
          // custom custom scaling which is essentially 100% scaling
          PS.fitToWidth = undefined;
          PS.fitToHeight = undefined;
          PS.fitToPage = false;
        }
      } else {
        // don't assume default settings for these
        PS.fitToWidth = 1;
        PS.fitToHeight = 1;
      }
    } else {
      // Don't output these if fitToPage is not set
      PS.fitToWidth = undefined;
      PS.fitToHeight = undefined;
    }

Update documentation changes with above

Clarify that setting fitToWidth or fitToHeight to 0 essentially says those parameters are not constrained. Clarify that setting both fitToWidth and fitToHeight to 0 is the same as scale=100 and will not emit fitToPage, fitToWidth or fitToColumn. Clarify that fitToPage is a setting that the user controls.

  • if fitToPage is true, then fitToWidth & fitToColumn values will be used, scale will not be emitted.
  • if fittoPage is falsy then fitToPage, fitToWidth & fitToColumn will not be emitted. Add an example of how to use these parameters for common scenarios:
  • Sheet all on one page
  • Sheet columns all on one page
  • Sheet Rows all on one page

PR

I have a code PR ready to go. I can develop the documentation PR pending a discussion here and approval.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
lucas2595commented, Apr 26, 2019

@TonyDobbs I don’t know if you found a solution already, but I managed to do it by setting:

worksheet.pageSetup.fitToPage = true
worksheet.pageSetup.fitToWidth = 1
worksheet.pageSetup.fitToHeight = 0

When setting fitToHeight = 0 it follows the fitToWidth defined and vice-versa.

3reactions
MuyarSPXcommented, May 6, 2020

@TonyDobbs I don’t know if you found a solution already, but I managed to do it by setting:

worksheet.pageSetup.fitToPage = true
worksheet.pageSetup.fitToWidth = 1
worksheet.pageSetup.fitToHeight = 0

When setting fitToHeight = 0 it follows the fitToWidth defined and vice-versa.

It works for me for ‘Fit all columns on one page’. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

PageSetup.FitToPagesWide property (Excel) | Microsoft Learn
In this article. Syntax; Remarks; Example. Returns or sets the number of pages wide that the worksheet will be scaled to when it's...
Read more >
openpyxl Page Setup "Fit To" vs "Adjust To" - Stack Overflow
fitToPage = True prior to setting the fitToHeight and/or fitToWidth properties. You do not need to set the scale percentage explicitly.
Read more >
Release 2.4.9 - openpyxl Documentation
These are advanced properties for particular behaviours, the most used ones are the “fitTopage” page setup property.
Read more >
openpyxl Documentation
cell.font = Font(name='Courier', size=36) ... fitToHeight = 0 ... 的高级属性,最常用的是页面设置参数(page setup property)fitTopage 和定义.
Read more >
https://hosted.assay.co.nz/report/PHPExcel-1.8/cha...
Feature: (Progi1984) Work item 9759 - Implement document properties in ... (ET) Work item 8472 - Support for fitToWidth and fitToHeight pagesetup properties ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found