ggreen86 / XLSX-Workbook-Class

VFP Class to Create an XLSX Workbook without Excel Automation or Installed
45 stars 16 forks source link

Print_Area and Print_Titles #74

Closed zulqasar closed 1 year ago

zulqasar commented 2 years ago

Print_Area and Print_Titles can be integrated into this awesome library by adding 2 methods:

METHOD setPrintArea

*-- tnEndRow (optional): will default to the last row in the sheet
*-- tnEndCol (optional): will default to the maxColumnNumber in the sheet
*-- tnBegRow and tnBegCol (optional): will default to 1 and 1
lparameters tnWB, tnSh, tnEndRow, tnEndCol, tnBegRow, tnBegCol

tnBegRow = evl(m.tnBegRow, 1)
tnBegCol = evl(m.tnBegCol, 1)
tnEndRow = evl(m.tnEndRow, this.GetLastRowNumber(m.tnWB, m.tnSh)
tnEndCol = evl(m.tnEndCol, this.GetMaxColumnNumber(m.tnWB, m.tnSh)

this.AddNamedRange(;
    m.tnWB, m.tnSh, 'Print_Area', SCOPE_SH_NAMED_RANGE, ;
    '', m.tnBegRow, m.tnBegCol, m.tnEndRow, m.tnEndCol ;
)

METHOD setPrintTitles

*-- rows to repeat = tnBegRow, tnEndRow
*-- cols to repeat = tnBegCol, tnEndCol
lparameters tnWB, tnSh, tnBegRow, tnEndRow, tnBegCol, tnEndCol

if empty(m.tnBegRow) and empty(m.tnEndRow) and empty(m.tnBegCol) and empty(m.tnEndCol)
    messagebox('Invalid params. blah blah')
    return .F.
endif

tnBegRow = evl(m.tnBegRow, m.tnEndRow)
tnEndRow = evl(m.tnEndRow, m.tnBegRow)
tnBegCol = evl(m.tnBegCol, m.tnEndCol)
tnEndCol = evl(m.tnEndCol, m.tnBegCol)

this.AddNamedRange(;
    m.tnWB, m.tnSh, 'Print_Titles', SCOPE_SH_NAMED_RANGE, ;
    '', m.tnBegRow, m.tnBegCol, m.tnEndRow, m.tnEndCol ;
)

LASTLY, changes to writeWorkBookXml method


*-- Note: definedName for Print_Titles:
*.           Rows to repeat on top and columns to repeat on left are separated by a comma
*.          for example: 'My Sheet'!$1:$1,'My Sheet'!$A:$C

*-- add to Locals
local lcSep, lcName, lcSheetName

*-- In the section write the named range info after line 63 add this code
lcName = alltrim(xl_namerange.rname)
if inlist(upper(m.lcName), 'PRINT_AREA', 'PRINT_TITLES')
    lcName = '_xlnm.' + m.lcName
endif

*-- replace line 64 with this
fwrite(lhFile, '<definedName name="' + m.lcName + '"')

*-- Line 69 requires correction (as was posted in issue # 72)
FWRITE(lhFile, ' localSheetId="' + TRANSFORM(xl_namerange.sheet - 1) + '"')

*-- replace line 74 with this one
lcSheetName = IIF(OCCURS(" ", lcSheetName)>0, "'", "") + lcSheetName + IIF(OCCURS(" ", lcSheetName)>0, "'", "")

*-- insert these lines after line 74
if upper(m.lcName) == '_XLNM.PRINT_TITLES'
    lcSep = ''
    if xl_namerange.begrow > 0 
        FWRITE(lhFile, lcSheetName)
        fwrite(lhFile, '!$' + transform(xl_namerange.begrow) + ':$' + transform(xl_namerange.endrow))
        lcSep = ','
    endif

    if xl_namerange.begcol > 0
        fwrite(lhFile, m.lcSep + lcSheetname)
        fwrite(lhFile, '!$' + this.ColumnIndextoascii(xl_namerange.begcol) +  ;
                       ':$' + thiis.Columnindextoascii(xl_namerange.endcol))
    endif
else
    FWRITE(lhFile, lcSheetName) 
       *-- do case .....
ggreen86 commented 1 year ago

Three new methods have been introduced in Release 35 for handling the Set Print Area for a sheet.