1 // When this script is run on an existing, saved, spreadsheet,
2 // eg. /home/testuser/myspreadsheet.sxc, the script will export
3 // each sheet to a separate html file,
4 // eg. /home/testuser/myspreadsheet_sheet1.html,
5 // /home/testuser/myspreadsheet_sheet2.html etc
6 importClass(Packages.com.sun.star.uno.UnoRuntime);
7 importClass(Packages.com.sun.star.sheet.XSpreadsheetDocument);
8 importClass(Packages.com.sun.star.container.XIndexAccess);
9 importClass(Packages.com.sun.star.beans.XPropertySet);
10 importClass(Packages.com.sun.star.beans.PropertyValue);
11 importClass(Packages.com.sun.star.util.XModifiable);
12 importClass(Packages.com.sun.star.frame.XStorable);
13 importClass(Packages.com.sun.star.frame.XModel);
14 importClass(Packages.com.sun.star.uno.AnyConverter);
15 importClass(Packages.com.sun.star.uno.Type);
17 importClass(java.lang.System);
19 //get the document object from the scripting context
20 oDoc = XSCRIPTCONTEXT.getDocument();
21 //get the XSpreadsheetDocument interface from the document
22 xSDoc = UnoRuntime.queryInterface(XSpreadsheetDocument, oDoc);
23 //get the XModel interface from the document
24 xModel = UnoRuntime.queryInterface(XModel,oDoc);
25 //get the XIndexAccess interface used to access each sheet
26 xSheetsIndexAccess = UnoRuntime.queryInterface(XIndexAccess, xSDoc.getSheets());
27 //get the XStorable interface used to save the document
28 xStorable = UnoRuntime.queryInterface(XStorable,xSDoc);
29 //get the XModifiable interface used to indicate if the document has been
31 xModifiable = UnoRuntime.queryInterface(XModifiable,xSDoc);
33 //set up an array of PropertyValue objects used to save each sheet in the
35 storeProps = new Array;//PropertyValue[1];
36 storeProps[0] = new PropertyValue();
37 storeProps[0].Name = "FilterName";
38 storeProps[0].Value = "HTML (StarCalc)";
39 storeUrl = xModel.getURL();
40 storeUrl = storeUrl.substring(0,storeUrl.lastIndexOf('.'));
42 //set only one sheet visible, and store to HTML doc
43 for(var i=0;i<xSheetsIndexAccess.getCount();i++)
45 setAllButOneHidden(xSheetsIndexAccess,i);
46 xModifiable.setModified(false);
47 xStorable.storeToURL(storeUrl+"_sheet"+(i+1)+".html", storeProps);
50 // now set all visible again
51 for(var i=0;i<xSheetsIndexAccess.getCount();i++)
53 xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(i));
54 xPropSet.setPropertyValue("IsVisible", true);
57 function setAllButOneHidden(xSheetsIndexAccess,vis) {
58 //System.err.println("count="+xSheetsIndexAccess.getCount());
59 //get an XPropertySet interface for the vis-th sheet
60 xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(vis));
61 //set the vis-th sheet to be visible
62 xPropSet.setPropertyValue("IsVisible", true);
63 // set all other sheets to be invisible
64 for(var i=0;i<xSheetsIndexAccess.getCount();i++)
66 xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(i));
68 xPropSet.setPropertyValue("IsVisible", false);