As Simple as a Java Static Method"
m (correct highlight (via JWB)) |
|||
(5 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
{{ZKSpreadsheetEssentialsPageHeader}} | {{ZKSpreadsheetEssentialsPageHeader}} | ||
+ | |||
+ | {{Deprecated|url=http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials}} | ||
+ | |||
+ | |||
__TOC__ | __TOC__ | ||
+ | {{ZSS EE}} | ||
ZK Spreadsheet allows developers to write their own custom formula functions. One way of implementing them is as a Java static method & declaring using xel-directive and another one is to implement it as EL function and register it in a TLD. Here we describe the first method. | ZK Spreadsheet allows developers to write their own custom formula functions. One way of implementing them is as a Java static method & declaring using xel-directive and another one is to implement it as EL function and register it in a TLD. Here we describe the first method. | ||
Line 8: | Line 13: | ||
===Implement formula function=== | ===Implement formula function=== | ||
Define your custom formula function as a normal Java static method. For example here is a static method to convert USD to TWD. | Define your custom formula function as a normal Java static method. For example here is a static method to convert USD to TWD. | ||
− | <source lang="java" | + | <source lang="java" highlight="3"> |
public class CurrencyFns { | public class CurrencyFns { | ||
Line 18: | Line 23: | ||
===ZUML=== | ===ZUML=== | ||
− | To be able to use above defined toTWD static method as a custom ZK Spreadsheet formula fuction declare it in your ZUL page using | + | To be able to use above defined toTWD static method as a custom ZK Spreadsheet formula fuction declare it in your ZUL page using [[ZK_ZUML_Reference/The_ZK_User_Interface_Markup_Language/Processing_Instructions/The_xel-method_Directive|the xel-method directive]] along with ZK Spreadsheet component as shown below |
− | <source lang="xml" | + | <source lang="xml" highlight="2,3,4"> |
<?page title="ZSS" contentType="text/html;charset=UTF-8"?> | <?page title="ZSS" contentType="text/html;charset=UTF-8"?> | ||
<?xel-method prefix="zss" name="toTWD" | <?xel-method prefix="zss" name="toTWD" | ||
− | class="org.zkoss. | + | class="org.zkoss.zssessentials.functions.CurrencyFns" |
signature="double toTWD(double,double)"?> | signature="double toTWD(double,double)"?> | ||
<zk> | <zk> | ||
− | <window title="ZSS User Defined Functions" border="normal" | + | <window title="ZSS User Defined Functions as Java static method" border="normal" |
width="100%" height="100%"> | width="100%" height="100%"> | ||
<spreadsheet width="800px" height="800px" | <spreadsheet width="800px" height="800px" | ||
− | src="/ | + | src="/WEB-INF/excel/functions/customfunctions.xlsx" maxrows="20" maxcolumns="10"> |
</spreadsheet> | </spreadsheet> | ||
</window> | </window> | ||
Line 40: | Line 45: | ||
=toTWD(A1,B1) | =toTWD(A1,B1) | ||
</source> | </source> | ||
+ | |||
+ | View complete source of ZUML [http://code.google.com/p/zkbooks/source/browse/trunk/zssessentials/examples/WebContent/functions/javafunction.zul javafunction.zul] | ||
+ | |||
+ | View complete source of function implementation class [http://code.google.com/p/zkbooks/source/browse/trunk/zssessentials/examples/src/org/zkoss/zssessentials/functions/CurrencyFns.java CurrencyFns.java] | ||
=Version History= | =Version History= |
Latest revision as of 12:56, 19 January 2022
This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Spreadsheet_Essentials for more up to date information.
Available in ZK Spreadsheet EE only
ZK Spreadsheet allows developers to write their own custom formula functions. One way of implementing them is as a Java static method & declaring using xel-directive and another one is to implement it as EL function and register it in a TLD. Here we describe the first method.
Purpose
Implement a custom formula function as a Java static method.
Implement formula function
Define your custom formula function as a normal Java static method. For example here is a static method to convert USD to TWD.
public class CurrencyFns {
public static double toTWD(double usdNum, double twdRate) {
return usdNum * twdRate;
}
}
ZUML
To be able to use above defined toTWD static method as a custom ZK Spreadsheet formula fuction declare it in your ZUL page using the xel-method directive along with ZK Spreadsheet component as shown below
<?page title="ZSS" contentType="text/html;charset=UTF-8"?>
<?xel-method prefix="zss" name="toTWD"
class="org.zkoss.zssessentials.functions.CurrencyFns"
signature="double toTWD(double,double)"?>
<zk>
<window title="ZSS User Defined Functions as Java static method" border="normal"
width="100%" height="100%">
<spreadsheet width="800px" height="800px"
src="/WEB-INF/excel/functions/customfunctions.xlsx" maxrows="20" maxcolumns="10">
</spreadsheet>
</window>
</zk>
Note that xel-method prefix attribute value must be set to "zss" for ZK Spreadsheet to invoke custom implementation for such custom formula function.
Using custom formula function in ZK Spreadsheet
Once defined and declared above custom formula function can be used just like any other built-in functions of Excel. Note that when entered in MS Excel it will result in #NAME? being displayed but when entered in ZK Spreadsheet it will we evaluated by invoking custom implementation declared through xel-method directive.
=toTWD(A1,B1)
View complete source of ZUML javafunction.zul
View complete source of function implementation class CurrencyFns.java
Version History
Version | Date | Content |
---|---|---|
All source code listed in this book is at Github.