Page 1 of 1

[Solved] Is there any page count for spreadsheets?

Posted: Thu Feb 28, 2008 11:57 pm
by Villeroy
c.s.s.text.TextDocumentView has a property PageCount. I'm not aware of any property or interface providing a method to get the page count as displayed in the header/footer of a spreadsheet. Is it hidden somewhere or simply not existing?
 Edit: Found c.s.s.sheet.XSheetPageBreak with methods getColumnPageBreaks and getRowPageBreaks. The interface is marked as depricated without apparent alternative. The dev guide suggests using it. I'll test how it interferes with used ranges and set print ranges. 
 Edit: 2008-03-03, answering my own question 
Forget about c.s.s.sheet.XSheetPageBreak and c.s.s.sheet.XPrintAreas. Don't search c.s.s.document.DocumentInfo (although you can see the page count in Menu:File>Properties>Tab:Statistics).
IMHO this is the starting point for further testing:
http://api.openoffice.org/docs/common/r ... dererCount
getRendererCount takes two arguments. The first one should be something renderable from any type of document. I tested the first argument with following spreadsheet related objects:
1. spreadsheet document. The return value is the same as in Menu:File>Properties>Tab:Statistics
2. single range. The return value is the page count when you print with option "Current Selection"
3. single sheet. The return value is the page count when you print with option "Selected Sheets"
4. multiple ranges (c.s.s.sheet.SheetCellRanges). The return value is zero. You've got to use c.s.s.sheet.XPrintAreas.setPrintAreas(<sequence of addresses>) in order to make this work. Then you get the page count from the document.
Annoyance: After setting PrintAreas for one sheet explicitly (GUI: Format>Print Ranges>Define) the other sheets need PrintAreas as well in order to be printable.

The second argument is a sequence of property structs, describing an output device. I passed an empty Basic Array() without testing other options.
Simple Basic wrapper:

Code: Select all

Function getPageCount(oDoc, obj, optional aProps())
if isMissing(aProps()) then aProps() = Array()
	getPageCount = oDoc.getRendererCount(obj, aProps())
End Function

Re: [Solved] Is there any page count for spreadsheets?

Posted: Fri Feb 29, 2008 10:37 am
by TerryE
Placeholder for Change Control and to remove topic from "View unanswered posts" list.

Re: [Solved] Is there any page count for spreadsheets?

Posted: Sat Aug 16, 2014 4:39 am
by duffyd
@Villeroy: Thanks for the excellent writeup. I'm trying to achieve the same in Python but can't get it to work. My code is below:

Code: Select all

doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.CurrentController.getActiveSheet()
...
# Setup the print area
# sheetrow is just a dynamic variable that is calculated based on how many
# rows of data we've inserted into the sheet
selectedcells = sheet.getCellRangeByPosition(0, 0, addresscolstart+5, sheetrow)
pagecount = doc.getRendererCount(selectedcells, PropertyValue("My page count", -1, "", 0))
When I run the code I get the following error:

Code: Select all

com.sun.star.uno.Exceptionconversion not possible! (Error during invoking function importMapData in module file:///Applications/LibreOffice.app/Contents/share/Scripts/python/ImportMapData.py (<class 'ooo_script_framework.com.sun.star.script.CannotConvertException'>: conversion not possible!
  /Applications/LibreOffice.app/Contents/share/Scripts/python/ImportMapData.py:255 in function importMapData() [pagecount = doc.getRendererCount(selectedcells, PropertyValue("My page count", -1, "", 0))]
  /Applications/LibreOffice.app/Contents/MacOS/pythonscript.py:869 in function invoke() [ret = self.func( *args )]
))
I've also tried inserting 'sheet' as per the variable defined in the code sample above and get the same error. Any ideas what I'm doing wrong?

Thanks,
Tim

Re: [Solved] Is there any page count for spreadsheets?

Posted: Sat Aug 16, 2014 6:08 am
by FJCC
This seems to work

Code: Select all

def pageCount():
  doc = XSCRIPTCONTEXT.getDocument() 
  sheet = doc.CurrentController.getActiveSheet()
  selectedcells = sheet.getCellRangeByPosition(0, 0, 5, 100)
 #pagecount = doc.getRendererCount(selectedcells, PropertyValue("My page count", -1, "", 0)) 
  pagecount = doc.getRendererCount(selectedcells, () )
  oCell = sheet.getCellRangeByName("A1")
  oCell.Value = pagecount 
Notice I used an empty tuple as the second argument of getRendererCount().
 Edit: I think the valid options to pass to getRendererCount() are shown here: http://www.openoffice.org/api/docs/comm ... tions.html 

Re: [Solved] Is there any page count for spreadsheets?

Posted: Sat Aug 16, 2014 7:12 am
by FJCC
Here is a Python version where I pass one of the RenderOptions

Code: Select all

import uno

from com.sun.star.beans import PropertyValue

def pageCount():
  doc = XSCRIPTCONTEXT.getDocument() 
  sheet = doc.CurrentController.getActiveSheet()
  selectedcells = sheet.getCellRangeByPosition(0, 0, 5, 100)
 #pagecount = doc.getRendererCount(selectedcells, PropertyValue("My page count", -1, "", 0)) 
  PV = PropertyValue()
  PV.Name = "IsSkipEmptyPages"
  PV.Value = True
  pagecount = doc.getRendererCount(selectedcells, (PV,) )
  oCell = sheet.getCellRangeByName("A1")
  oCell.Value = pagecount 

Re: [Solved] Is there any page count for spreadsheets?

Posted: Sat Aug 16, 2014 10:51 am
by duffyd
The empty tuple worked like a treat. I'd tried an empty list but that didn't work. Hadn't tried putting the PropertyValue into a tuple either. This was the last remaining thing I needed to complete my spreadsheet macro for my app so I'm very indebted for your help!!

Re: [Solved] Is there any page count for spreadsheets?

Posted: Sat Aug 16, 2014 11:08 am
by Villeroy
Whenever you get kind of "array" from your Python program to UNO, you always get a tuple from the PyUNO bridge.
Whenever you pass kind of "array" from UNO to your Python program, you always pass a tuple over the PyUNO bridge.

The equivalent to the above Basic function getPageCount in Python is:

Code: Select all

def getPageCount(doc, sel, *props):
    return doc.getRendererCount(sel, props)
Tuples (arrays, sequences) of property structs are the unified way how you can pass variable amounts of optional, named arguments to UNO from any langauge. In case of no arguments, you need to pass an empty tuple (array, sequence) from your programming langauge. As far as I know, you can not completely leave out arguments in UNO.