Today''s Page Hits: 155
In ZK Spreadsheet, it provides a convenient way to plug any custom formula into it - use tag library. Only few steps need to do to add a custom formula into ZK Spreadsheet. In following example, I add a simple formula "ECHO" which concatenate all value into a string
1.Write a tag lib functions class file, in those functions you must declare two arguments (Object [] and XelContext) just like following example
public class CustomFunction {
public static Object echo(java.lang.Object[] args, org.zkoss.xel.XelContext ctx){
StringBuffer sb = new StringBuffer();
sb.append("[");
if(args!=null){
for(int i=0;i<args.length;i++){
if(i>0) sb.append(", ");
if(args[i] instanceof RangeRef){
RangeRef ref = (RangeRef)args[i];
for(Iterator iter = ref.getCells().iterator();iter.hasNext();){
Cell cell = (Cell)iter.next();
sb.append(cell.getText());
if(iter.hasNext()) sb.append(", ");
}
}else if(args[i]!=null){
sb.append(args[i].toString());
}
}
}
sb.append("]");
return sb.toString();
}
}
2.Write a tld file(customfunction.tld in this example) in WEB-INF, and point to the function you want to add.
<taglib>
<uri>http://my.custom.function</uri>
<description>Custom Function</description>
<function>
<name>ECHO</name>
<function-class>
my.CustomFunction
</function-class>
<function-signature>
java.lang.Object echo(java.lang.Object[] args,
org.zkoss.xel.XelContext ctx)
</function-signature>
<description></description>
</function>
</taglib>
3.Add tab-lib directive in zul file and leave the prefix attribute a empty string.
<?taglib uri="/WEB-INF/customfunction.tld" prefix=""?>
<zk>
<spreadsheet id="ss1" url="/WEB-INF/empty.xls" maxrows="500" maxcolumns="80" width="90%" height="300px"/>
</zk>
4.Use custom function in ZK Spreadsheet, following is the live demo