[Solved] Is there any page count for spreadsheets?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

[Solved] Is there any page count for spreadsheets?

Post 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
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
TerryE
Volunteer
Posts: 1401
Joined: Sat Oct 06, 2007 10:13 pm
Location: UK

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

Post by TerryE »

Placeholder for Change Control and to remove topic from "View unanswered posts" list.
Ubuntu 11.04-x64 + LibreOffice 3 and MS free except the boss's Notebook which runs XP + OOo 3.3.
duffyd
Posts: 2
Joined: Sat Aug 16, 2014 4:25 am

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

Post 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
LibreOffice 4.2.5.2 | OS X 10.9.4
FJCC
Moderator
Posts: 9563
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Post 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 
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
Posts: 9563
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Post 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 
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
duffyd
Posts: 2
Joined: Sat Aug 16, 2014 4:25 am

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

Post 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!!
LibreOffice 4.2.5.2 | OS X 10.9.4
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Post 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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply