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