Apache OpenOffice (AOO) Bugzilla – Issue 127395
setActiveSheet does not work in Hidden mode
Last modified: 2020-04-27 17:33:32 UTC
You can't use the API to process (eg: export as CSV) all sheets in a Calc document opened in hidden mode. See numerous examples of code attempts and queries. Eg: - http://stackoverflow.com/questions/20714857/how-can-i-save-all-sheets-in-a-libreoffice-calc-spreadsheet-from-a-command-line - http://stackoverflow.com/questions/41870205/openoffice-calc-setactivesheet - http://www.linuxjournal.com/files/linuxjournal.com/ufiles/ssconverter.py_.txt - https://github.com/whyleyc/unoconv/blob/master/unoconv (comments/code at line 847)
Why using ActiveSheet property if the document is hidden ? It's a non-sense. Each sheet can be handling with ThisComponent.Sheets and using .getByName or .getByIndex.
(In reply to oooforum (fr) from comment #1) > Why using ActiveSheet property if the document is hidden ? It's a non-sense. > Each sheet can be handling with ThisComponent.Sheets and using .getByName or > .getByIndex. Export to CSV exports only the /active/ sheet. How to export another sheet?
Not reproduce with AOO 4.1.3 and Win7 x64 Sub Main Dim sDoc as String Dim Arg(0) As New com.sun.star.beans.PropertyValue Arg(0).Name = "Hidden" Arg(0).Value = True sDoc = ConvertToURL("C:\Temp\export_csv.ods") oDoc = StarDesktop.loadComponentFromURL(sDoc, "_default", 0, Arg() ) oSheet = oDoc.sheets.GetByIndex(2) oDoc.CurrentController.setActiveSheet(oSheet) msgbox oDoc.CurrentController.ActiveSheet.Name oDoc.close(true) End Sub
These lines opens export_csv.ods in hidden mode and jump to sheet #3 with no problem.
(In reply to oooforum (fr) from comment #3) Please test if you (every time) can really save a sheet other than the first as CSV. Sub csvexport Dim oDoc as Object Dim oSheet as Object Dim sDoc as String Dim outputUrl As String Dim Arg(0) As New com.sun.star.beans.PropertyValue Dim Arg2(0) As New com.sun.star.beans.PropertyValue Arg(0).Name = "Hidden" Arg(0).Value = True Arg2(0).Name = "FilterName" Arg2(0).Value = "Text - txt - csv (StarCalc)" sDoc = ConvertToURL("C:\Temp\export_csv.ods") oDoc = StarDesktop.loadComponentFromURL(sDoc, "_default", 0, Arg() ) oSheet = oDoc.sheets.GetByIndex(2) oDoc.CurrentController.setActiveSheet(oSheet) outputUrl = ConvertToURL( "C:\Temp\" & oSheet.Name & ".csv") 'msgbox oDoc.CurrentController.ActiveSheet.Name oDoc.storeToURL( outputUrl, Arg2() ) oDoc.close(true) End Sub
I'm not sure that setActiveSheet is buggy. If I set filter as "dBase", the code works fine.
Created attachment 86041 [details] Sample python script demonstrating the problem Not sure if this helps, but I am a python-er and I attach oo-save-ods-sheet.py - a butchered subset of the UNOCONV script (ref: https://github.com/dagwieers/unoconv) which I was trying to modify to save .ods sheets as .csv I run on Windows 10 and so, to simplify it, this is hard-wired for that environment. I assume you could make it run elsewhere by changing the variables at the front. In a folder containing oo-save-ods-sheet.py and the 2-sheet oo-save-ods-sheet.ods, I run it using: "C:\Program Files (x86)\OpenOffice 4\program/python.exe" oo-save-ods-sheet.py Change the Hidden= setting on line 92 to illustrate the problem.
So weird, it seems ActiveSheet not compatible _only_ with CSV filter I try with SYLK export and it works too. I found a workaround with dispatcher help. Sub csvexport Dim sDoc as String Dim Arg(0) As New com.sun.star.beans.PropertyValue Dim Args1(0) as new com.sun.star.beans.PropertyValue Dim Args2(0) as new com.sun.star.beans.PropertyValue Arg(0).Name = "Hidden" Arg(0).Value = True sDoc = ConvertToURL("C:\Temp\export_csv.ods") oDoc = StarDesktop.loadComponentFromURL(sDoc, "_default", 0, Arg() ) 'oSheet = oDoc.sheets.GetByIndex(2) 'oDoc.CurrentController.setActiveSheet(oSheet) ' Workaround with these lines oFrame = oDoc.CurrentController.Frame dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") Args1(0).Name = "Tables" Args1(0).Value = Array(2) 'index # for sheet dispatcher.executeDispatch(oFrame, ".uno:SelectTables", "", 0, args1()) Arg2(0).Name = "FilterName" Arg2(0).Value = "Text - txt - csv (StarCalc)" 'Arg2(0).Value = "dBase" outputUrl = ConvertToURL( "C:\Temp\test.csv") 'msgbox oDoc.CurrentController.ActiveSheet.Name oDoc.storeToURL( outputUrl, Arg() ) oDoc.close(true) End Sub I don't know if this issue must be reopen
Minor typo in your code: Arg2(0).Name and Arg2(0).Value assignments near the end should be Arg(0).Name and Arg(0).Value I can see that that works using a Basic macro, but can't get it to work in the Python unoconv.py script. I replaced the document.CurrentController.setActiveSheet(sheet) FOR loop block in my code with: oFrame = document.CurrentController.Frame dispatcher = self.unosvcmgr.createInstanceWithContext("com.sun.star.frame.DispatchHelper", self.unocontext) patchProps = UnoProps(NameError="Tables", Value=tuple([2])) dispatcher.executeDispatch(oFrame, ".uno:SelectTables", "", 0, patchProps) to no avail. So this is still a bug!
> I replaced the document.CurrentController.setActiveSheet(sheet) FOR loop > block in my code with: > > oFrame = document.CurrentController.Frame > dispatcher = > self.unosvcmgr.createInstanceWithContext("com.sun.star.frame.DispatchHelper", > self.unocontext) > patchProps = UnoProps(NameError="Tables", Value=tuple([2])) -------------------------^^^^^^^^^^ Really?
Created attachment 86071 [details] Original .py script hacked further to attempt to add the Basic workaround Oops, my typo this time! But no change after correcting it to: patchProps = UnoProps(Name="Tables", Value=tuple([2])) I attach my updated full script.
Problem still present in 4.1.5
Please DO NOT change the Version field to note that it still does not work in a newer version. That is what the Latest Confirmation in Field is for. THe version field is used to not the first version that the issue was reported against and can be used by developers to narrow down where the problem is in the code. I have re-set the Version field to 4.1.3 and added 4.12.5 as the latest confirmation in Field
Created attachment 86933 [details] Original .py script updated with my workaround An update on this - I have been able to work round the bug using moveByName, instead of setActiveSheet. Using this, I move each required sheet to be first and storeToURL to export it. I don't know if anyone is interested in this work-around, or in a test harness if fixing the underlying bug. I am a python-er and attach an updated version of oo-save-ods-sheet.py, my butchered subset of the UNOCONV script (ref: https://github.com/dagwieers/unoconv) which I was trying to modify to save .ods sheets as .csv Also the optional supporting "expected outcome" file oo-save-ods-sheet-sheet3.csv The script now takes various arguments. If run on Windows with no argument it exports from the (also attached) oo-save-ods-sheet.ods without using Hidden mode. If oo-save-ods-sheet-sheet3.csv is in the same folder it compares the generated file (oo-save-ods-sheet.csv) to it and reports that they are the same (ie: "Sheet3" has been saved). If you add the -Hidden argument, you will see the problem, with oo-save-ods-sheet.csv containing the first sheet ("Bikes"), not the last. I have run it against OpenOffice on Windows 10 and LibreOffice on Ubuntu. It is simplified, with hard-wirings for those environments. I assume you could make it run elsewhere by changing the variables settings towards the end, before the Main entry point. My suggested test sequence is: 1. Run with no args: .../python oo-save-ods-sheet.py .ods will flash on screen before reporting: Export done, created file oo-save-ods-sheet.csv same as oo-save-ods-sheet-sheet3.csv 2. Run: .../python oo-save-ods-sheet.py -Hidden No flash on screen, but reports: Export done, created file oo-save-ods-sheet.csv NOT same as oo-save-ods-sheet-sheet3.csv 3. Run: .../python oo-save-ods-sheet.py -Hidden -workAround Runs OK, as test #1, also reporting: Moved Sheet3 to be first, before: Bikes
Created attachment 86934 [details] Test file for the .py script Attachment described in previous comment
Created attachment 86935 [details] Expected result file Attachment described in previous but one comment