xref: /trunk/main/scripting/examples/javascript/ExportSheetsToHTML/exportsheetstohtml.js (revision b979193529a53bac9c684a0a7e765235c16a6d91)
1d71964d5SAndrew Rist// *************************************************************
2d71964d5SAndrew Rist//
3d71964d5SAndrew Rist//  Licensed to the Apache Software Foundation (ASF) under one
4d71964d5SAndrew Rist//  or more contributor license agreements.  See the NOTICE file
5d71964d5SAndrew Rist//  distributed with this work for additional information
6d71964d5SAndrew Rist//  regarding copyright ownership.  The ASF licenses this file
7d71964d5SAndrew Rist//  to you under the Apache License, Version 2.0 (the
8d71964d5SAndrew Rist//  "License"); you may not use this file except in compliance
9d71964d5SAndrew Rist//  with the License.  You may obtain a copy of the License at
10d71964d5SAndrew Rist//
11d71964d5SAndrew Rist//    http://www.apache.org/licenses/LICENSE-2.0
12d71964d5SAndrew Rist//
13d71964d5SAndrew Rist//  Unless required by applicable law or agreed to in writing,
14d71964d5SAndrew Rist//  software distributed under the License is distributed on an
15d71964d5SAndrew Rist//  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16d71964d5SAndrew Rist//  KIND, either express or implied.  See the License for the
17d71964d5SAndrew Rist//  specific language governing permissions and limitations
18d71964d5SAndrew Rist//  under the License.
19d71964d5SAndrew Rist//
20d71964d5SAndrew Rist// *************************************************************
21cdf0e10cSrcweir// When this script is run on an existing, saved, spreadsheet,
22cdf0e10cSrcweir// eg. /home/testuser/myspreadsheet.sxc, the script will export
23*499844abSJohn Bampton// each sheet to a separate HTML file,
24cdf0e10cSrcweir// eg. /home/testuser/myspreadsheet_sheet1.html,
25cdf0e10cSrcweir// /home/testuser/myspreadsheet_sheet2.html etc
26cdf0e10cSrcweirimportClass(Packages.com.sun.star.uno.UnoRuntime);
27cdf0e10cSrcweirimportClass(Packages.com.sun.star.sheet.XSpreadsheetDocument);
28cdf0e10cSrcweirimportClass(Packages.com.sun.star.container.XIndexAccess);
29cdf0e10cSrcweirimportClass(Packages.com.sun.star.beans.XPropertySet);
30cdf0e10cSrcweirimportClass(Packages.com.sun.star.beans.PropertyValue);
31cdf0e10cSrcweirimportClass(Packages.com.sun.star.util.XModifiable);
32cdf0e10cSrcweirimportClass(Packages.com.sun.star.frame.XStorable);
33cdf0e10cSrcweirimportClass(Packages.com.sun.star.frame.XModel);
34cdf0e10cSrcweirimportClass(Packages.com.sun.star.uno.AnyConverter);
35cdf0e10cSrcweirimportClass(Packages.com.sun.star.uno.Type);
36cdf0e10cSrcweir
37cdf0e10cSrcweirimportClass(java.lang.System);
38cdf0e10cSrcweir
39cdf0e10cSrcweir//get the document object from the scripting context
40cdf0e10cSrcweiroDoc = XSCRIPTCONTEXT.getDocument();
41cdf0e10cSrcweir//get the XSpreadsheetDocument interface from the document
42cdf0e10cSrcweirxSDoc = UnoRuntime.queryInterface(XSpreadsheetDocument, oDoc);
43cdf0e10cSrcweir//get the XModel interface from the document
44cdf0e10cSrcweirxModel = UnoRuntime.queryInterface(XModel,oDoc);
45cdf0e10cSrcweir//get the XIndexAccess interface used to access each sheet
46cdf0e10cSrcweirxSheetsIndexAccess = UnoRuntime.queryInterface(XIndexAccess, xSDoc.getSheets());
47cdf0e10cSrcweir//get the XStorable interface used to save the document
48cdf0e10cSrcweirxStorable = UnoRuntime.queryInterface(XStorable,xSDoc);
49cdf0e10cSrcweir//get the XModifiable interface used to indicate if the document has been
50cdf0e10cSrcweir//changed
51cdf0e10cSrcweirxModifiable = UnoRuntime.queryInterface(XModifiable,xSDoc);
52cdf0e10cSrcweir
53cdf0e10cSrcweir//set up an array of PropertyValue objects used to save each sheet in the
54cdf0e10cSrcweir//document
55cdf0e10cSrcweirstoreProps = new Array;//PropertyValue[1];
56cdf0e10cSrcweirstoreProps[0] = new PropertyValue();
57cdf0e10cSrcweirstoreProps[0].Name = "FilterName";
58cdf0e10cSrcweirstoreProps[0].Value = "HTML (StarCalc)";
59cdf0e10cSrcweirstoreUrl = xModel.getURL();
60cdf0e10cSrcweirstoreUrl = storeUrl.substring(0,storeUrl.lastIndexOf('.'));
61cdf0e10cSrcweir
62cdf0e10cSrcweir//set only one sheet visible, and store to HTML doc
63cdf0e10cSrcweirfor(var i=0;i<xSheetsIndexAccess.getCount();i++)
64cdf0e10cSrcweir{
65cdf0e10cSrcweir    setAllButOneHidden(xSheetsIndexAccess,i);
66cdf0e10cSrcweir    xModifiable.setModified(false);
67cdf0e10cSrcweir    xStorable.storeToURL(storeUrl+"_sheet"+(i+1)+".html", storeProps);
68cdf0e10cSrcweir}
69cdf0e10cSrcweir
70cdf0e10cSrcweir// now set all visible again
71cdf0e10cSrcweirfor(var i=0;i<xSheetsIndexAccess.getCount();i++)
72cdf0e10cSrcweir{
73cdf0e10cSrcweir    xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(i));
74cdf0e10cSrcweir    xPropSet.setPropertyValue("IsVisible", true);
75cdf0e10cSrcweir}
76cdf0e10cSrcweir
77cdf0e10cSrcweirfunction setAllButOneHidden(xSheetsIndexAccess,vis) {
78cdf0e10cSrcweir    //System.err.println("count="+xSheetsIndexAccess.getCount());
79cdf0e10cSrcweir    //get an XPropertySet interface for the vis-th sheet
80cdf0e10cSrcweir    xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(vis));
81cdf0e10cSrcweir    //set the vis-th sheet to be visible
82cdf0e10cSrcweir    xPropSet.setPropertyValue("IsVisible", true);
83cdf0e10cSrcweir    // set all other sheets to be invisible
84cdf0e10cSrcweir    for(var i=0;i<xSheetsIndexAccess.getCount();i++)
85cdf0e10cSrcweir    {
86cdf0e10cSrcweir        xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(i));
87cdf0e10cSrcweir        if(i!=vis) {
88cdf0e10cSrcweir            xPropSet.setPropertyValue("IsVisible", false);
89cdf0e10cSrcweir        }
90cdf0e10cSrcweir    }
91cdf0e10cSrcweir}
92