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
6importClass(Packages.com.sun.star.uno.UnoRuntime);
7importClass(Packages.com.sun.star.sheet.XSpreadsheetDocument);
8importClass(Packages.com.sun.star.container.XIndexAccess);
9importClass(Packages.com.sun.star.beans.XPropertySet);
10importClass(Packages.com.sun.star.beans.PropertyValue);
11importClass(Packages.com.sun.star.util.XModifiable);
12importClass(Packages.com.sun.star.frame.XStorable);
13importClass(Packages.com.sun.star.frame.XModel);
14importClass(Packages.com.sun.star.uno.AnyConverter);
15importClass(Packages.com.sun.star.uno.Type);
16
17importClass(java.lang.System);
18
19//get the document object from the scripting context
20oDoc = XSCRIPTCONTEXT.getDocument();
21//get the XSpreadsheetDocument interface from the document
22xSDoc = UnoRuntime.queryInterface(XSpreadsheetDocument, oDoc);
23//get the XModel interface from the document
24xModel = UnoRuntime.queryInterface(XModel,oDoc);
25//get the XIndexAccess interface used to access each sheet
26xSheetsIndexAccess = UnoRuntime.queryInterface(XIndexAccess, xSDoc.getSheets());
27//get the XStorable interface used to save the document
28xStorable = UnoRuntime.queryInterface(XStorable,xSDoc);
29//get the XModifiable interface used to indicate if the document has been
30//changed
31xModifiable = UnoRuntime.queryInterface(XModifiable,xSDoc);
32
33//set up an array of PropertyValue objects used to save each sheet in the
34//document
35storeProps = new Array;//PropertyValue[1];
36storeProps[0] = new PropertyValue();
37storeProps[0].Name = "FilterName";
38storeProps[0].Value = "HTML (StarCalc)";
39storeUrl = xModel.getURL();
40storeUrl = storeUrl.substring(0,storeUrl.lastIndexOf('.'));
41
42//set only one sheet visible, and store to HTML doc
43for(var i=0;i<xSheetsIndexAccess.getCount();i++)
44{
45	setAllButOneHidden(xSheetsIndexAccess,i);
46	xModifiable.setModified(false);
47	xStorable.storeToURL(storeUrl+"_sheet"+(i+1)+".html", storeProps);
48}
49
50// now set all visible again
51for(var i=0;i<xSheetsIndexAccess.getCount();i++)
52{
53	xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(i));
54	xPropSet.setPropertyValue("IsVisible", true);
55}
56
57function 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++)
65	{
66		xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(i));
67		if(i!=vis) {
68			xPropSet.setPropertyValue("IsVisible", false);
69		}
70	}
71}
72