summaryrefslogtreecommitdiffstats
path: root/scripting/examples/javascript/ExportSheetsToHTML
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /scripting/examples/javascript/ExportSheetsToHTML
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'scripting/examples/javascript/ExportSheetsToHTML')
-rw-r--r--scripting/examples/javascript/ExportSheetsToHTML/exportsheetstohtml.js88
-rw-r--r--scripting/examples/javascript/ExportSheetsToHTML/parcel-descriptor.xml32
2 files changed, 120 insertions, 0 deletions
diff --git a/scripting/examples/javascript/ExportSheetsToHTML/exportsheetstohtml.js b/scripting/examples/javascript/ExportSheetsToHTML/exportsheetstohtml.js
new file mode 100644
index 000000000..37f9e5afb
--- /dev/null
+++ b/scripting/examples/javascript/ExportSheetsToHTML/exportsheetstohtml.js
@@ -0,0 +1,88 @@
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+// When this script is run on an existing, saved, spreadsheet,
+// eg. /home/testuser/myspreadsheet.sxc, the script will export
+// each sheet to a separate html file,
+// eg. /home/testuser/myspreadsheet_sheet1.html,
+// /home/testuser/myspreadsheet_sheet2.html etc
+importClass(Packages.com.sun.star.uno.UnoRuntime);
+importClass(Packages.com.sun.star.sheet.XSpreadsheetDocument);
+importClass(Packages.com.sun.star.container.XIndexAccess);
+importClass(Packages.com.sun.star.beans.XPropertySet);
+importClass(Packages.com.sun.star.beans.PropertyValue);
+importClass(Packages.com.sun.star.util.XModifiable);
+importClass(Packages.com.sun.star.frame.XStorable);
+importClass(Packages.com.sun.star.frame.XModel);
+importClass(Packages.com.sun.star.uno.AnyConverter);
+importClass(Packages.com.sun.star.uno.Type);
+
+importClass(java.lang.System);
+
+//get the document object from the scripting context
+oDoc = XSCRIPTCONTEXT.getDocument();
+//get the XSpreadsheetDocument interface from the document
+xSDoc = UnoRuntime.queryInterface(XSpreadsheetDocument, oDoc);
+//get the XModel interface from the document
+xModel = UnoRuntime.queryInterface(XModel,oDoc);
+//get the XIndexAccess interface used to access each sheet
+xSheetsIndexAccess = UnoRuntime.queryInterface(XIndexAccess, xSDoc.getSheets());
+//get the XStorable interface used to save the document
+xStorable = UnoRuntime.queryInterface(XStorable,xSDoc);
+//get the XModifiable interface used to indicate if the document has been
+//changed
+xModifiable = UnoRuntime.queryInterface(XModifiable,xSDoc);
+
+//set up an array of PropertyValue objects used to save each sheet in the
+//document
+storeProps = new Array;//PropertyValue[1];
+storeProps[0] = new PropertyValue();
+storeProps[0].Name = "FilterName";
+storeProps[0].Value = "HTML (StarCalc)";
+storeUrl = xModel.getURL();
+storeUrl = storeUrl.substring(0,storeUrl.lastIndexOf('.'));
+
+//set only one sheet visible, and store to HTML doc
+for(var i=0;i<xSheetsIndexAccess.getCount();i++)
+{
+ setAllButOneHidden(xSheetsIndexAccess,i);
+ xModifiable.setModified(false);
+ xStorable.storeToURL(storeUrl+"_sheet"+(i+1)+".html", storeProps);
+}
+
+// now set all visible again
+for(var i=0;i<xSheetsIndexAccess.getCount();i++)
+{
+ xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(i));
+ xPropSet.setPropertyValue("IsVisible", true);
+}
+
+function setAllButOneHidden(xSheetsIndexAccess,vis) {
+ //System.err.println("count="+xSheetsIndexAccess.getCount());
+ //get an XPropertySet interface for the vis-th sheet
+ xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(vis));
+ //set the vis-th sheet to be visible
+ xPropSet.setPropertyValue("IsVisible", true);
+ // set all other sheets to be invisible
+ for(var i=0;i<xSheetsIndexAccess.getCount();i++)
+ {
+ xPropSet = AnyConverter.toObject( new Type(XPropertySet), xSheetsIndexAccess.getByIndex(i));
+ if(i!=vis) {
+ xPropSet.setPropertyValue("IsVisible", false);
+ }
+ }
+}
diff --git a/scripting/examples/javascript/ExportSheetsToHTML/parcel-descriptor.xml b/scripting/examples/javascript/ExportSheetsToHTML/parcel-descriptor.xml
new file mode 100644
index 000000000..be8880ab0
--- /dev/null
+++ b/scripting/examples/javascript/ExportSheetsToHTML/parcel-descriptor.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+-->
+<parcel language="JavaScript" xmlns:parcel="scripting.dtd">
+
+ <script language="JavaScript">
+ <locale lang="en">
+ <displayname value="ExportSheetsToHTML"/>
+ <description>
+ Saves each sheet in the current Calc document as a separate HTML file in the same directory as the original Calc document.
+ </description>
+ </locale>
+ <functionname value="exportsheetstohtml.js"/>
+ <logicalname value="ExportSheetsToHTML.JavaScript"/>
+ </script>
+
+</parcel>