exportsheetstohtml.js 3.7 KB

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