diff options
author | Samuel Mehrbrodt <samuel.mehrbrodt@allotropia.de> | 2021-11-23 09:43:35 +0100 |
---|---|---|
committer | Samuel Mehrbrodt <samuel.mehrbrodt@allotropia.de> | 2021-11-23 11:11:50 +0100 |
commit | acd820a47e90ceb9404f05dbc9f6c040bd7a9fee (patch) | |
tree | 293c257579b71920da71116934b8094f5e7a2df4 /scripting/examples | |
parent | 324daa38a823477e1d1b650b5d91f3dc91c6a9af (diff) |
Add more beanshell samples
For Writer & Calc
Change-Id: I85815acf11b750ec76d138ff8fe72cc4791e9038
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125689
Tested-by: Jenkins
Reviewed-by: Samuel Mehrbrodt <samuel.mehrbrodt@allotropia.de>
Diffstat (limited to 'scripting/examples')
-rw-r--r-- | scripting/examples/beanshell/Calc/CopyRange.bsh | 39 | ||||
-rw-r--r-- | scripting/examples/beanshell/Calc/FixView.bsh | 34 | ||||
-rw-r--r-- | scripting/examples/beanshell/Calc/InsertSheet.bsh | 25 | ||||
-rw-r--r-- | scripting/examples/beanshell/Calc/ProtectSheet.bsh | 30 | ||||
-rw-r--r-- | scripting/examples/beanshell/Calc/SelectCell.bsh | 30 | ||||
-rw-r--r-- | scripting/examples/beanshell/Calc/parcel-descriptor.xml | 2 | ||||
-rw-r--r-- | scripting/examples/beanshell/Writer/ChangeFont.bsh | 36 | ||||
-rw-r--r-- | scripting/examples/beanshell/Writer/ChangeParaAdjust.bsh | 37 | ||||
-rw-r--r-- | scripting/examples/beanshell/Writer/InsertTable.bsh | 32 | ||||
-rw-r--r-- | scripting/examples/beanshell/Writer/InsertText.bsh | 28 | ||||
-rw-r--r-- | scripting/examples/beanshell/Writer/SetText.bsh | 21 | ||||
-rw-r--r-- | scripting/examples/beanshell/Writer/parcel-descriptor.xml | 2 |
12 files changed, 316 insertions, 0 deletions
diff --git a/scripting/examples/beanshell/Calc/CopyRange.bsh b/scripting/examples/beanshell/Calc/CopyRange.bsh new file mode 100644 index 000000000000..d1e7a49f1b43 --- /dev/null +++ b/scripting/examples/beanshell/Calc/CopyRange.bsh @@ -0,0 +1,39 @@ +/* + * 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/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.container.XIndexAccess; +import com.sun.star.sheet.XCellAddressable; +import com.sun.star.sheet.XCellRangeAddressable; +import com.sun.star.table.CellAddress; +import com.sun.star.table.CellRangeAddress; +import com.sun.star.sheet.XCellRangeMovement; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); +XIndexAccess xSheetsIA = UnoRuntime.queryInterface(XIndexAccess.class, xSheets); +XSpreadsheet xSheet = UnoRuntime.queryInterface(com.sun.star.sheet.XSpreadsheet.class, xSheetsIA.getByIndex(0)); + +XCellRangeAddressable xAddr1 = UnoRuntime.queryInterface(XCellRangeAddressable.class, xSheet.getCellRangeByName("A1:A10") ); +CellRangeAddress source = xAddr1.getRangeAddress(); + +XCellAddressable xAddr2 = UnoRuntime.queryInterface(XCellAddressable.class, xSheet.getCellRangeByName("B1").getCellByPosition( 0, 0 ) ); +CellAddress target = xAddr2.getCellAddress(); + +XCellRangeMovement xCRM = UnoRuntime.queryInterface(XCellRangeMovement.class, xSheet); +xCRM.copyRange(target, source); + +return 0; diff --git a/scripting/examples/beanshell/Calc/FixView.bsh b/scripting/examples/beanshell/Calc/FixView.bsh new file mode 100644 index 000000000000..4ea2c62f9e2f --- /dev/null +++ b/scripting/examples/beanshell/Calc/FixView.bsh @@ -0,0 +1,34 @@ +/* + * 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/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.container.XIndexAccess; +import com.sun.star.sheet.XViewFreezable; +import com.sun.star.sheet.XViewPane; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); +XIndexAccess xSheetsIA = UnoRuntime.queryInterface(XIndexAccess.class, xSheets); +XSpreadsheet xSheet = UnoRuntime.queryInterface(com.sun.star.sheet.XSpreadsheet.class, xSheetsIA.getByIndex(0)); + +XViewFreezable xFreeze = UnoRuntime.queryInterface(XViewFreezable.class, oDoc.getCurrentController() ); +xFreeze.freezeAtPosition(2, 3); + +XViewPane xViewPane = UnoRuntime.queryInterface(XViewPane.class, oDoc.getCurrentController() ); +xViewPane.setFirstVisibleColumn(12); +xViewPane.setFirstVisibleRow(149); + +return 0; diff --git a/scripting/examples/beanshell/Calc/InsertSheet.bsh b/scripting/examples/beanshell/Calc/InsertSheet.bsh new file mode 100644 index 000000000000..ef68f9b3ec9f --- /dev/null +++ b/scripting/examples/beanshell/Calc/InsertSheet.bsh @@ -0,0 +1,25 @@ +/* + * 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/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.lang.XMultiServiceFactory; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); + +xSheets.insertNewByName("First new sheet", (short)0); +xSheets.insertNewByName("Second new sheet", (short)1); + +return 0; diff --git a/scripting/examples/beanshell/Calc/ProtectSheet.bsh b/scripting/examples/beanshell/Calc/ProtectSheet.bsh new file mode 100644 index 000000000000..eea3bd511436 --- /dev/null +++ b/scripting/examples/beanshell/Calc/ProtectSheet.bsh @@ -0,0 +1,30 @@ +/* + * 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/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.container.XIndexAccess; +import com.sun.star.util.XProtectable; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); +XIndexAccess xSheetsIA = UnoRuntime.queryInterface(XIndexAccess.class, xSheets); +XSpreadsheet xSheet = UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsIA.getByIndex(0)); +XProtectable xProtectable = UnoRuntime.queryInterface(XProtectable.class, xSheet); +xProtectable.protect("myPassword"); + +//xProtectable.unprotect("myPassword"); + +return 0; diff --git a/scripting/examples/beanshell/Calc/SelectCell.bsh b/scripting/examples/beanshell/Calc/SelectCell.bsh new file mode 100644 index 000000000000..7d8f5a381177 --- /dev/null +++ b/scripting/examples/beanshell/Calc/SelectCell.bsh @@ -0,0 +1,30 @@ +/* + * 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/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.view.XSelectionSupplier; +import com.sun.star.container.XIndexAccess; +import com.sun.star.table.XCellRange; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); +XIndexAccess xSheetsIA = UnoRuntime.queryInterface(XIndexAccess.class, xSheets); +XSpreadsheet xSheet = UnoRuntime.queryInterface(com.sun.star.sheet.XSpreadsheet.class, xSheetsIA.getByIndex(0)); +XCellRange xResultRange = xSheet.getCellRangeByName("B20"); +XSelectionSupplier xSel = UnoRuntime.queryInterface(XSelectionSupplier.class, oDoc.getCurrentController()); +xSel.select(xResultRange); + +return 0; diff --git a/scripting/examples/beanshell/Calc/parcel-descriptor.xml b/scripting/examples/beanshell/Calc/parcel-descriptor.xml new file mode 100644 index 000000000000..155d53c9bca5 --- /dev/null +++ b/scripting/examples/beanshell/Calc/parcel-descriptor.xml @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?><parcel xmlns:parcel="scripting.dtd" language="BeanShell"> +<script language="BeanShell"><locale lang="en"><displayname value="SelectCell.bsh"/><description>SelectCell.bsh</description></locale><logicalname value="SelectCell.bsh"/><functionname value="SelectCell.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="InsertSheet.bsh"/><description>InsertSheet.bsh</description></locale><logicalname value="InsertSheet.bsh"/><functionname value="InsertSheet.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="ProtectSheet.bsh"/><description>ProtectSheet.bsh</description></locale><logicalname value="ProtectSheet.bsh"/><functionname value="ProtectSheet.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="CopyRange.bsh"/><description>CopyRange.bsh</description></locale><logicalname value="CopyRange.bsh"/><functionname value="CopyRange.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="FixView.bsh"/><description>FixView.bsh</description></locale><logicalname value="FixView.bsh"/><functionname value="FixView.bsh"/></script></parcel>
\ No newline at end of file diff --git a/scripting/examples/beanshell/Writer/ChangeFont.bsh b/scripting/examples/beanshell/Writer/ChangeFont.bsh new file mode 100644 index 000000000000..7cbea8fc3f6b --- /dev/null +++ b/scripting/examples/beanshell/Writer/ChangeFont.bsh @@ -0,0 +1,36 @@ +/* + * 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/. + */ + +import com.sun.star.beans.XPropertySet; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.container.XEnumeration; +import com.sun.star.container.XEnumerationAccess; + +import com.sun.star.text.XTextDocument; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xText = xTextDoc.getText(); +XEnumerationAccess xEnumAcc = (XEnumerationAccess)(UnoRuntime.queryInterface(XEnumerationAccess.class, xText)); +XEnumeration xEnum = xEnumAcc.createEnumeration(); +while (xEnum.hasMoreElements()) { + Object xObj = xEnum.nextElement(); + XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, xObj); + if (xServiceInfo.supportsService("com.sun.star.text.Paragraph")) { + XPropertySet xSet = UnoRuntime.queryInterface(XPropertySet.class, xServiceInfo ); + xSet.setPropertyValue( "CharHeight", 28 ); + xSet.setPropertyValue( "CharFontName", "Liberation Sans" ); + } +} + +return 0; diff --git a/scripting/examples/beanshell/Writer/ChangeParaAdjust.bsh b/scripting/examples/beanshell/Writer/ChangeParaAdjust.bsh new file mode 100644 index 000000000000..db5e5b66a54d --- /dev/null +++ b/scripting/examples/beanshell/Writer/ChangeParaAdjust.bsh @@ -0,0 +1,37 @@ +/* + * 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/. + */ + +import com.sun.star.beans.XPropertySet; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.container.XEnumeration; +import com.sun.star.container.XEnumerationAccess; + +import com.sun.star.text.XTextDocument; +import com.sun.star.text.XText; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xText = xTextDoc.getText(); +XEnumerationAccess xEnumAcc = (XEnumerationAccess)(UnoRuntime.queryInterface(XEnumerationAccess.class, xText)); +XEnumeration xEnum = xEnumAcc.createEnumeration(); +while (xEnum.hasMoreElements()) { + Object xObj = xEnum.nextElement(); + XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, xObj); + if (xServiceInfo.supportsService("com.sun.star.text.Paragraph")) { + XPropertySet xSet = UnoRuntime.queryInterface(XPropertySet.class, xServiceInfo ); + // Set the justification to be center justified + xSet.setPropertyValue( "ParaAdjust", com.sun.star.style.ParagraphAdjust.CENTER ); + } +} + +return 0; diff --git a/scripting/examples/beanshell/Writer/InsertTable.bsh b/scripting/examples/beanshell/Writer/InsertTable.bsh new file mode 100644 index 000000000000..5117dc1e9363 --- /dev/null +++ b/scripting/examples/beanshell/Writer/InsertTable.bsh @@ -0,0 +1,32 @@ +/* + * 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/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.lang.XMultiServiceFactory; + +import com.sun.star.text.XTextDocument; +import com.sun.star.text.XText; +import com.sun.star.text.XTextContent; +import com.sun.star.text.XTextTable; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XMultiServiceFactory xDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc); +Object oTab = xDocMSF.createInstance("com.sun.star.text.TextTable"); +XTextTable xTextTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, oTab); +xTextTable.initialize(4,3); //vier Reihen, drei Spalten +xTextContent = (XTextContent)UnoRuntime.queryInterface(XTextContent.class, xTextTable); + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xText = (XText) UnoRuntime.queryInterface(XText.class, xTextDoc.getText()); +xText.insertTextContent(xText.getEnd(), xTextContent, false); + +return 0; diff --git a/scripting/examples/beanshell/Writer/InsertText.bsh b/scripting/examples/beanshell/Writer/InsertText.bsh new file mode 100644 index 000000000000..a1cfd3566150 --- /dev/null +++ b/scripting/examples/beanshell/Writer/InsertText.bsh @@ -0,0 +1,28 @@ +/* + * 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/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; + +import com.sun.star.text.XTextDocument; +import com.sun.star.text.XText; +import com.sun.star.text.XTextRange; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +String sText = "This text is inserted before the existing text\n" + + "Here comes a second line\n"; + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xText = xTextDoc.getText(); +xTextRange = xText.getEnd(); +xTextRange.setString(sText); + +return 0; diff --git a/scripting/examples/beanshell/Writer/SetText.bsh b/scripting/examples/beanshell/Writer/SetText.bsh new file mode 100644 index 000000000000..99f267c63260 --- /dev/null +++ b/scripting/examples/beanshell/Writer/SetText.bsh @@ -0,0 +1,21 @@ +/* + * 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/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; + +import com.sun.star.text.XTextDocument; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xTextDoc.getText().setString("Hello from Beanshell!"); + +return 0; diff --git a/scripting/examples/beanshell/Writer/parcel-descriptor.xml b/scripting/examples/beanshell/Writer/parcel-descriptor.xml new file mode 100644 index 000000000000..e0f034b3dc26 --- /dev/null +++ b/scripting/examples/beanshell/Writer/parcel-descriptor.xml @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?><parcel xmlns:parcel="scripting.dtd" language="BeanShell"> +<script language="BeanShell"><locale lang="en"><displayname value="InsertText.bsh"/><description>InsertText.bsh</description></locale><logicalname value="InsertText.bsh"/><functionname value="InsertText.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="SetText.bsh"/><description>SetText.bsh</description></locale><logicalname value="SetText.bsh"/><functionname value="SetText.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="InsertTable.bsh"/><description>InsertTable.bsh</description></locale><logicalname value="InsertTable.bsh"/><functionname value="InsertTable.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="ChangeParaAdjust.bsh"/><description>ChangeParaAdjust.bsh</description></locale><logicalname value="ChangeParaAdjust.bsh"/><functionname value="ChangeParaAdjust.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="ChangeFont.bsh"/><description>ChangeFont.bsh</description></locale><logicalname value="ChangeFont.bsh"/><functionname value="ChangeFont.bsh"/></script></parcel>
\ No newline at end of file |