Frequently asked questions
- Dates value.
- Custom function/VBA function.
- Dates value
The date cell is the value stored as numeric(double) value with date formatting in excel.
//a double value representing the number of days since 1/1/1900.
//Fractional days represent hours,minutes,and seconds.
double dd = workBook.getNumber(1, 1);//B2 is a date cell.
Console.WriteLine("days since 1900:" + dd);
String ds = workBook.getText(1,1);
//the formatted text as it is showed in Excel.
String dfs = workBook.getFormattedText(1,1);
Console.WriteLine("Formatted text:" + dfs);
//method 1 to set date value
//set cell value with formatted text(mm/dd/yy).
workBook.setEntry(3, 2, "08/08/2009");
//method 2 to set date value
//set the cell with number value.
workBook.setNumber(3,2,40033.0);
//formatting the value to date 'yyyy/mm/dd'
RangeStyle rs = workBook.getRangeStyle();
rs.CustomFormat = "yyyy/mm/dd";
workBook.setRangeStyle(rs, 3,2,3,2);
- Custom function/VBA function
Custom functions are classes that extend the capabilities of the SmartXLS calculate engine. To implement an add-in function, create a class that extends SmartXLS.calc.AddinFunc.
MyDoSum.cs
using System.Collections.Generic;
using System.Text;
using SmartXLS.io;
using Math=System.Math;
namespace SmartXLS
{
public class MyDoSum : SmartXLS.calc.AddinFunc
{
static short valueTypeEmpty = 0;
static short valueTypeError = 4;
static short valueTypeLogical = 3;
static short valueTypeNumber = 1;
static short valueTypeText = 2;
static short valueErrInvalidValue = 3;
/**
* This static initializer guarantees that the instance of this
* function is created and added to the list of add-in functions.
*/
static MyDoSum()
{
new MyDoSum();
}
/**
* Make this private so that only one instance is created
*/
public MyDoSum()
: base("MyDoSum", 1, 30)
{
/* we can allow 1-30 arguments */
/* any argument can be a reference (not just resolved to* a"value"
*/
for (int ii = 1; ii < 30; ii++)
base.ReferenceArgument = ii;
}
/**
* Override the SmartXLS.calc.AddinFunc.evaluate(...) to do the work
*/
public override void evaluate(SmartXLS.calc.AddinFuncContext fc)
{
int argCount = fc.ArgumentCount;
double sum = 0.0; //initialize the result
for (int ii = 0; ii < argCount; ii++)
{
SmartXLS.calc.CalcValue val = fc.getArgument(ii);
if (val.isCell())
{
SmartXLS.ss.Sheet sheet = val.Sheet;
int cellType = System.Math.Abs(sheet.getType(
val.Row1,
val.Col1));
if (cellType == valueTypeNumber)
{
sum += sheet.getNumber(val.Row1,
val.Col1);
}
else if (cellType == valueTypeError)
{
fc.setReturnValue(valueErrInvalidValue);
return;
}
}
else if (val.isRange())
{
SmartXLS.ss.Sheet sheet = val.Sheet;
int row2 = val.Row2;
int col2 = val.Col2;
for (int i = val.Row1; i <= row2; i++)
{
for (int j = val.Col1; j <= col2; j++)
{
int cellType = System.Math.Abs(sheet.getType(i, j));
if (cellType == SmartXLS.ss.Constants.eTypeNumber)
{
sum += sheet.getNumber(i, j);
}
else if (cellType == valueTypeError)
{
fc.setReturnValue(valueErrInvalidValue);
return;
}
}
}
}
else if (val.checkNumber())
{
sum += val.getNumber();
}
else
{
fc.setReturnValue(valueErrInvalidValue);
return;
}
}
fc.setReturnValue(sum);
}
}
}