Commit 7b2f009c authored by Dmitry.Shahtanov's avatar Dmitry.Shahtanov Committed by Alexander.Trofimov

разнес функции по отдельным файлам

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@47577 954022d7-b5bf-4e40-9824-e11837661b57
parent 772acfe0
......@@ -53,6 +53,17 @@ SET SRC_TABLE_EGINE=^
"%TABLE_ENGINE%\graphics\pdfprinter.js" ^
"%TABLE_ENGINE%\model\CollaborativeEditing.js" ^
"%TABLE_ENGINE%\model\parserFormula.js" ^
"%TABLE_ENGINE%\model\dateandtimeFunctions.js" ^
"%TABLE_ENGINE%\model\engineeringFunctions.js" ^
"%TABLE_ENGINE%\model\cubeFunctions.js" ^
"%TABLE_ENGINE%\model\databaseFunctions.js" ^
"%TABLE_ENGINE%\model\textanddataFunctions.js" ^
"%TABLE_ENGINE%\model\statisticalFunctions.js" ^
"%TABLE_ENGINE%\model\financialFunctions.js" ^
"%TABLE_ENGINE%\model\mathematicFunctions.js" ^
"%TABLE_ENGINE%\model\lookupandreferenceFunctions.js" ^
"%TABLE_ENGINE%\model\informationFunctions.js" ^
"%TABLE_ENGINE%\model\logicalFunctions.js" ^
"%TABLE_ENGINE%\model\DrawingObjects.js" ^
"%TABLE_ENGINE%\model\NumFormat.js" ^
"%TABLE_ENGINE%\model\Serialize.js" ^
......
......@@ -41,6 +41,17 @@
<script type="text/javascript" src="../model/CollaborativeEditing.js"></script>
<script type="text/javascript" src="../model/parserFormula.js"></script>
<script type="text/javascript" src="../model/dateandtimeFunctions.js"></script>
<script type="text/javascript" src="../model/engineeringFunctions.js"></script>
<script type="text/javascript" src="../model/cubeFunctions.js"></script>
<script type="text/javascript" src="../model/databaseFunctions.js"></script>
<script type="text/javascript" src="../model/textanddataFunctions.js"></script>
<script type="text/javascript" src="../model/statisticalFunctions.js"></script>
<script type="text/javascript" src="../model/financialFunctions.js"></script>
<script type="text/javascript" src="../model/mathematicFunctions.js"></script>
<script type="text/javascript" src="../model/lookupandreferenceFunctions.js"></script>
<script type="text/javascript" src="../model/informationFunctions.js"></script>
<script type="text/javascript" src="../model/logicalFunctions.js"></script>
<script type="text/javascript" src="../model/CellComment.js"></script>
<script type="text/javascript" src="../../Common/NumFormat.js"></script>
<script type="text/javascript" src="../model/Serialize.js"></script>
......@@ -104,9 +115,22 @@
<!--for colors-->
<script src="../../Common/Charts/libraries/hsv.js"></script>
<script src="../../Common/Charts/libraries/rgbcolor.js"></script>
<!--for shapes-->
<script src="../../Word/Drawing/Graphics.js"></script>
<script src="../model/DrawingObjects/DrawingObjectsController.js"></script>
<script src="../model/DrawingObjects/States.js"></script>
<script src="../model/DrawingObjects/Format/GroupShape.js"></script>
<script src="../model/DrawingObjects/Format/Image.js"></script>
<script src="../model/DrawingObjects/Format/Shape.js"></script>
<script src="../model/DrawingObjects/Tracks/ResizeTracks.js"></script>
<script src="../model/DrawingObjects/Tracks/RotateTracks.js"></script>
<script src="../model/DrawingObjects/Tracks/NewShapeTracks.js"></script>
<script src="../model/DrawingObjects/Hit.js"></script>
<script type="text/javascript" src="FormulaTests.js"></script>
</head>
<body>
<h1 id="qunit-header">Parser Formulas tests</h1>
......
......@@ -935,5 +935,104 @@
ok( Math.abs(oParser.calculate().getValue() - pmt(0.08/12,10,10000,0,1)) < dif );
})
test("Test: \"TIMEVALUE\"",function(){
oParser = new parserFormula("timevalue(\"10:02:34\")","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 0.4184490740740740) < dif );
oParser = new parserFormula("timevalue(\"02-01-2006 10:15:29 AM\")","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 0.4274189814823330) < dif );
oParser = new parserFormula("timevalue(\"22:02\")","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 0.9180555555555560) < dif );
})
test("Test: \"WEEKNUM\"",function(){
/* oParser = new parserFormula("WEEKNUM(DATE(2006,1,1))","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 1) < dif );
oParser = new parserFormula("WEEKNUM(DATE(2006,1,1),17)","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 1) < dif );
oParser = new parserFormula("WEEKNUM(DATE(2006,1,1),1)","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 1) < dif );
oParser = new parserFormula("WEEKNUM(DATE(2006,1,1),21)","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 52) < dif );
oParser = new parserFormula("WEEKNUM(DATE(2006,2,1),1)","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 5) < dif );//10
oParser = new parserFormula("WEEKNUM(DATE(2006,2,1),2)","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 6) < dif );//12
oParser = new parserFormula("WEEKNUM(DATE(2006,2,1),11)","A2",ws);
ok(oParser.parse());
ok( Math.abs(oParser.calculate().getValue() - 6) < dif );//14*/
oParser = new parserFormula("WEEKNUM(DATE(2007,1,1),15)","A2",ws);//понед
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 1);
oParser = new parserFormula("WEEKNUM(DATE(2008,1,1),15)","A2",ws);//втор
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 1);
oParser = new parserFormula("WEEKNUM(DATE(2003,1,1),15)","A2",ws);//сред
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 1);
oParser = new parserFormula("WEEKNUM(DATE(2009,1,1),15)","A2",ws);//чет
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 1);
oParser = new parserFormula("WEEKNUM(DATE(2010,1,1),15)","A2",ws);//пят
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 1);
oParser = new parserFormula("WEEKNUM(DATE(2011,1,1),15)","A2",ws);//суб
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 1);
oParser = new parserFormula("WEEKNUM(DATE(2012,1,1),15)","A2",ws);//вск
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 1);
oParser = new parserFormula("WEEKNUM(DATE(2008,1,4),15)","A2",ws);//вск
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 2);
oParser = new parserFormula("WEEKNUM(DATE(2008,1,10),15)","A2",ws);//вск
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 2);
oParser = new parserFormula("WEEKNUM(DATE(2008,1,11),15)","A2",ws);//вск
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 3);
oParser = new parserFormula("WEEKNUM(DATE(2008,1,17),15)","A2",ws);//вск
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 3);
oParser = new parserFormula("WEEKNUM(DATE(2008,1,18),15)","A2",ws);//вск
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 4);
oParser = new parserFormula("WEEKNUM(DATE(2008,1,24),15)","A2",ws);//вск
ok(oParser.parse());
strictEqual( oParser.calculate().getValue(), 4);
})
});
......@@ -66,6 +66,17 @@
<script type="text/javascript" src="model/CollaborativeEditing.js"></script>
<script type="text/javascript" src="model/ConditionalFormatting.js"></script>
<script type="text/javascript" src="model/parserFormula.js"></script>
<script type="text/javascript" src="model/dateandtimeFunctions.js"></script>
<script type="text/javascript" src="model/engineeringFunctions.js"></script>
<script type="text/javascript" src="model/cubeFunctions.js"></script>
<script type="text/javascript" src="model/databaseFunctions.js"></script>
<script type="text/javascript" src="model/textanddataFunctions.js"></script>
<script type="text/javascript" src="model/statisticalFunctions.js"></script>
<script type="text/javascript" src="model/financialFunctions.js"></script>
<script type="text/javascript" src="model/mathematicFunctions.js"></script>
<script type="text/javascript" src="model/lookupandreferenceFunctions.js"></script>
<script type="text/javascript" src="model/informationFunctions.js"></script>
<script type="text/javascript" src="model/logicalFunctions.js"></script>
<script type="text/javascript" src="model/CellComment.js"></script>
<script type="text/javascript" src="../Common/NumFormat.js"></script>
<script type="text/javascript" src="model/Serialize.js"></script>
......
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 15:15
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.Cube = {
'groupName' : "Cube",
'CUBEKPIMEMBER' : function(){
var r = new cBaseFunction("CUBEKPIMEMBER");
return r;
},
'CUBEMEMBER' : function(){
var r = new cBaseFunction("CUBEMEMBER");
return r;
},
'CUBEMEMBERPROPERTY' : function(){
var r = new cBaseFunction("CUBEMEMBERPROPERTY");
return r;
},
'CUBERANKEDMEMBER' : function(){
var r = new cBaseFunction("CUBERANKEDMEMBER");
return r;
},
'CUBESET' : function(){
var r = new cBaseFunction("CUBESET");
return r;
},
'CUBESETCOUNT' : function(){
var r = new cBaseFunction("CUBESETCOUNT");
return r;
},
'CUBEVALUE' : function(){
var r = new cBaseFunction("CUBEVALUE");
return r;
}
}
\ No newline at end of file
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 15:16
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.Database = {
'groupName' : "Database",
'DAVERAGE' : function(){
var r = new cBaseFunction("DAVERAGE");
return r;
},
'DCOUNT' : function(){
var r = new cBaseFunction("DCOUNT");
return r;
},
'DCOUNTA' : function(){
var r = new cBaseFunction("DCOUNTA");
return r;
},
'DGET' : function(){
var r = new cBaseFunction("DGET");
return r;
},
'DMAX' : function(){
var r = new cBaseFunction("DMAX");
return r;
},
'DMIN' : function(){
var r = new cBaseFunction("DMIN");
return r;
},
'DPRODUCT' : function(){
var r = new cBaseFunction("DPRODUCT");
return r;
},
'DSTDEV' : function(){
var r = new cBaseFunction("DSTDEV");
return r;
},
'DSTDEVP' : function(){
var r = new cBaseFunction("DSTDEVP");
return r;
},
'DSUM' : function(){
var r = new cBaseFunction("DSUM");
return r;
},
'DVAR' : function(){
var r = new cBaseFunction("DVAR");
return r;
},
'DVARP' : function(){
var r = new cBaseFunction("DVARP");
return r;
}
}
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 13:24
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.DateAndTime = {
'groupName' : "DateAndTime",
'DATE' : function(){
var r = new cBaseFunction("DATE");
r.setArgumentsMin(3);
r.setArgumentsMax(3);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], year, month, day;
for (var i = 0; i<this.argumentsCurrent;i++){
var arg0 = arg[i];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
else if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
}
if(arg0 instanceof cError){
return this.setCA(arg0,true);
}
else if( arg0 instanceof cNumber || arg0 instanceof cBool){
if(i==0)year = arg0.tocNumber().getValue();
if(i==1)month = arg0.tocNumber().getValue();
if(i==2)day = arg0.tocNumber().getValue();
}
else if ( arg0 instanceof cRef || arg0 instanceof cRef3D ){
var val = arg0.getValue();
if(val instanceof cError) return this.setCA(val,true);
else if( val instanceof cNumber || val instanceof cBool ){
if(i==0)year = arg0.tocNumber().getValue();
if(i==1)month = arg0.tocNumber().getValue();
if(i==2)day = arg0.tocNumber().getValue();
}
else{
return this.setCA(new cError( cErrorType.wrong_value_type ),true);
}
}
else if(arg0 instanceof cString || arg0 instanceof cEmpty){
var val = arg0.tocNumber();
if(val instanceof cError){
return this.setCA(val,true);
}
else {
if(i==0)year = arg0.tocNumber().getValue();
if(i==1)month = arg0.tocNumber().getValue();
if(i==2)day = arg0.tocNumber().getValue();
}
}
}
if( year >= 0 && year <= 1899)
year+=1900;
if( month == 0 ){
return this.setCA(new cError( cErrorType.not_numeric ),true);
}
this.value = new cNumber( Math.round( ( ( new Date(year, month - 1, day) ).getTime()/1000 )/c_sPerDay+(c_DateCorrectConst+1) ) )
this.value.numFormat = 14;
this.value.ca = true;
return this.value;
}
r.getInfo = function(){
return {
name:this.name,
args:"( year, month, day )"
};
}
return r;
},
'DATEDIF' : function(){
var r = new cBaseFunction("DATEDIF");
return r;
},
'DATEVALUE' : function(){
var r = new cBaseFunction("DATEVALUE");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
arg0 = arg0.tocString();
if ( arg0 instanceof cError )
return this.value = arg0;
if( arg0.tocNumber() instanceof cNumber && arg0.tocNumber().getValue() > 0 )
return this.value = new cNumber( parseInt(arg0.tocNumber().getValue()) );
var res = g_oFormatParser.parse(arg0.getValue());
if( res && res.bDateTime )
return this.value = new cNumber( parseInt(res.value) );
else
return this.value = new cError( cErrorType.wrong_value_type );
}
r.getInfo = function(){
return {
name:this.name,
args:"( date-time-string )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'DAY' : function(){
var r = new cBaseFunction("DAY");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0],val;
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
else if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
val = arg0.tocNumber().getValue();
}
if(arg0 instanceof cError) return this.setCA(arg0,true);
else if( arg0 instanceof cNumber || arg0 instanceof cBool){
val = arg0.tocNumber().getValue();
}
else if ( arg0 instanceof cRef || arg0 instanceof cRef3D ){
val = arg0.getValue().tocNumber();
if( val instanceof cNumber || val instanceof cBool ){
val = arg0.tocNumber().getValue();
}
else{
return this.setCA(new cError( cErrorType.wrong_value_type ),true);
}
}
else if(arg0 instanceof cString){
val = arg0.tocNumber();
if(val instanceof cError || val instanceof cEmpty ){
var d = new Date( arg0.getValue() );
if( isNaN(d) ){
return this.setCA(new cError( cErrorType.wrong_value_type ),true);
}
else
val = Math.floor( ( d.getTime()/1000 - d.getTimezoneOffset()*60 )/c_sPerDay+( c_DateCorrectConst + (g_bDate1904?0:1) ) );
}
else {
val = arg0.tocNumber().getValue();
}
}
if(val < 0)
return this.setCA(new cError( cErrorType.not_numeric ),true);
else if(!g_bDate1904)
{
if( val < 60 )
return this.setCA(new cNumber( ( new Date((val-c_DateCorrectConst)*c_msPerDay) ).getUTCDate() ),true,0);
else if( val == 60 )
return this.setCA(new cNumber( ( new Date((val-c_DateCorrectConst-1)*c_msPerDay) ).getUTCDate() +1),true,0);
else
return this.setCA(new cNumber( ( new Date((val-c_DateCorrectConst-1)*c_msPerDay) ).getUTCDate() ),true,0);
}
else
return this.setCA(new cNumber( ( new Date((val-c_DateCorrectConst)*c_msPerDay) ).getUTCDate() ),true,0);
}
r.getInfo = function(){
return {
name:this.name,
args:"( date-value )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'DAYS360' : function(){
var r = new cBaseFunction("DAYS360");
return r;
},
'EDATE' : function(){
var r = new cBaseFunction("EDATE");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1];
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if ( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
arg0 = arg0.tocNumber();
arg1 = arg1.tocNumber();
if( arg0 instanceof cError) return this.value = arg0;
if( arg1 instanceof cError) return this.value = arg1;
var val = arg0.getValue(), date, _date;
if(val < 0)
return this.setCA(new cError( cErrorType.not_numeric ),true);
else if(!g_bDate1904){
if( val < 60 )
val = new Date((val-c_DateCorrectConst)*c_msPerDay);
else if( val == 60 )
val = new Date((val-c_DateCorrectConst-1)*c_msPerDay);
else
val = new Date((val-c_DateCorrectConst-1)*c_msPerDay);
}
else
val = new Date((val-c_DateCorrectConst)*c_msPerDay);
date = new Date(val);
if( 0 <= date.getDate() && 28 >= date.getDate() ){
val = new Date(val.setMonth(val.getMonth()+arg1.getValue()))
}
else if( 29 <= date.getDate() && 31 >= date.getDate() ){
date.setDate(1);
date.setMonth(date.getMonth()+arg1.getValue());
if( val.getDate() > (_date = date.getDaysInMonth()) ){
val.setDate( _date );
}
val = new Date(val.setMonth(val.getMonth()+arg1.getValue()));
}
return this.value = new cNumber( Math.floor( ( val.getTime()/1000 - val.getTimezoneOffset()*60 )/c_sPerDay+(c_DateCorrectConst+1) ) )
}
r.getInfo = function(){
return {
name:this.name,
args:"( start-date , month-offset )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'EOMONTH' : function(){
var r = new cBaseFunction("EOMONTH");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1];
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if ( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
arg0 = arg0.tocNumber();
arg1 = arg1.tocNumber();
if( arg0 instanceof cError) return this.value = arg0;
if( arg1 instanceof cError) return this.value = arg1;
var val = arg0.getValue(), date, _date;
if(val < 0)
return this.setCA(new cError( cErrorType.not_numeric ),true);
else if(!g_bDate1904){
if( val < 60 )
val = new Date((val-c_DateCorrectConst)*c_msPerDay);
else if( val == 60 )
val = new Date((val-c_DateCorrectConst-1)*c_msPerDay);
else
val = new Date((val-c_DateCorrectConst-1)*c_msPerDay);
}
else
val = new Date((val-c_DateCorrectConst)*c_msPerDay);
date = new Date(val);
val.setDate(1);
val.setMonth(val.getMonth()+arg1.getValue());
val.setDate(val.getDaysInMonth());
return this.value = new cNumber( Math.floor( ( val.getTime()/1000 - val.getTimezoneOffset()*60 )/c_sPerDay+(c_DateCorrectConst+1) ) );
}
r.getInfo = function(){
return {
name:this.name,
args:"( start-date , month-offset )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'HOUR' : function(){
var r = new cBaseFunction("HOUR");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0],val;
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
else if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
}
if(arg0 instanceof cError)return this.setCA(arg0,true);
else if( arg0 instanceof cNumber || arg0 instanceof cBool){
val = arg0.tocNumber().getValue();
}
else if ( arg0 instanceof cRef || arg0 instanceof cRef3D ){
val = arg0.getValue();
if(val instanceof cError)return this.setCA(val,true);
else if( val instanceof cNumber || val instanceof cBool ){
val = arg0.tocNumber().getValue();
}
else{
return this.setCA(new cError( cErrorType.wrong_value_type ),true);
}
}
else if(arg0 instanceof cString){
val = arg0.tocNumber();
if(val instanceof cError || val instanceof cEmpty ){
var d = new Date( arg0.getValue() );
if( isNaN(d) ){
d = g_oFormatParser.parseDate(arg0.getValue());
if( d == null ){
return this.setCA(new cError( cErrorType.wrong_value_type ),true);
}
val = d.value ;
}
else
val = ( d.getTime()/1000 - d.getTimezoneOffset()*60 )/c_sPerDay+( c_DateCorrectConst + (g_bDate1904?0:1) );
}
else {
val = arg0.tocNumber().getValue();
}
}
if(val < 0)
return this.setCA(new cError( cErrorType.not_numeric ),true);
else //1 2 3 4 4 3 2 1
return this.setCA( new cNumber( parseInt( ( ( val-Math.floor(val) )*24 ).toFixed(cExcelDateTimeDigits) ) ) ,true,0);
}
r.getInfo = function(){
return {
name:this.name,
args:"( time-value )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'MINUTE' : function(){
var r = new cBaseFunction("MINUTE");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0],val;
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
else if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
}
if(arg0 instanceof cError) return this.setCA(arg0,true);
else if( arg0 instanceof cNumber || arg0 instanceof cBool){
val = arg0.tocNumber().getValue();
}
else if ( arg0 instanceof cRef || arg0 instanceof cRef3D ){
val = arg0.getValue();
if(val instanceof cError) return this.setCA( val ,true);
else if( val instanceof cNumber || val instanceof cBool ){
val = arg0.tocNumber().getValue();
}
else{
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
}
else if(arg0 instanceof cString){
val = arg0.tocNumber();
if(val instanceof cError || val instanceof cEmpty ){
var d = new Date( arg0.getValue() );
if( isNaN(d) ){
d = g_oFormatParser.parseDate(arg0.getValue());
if( d == null ){
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
val = d.value ;
}
else
val = ( d.getTime()/1000 - d.getTimezoneOffset()*60 )/c_sPerDay+( c_DateCorrectConst + (g_bDate1904?0:1) );
}
else {
val = arg0.tocNumber().getValue();
}
}
if(val < 0)
return this.setCA( new cError( cErrorType.not_numeric ) ,true);
else{
val = parseInt(( ( val*24-Math.floor(val*24) ) * 60 ).toFixed(cExcelDateTimeDigits)) % 60;
return this.setCA( new cNumber( val ) ,true,0);
}
}
r.getInfo = function(){
return {
name:this.name,
args:"( time-value )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'MONTH' : function(){
var r = new cBaseFunction("MONTH");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0],val;
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
else if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
}
if(arg0 instanceof cError) return this.setCA( arg0 ,true);
else if( arg0 instanceof cNumber || arg0 instanceof cBool){
val = arg0.tocNumber().getValue();
}
else if ( arg0 instanceof cRef || arg0 instanceof cRef3D ){
val = arg0.getValue();
if(val instanceof cError) return this.setCA( val ,true);
else if( val instanceof cNumber || val instanceof cBool ){
val = arg0.tocNumber().getValue();
}
else{
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
}
else if(arg0 instanceof cString){
val = arg0.tocNumber();
if(val instanceof cError || val instanceof cEmpty ){
var d = new Date( arg0.getValue() );
if( isNaN(d) ){
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
else
val = Math.floor( ( d.getTime()/1000 - d.getTimezoneOffset()*60 )/c_sPerDay+( c_DateCorrectConst + (g_bDate1904?0:1) ) );
}
else {
val = arg0.tocNumber().getValue();
}
}
if(val < 0)
return this.setCA( new cError( cErrorType.not_numeric ) ,true);
else
if(!g_bDate1904){
if( val == 60 )
return this.setCA( new cNumber( 2 ) ,true,0);
else
return this.setCA( new cNumber( ( new Date( ( (val==0?1:val) - c_DateCorrectConst ) * c_msPerDay) ).getUTCMonth() + 1 ) ,true,0);
}
else
return this.setCA( new cNumber( ( new Date( ( (val==0?1:val) - c_DateCorrectConst ) * c_msPerDay) ).getUTCMonth() + 1 ) ,true,0);
}
r.getInfo = function(){
return {
name:this.name,
args:"( date-value )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'NETWORKDAYS' : function(){
var r = new cBaseFunction("NETWORKDAYS");
r.setArgumentsMin(2);
r.setArgumentsMax(3);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], arrDateIncl = [];
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if ( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
arg0 = arg0.tocNumber();
arg1 = arg1.tocNumber();
if( arg0 instanceof cError) return this.value = arg0;
if( arg1 instanceof cError) return this.value = arg1;
var val0 = arg0.getValue(), val1 = arg1.getValue(), dif, count = 0;
if(val0 < 0)
return this.setCA(new cError( cErrorType.not_numeric ),true);
else if(!g_bDate1904){
if( val0 < 60 )
val0 = new Date((val0-c_DateCorrectConst)*c_msPerDay);
else if( val0 == 60 )
val0 = new Date((val0-c_DateCorrectConst-1)*c_msPerDay);
else
val0 = new Date((val0-c_DateCorrectConst-1)*c_msPerDay);
}
else
val0 = new Date((val0-c_DateCorrectConst)*c_msPerDay);
if(val1 < 0)
return this.setCA(new cError( cErrorType.not_numeric ),true);
else if(!g_bDate1904){
if( val1 < 60 )
val1 = new Date((val1-c_DateCorrectConst)*c_msPerDay);
else if( val1 == 60 )
val1 = new Date((val1-c_DateCorrectConst-1)*c_msPerDay);
else
val1 = new Date((val1-c_DateCorrectConst-1)*c_msPerDay);
}
else
val1 = new Date((val1-c_DateCorrectConst)*c_msPerDay);
var holidays = [];
if( arg2 ){
if( arg2 instanceof cArea || arg2 instanceof cArea3D ){
var arr = arg2.getValue();
for(var i = 0; i < arr.length; i++){
if( arr[i] instanceof cNumber && arr[i].getValue() >= 0 ){
holidays.push(arr[i]);
}
}
}
else if( arg2 instanceof cArray ){
arg2.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
holidays.push(elem);
}
else if( elem instanceof cString ){
var res = g_oFormatParser.parse(elem.getValue());
if( res && res.bDateTime && res.value >= 0 )
holidays.push( new cNumber(parseInt(res.value)) );
}
})
}
}
for(var i = 0; i < holidays.length; i++ ){
if(!g_bDate1904){
if( holidays[i].getValue() < 60 )
holidays[i] = new Date((holidays[i].getValue()-c_DateCorrectConst)*c_msPerDay);
else if( holidays[i] == 60 )
holidays[i] = new Date((holidays[i].getValue()-c_DateCorrectConst-1)*c_msPerDay);
else
holidays[i] = new Date((holidays[i].getValue()-c_DateCorrectConst-1)*c_msPerDay);
}
else
holidays[i] = new Date((holidays[i].getValue()-c_DateCorrectConst)*c_msPerDay);
}
function includeInHolidays(date){
for(var i = 0; i < holidays.length; i++ ){
if( date.getTime() == holidays[i].getTime() )
return false;
}
return true;
}
dif = ( val1 - val0 ) / c_msPerDay;
for(var i = 0; i < Math.abs(dif); i++ ){
var date = new Date( val0 );
date.setDate(val0.getDate()+i) ;
if( date.getDay() != 5 && date.getDay() != 6 && includeInHolidays(date) )
count++;
}
return this.value = new cNumber( (dif<0?-1:1)*count );
}
r.getInfo = function(){
return {
name:this.name,
args:"( start-date , end-date [ , holidays ] )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'NETWORKDAYS.INTL' : function(){
var r = new cBaseFunction("NETWORKDAYS.INTL");
return r;
},
'NOW' : function(){
var r = new cBaseFunction("NOW");
r.setArgumentsMin(0);
r.setArgumentsMax(0);
r.Calculate = function(){
var d = new Date();
this.value = new cNumber( Math.floor( ( d.getTime()/1000 - d.getTimezoneOffset()*60 )/c_sPerDay+(c_DateCorrectConst+1) ) + ( (d.getHours()*60*60+d.getMinutes()*60+d.getSeconds())/c_sPerDay ) );
this.value.numFormat = 22;
return this.setCA( this.value ,true);
}
r.getInfo = function(){
return {
name:this.name,
args:"()"
};
}
return r;
},
'SECOND' : function(){
var r = new cBaseFunction("SECOND");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0],val;
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
else if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
}
if(arg0 instanceof cError) return this.setCA( arg0 ,true);
else if( arg0 instanceof cNumber || arg0 instanceof cBool){
val = arg0.tocNumber().getValue();
}
else if ( arg0 instanceof cRef || arg0 instanceof cRef3D ){
val = arg0.getValue();
if(val instanceof cError) return this.setCA( val ,true);
else if( val instanceof cNumber || val instanceof cBool ){
val = arg0.tocNumber().getValue();
}
else{
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
}
else if(arg0 instanceof cString){
val = arg0.tocNumber();
if(val instanceof cError || val instanceof cEmpty ){
var d = new Date( arg0.getValue() );
if( isNaN(d) ){
d = g_oFormatParser.parseDate(arg0.getValue());
if( d == null ){
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
val = d.value ;
}
else
val = ( d.getTime()/1000 - d.getTimezoneOffset()*60 )/c_sPerDay+( c_DateCorrectConst + (g_bDate1904?0:1) );
}
else {
val = arg0.tocNumber().getValue();
}
}
if(val < 0)
return this.setCA( new cError( cErrorType.not_numeric ) ,true);
else{
val = parseInt((( val*24*60-Math.floor(val*24*60) ) * 60).toFixed(cExcelDateTimeDigits)) % 60;
return this.setCA( new cNumber( val ) ,true,0);
}
}
r.getInfo = function(){
return {
name:this.name,
args:"( time-value )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'TIME' : function(){
var r = new cBaseFunction("TIME");
r.setArgumentsMin(3);
r.setArgumentsMax(3);
//to excel (hh*60*60+mm*60+ss)/c_sPerDay (c_sPerDay the number of seconds in a day)
r.Calculate = function(arg){
var hour,minute,second;
for (var i = 0; i<this.argumentsCurrent;i++){
var arg0 = arg[i];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
else if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
}
if(arg0 instanceof cError) return this.setCA( arg0 ,true);
else if( arg0 instanceof cNumber || arg0 instanceof cBool){
if(i==0)hour = arg0.tocNumber().getValue();
if(i==1)minute = arg0.tocNumber().getValue();
if(i==2)second = arg0.tocNumber().getValue();
}
else if ( arg0 instanceof cRef || arg0 instanceof cRef3D ){
var val = arg0.getValue();
if(val instanceof cError) return this.setCA( val ,true);
else if( val instanceof cNumber || val instanceof cBool ){
if(i==0)hour = arg0.tocNumber().getValue();
if(i==1)minute = arg0.tocNumber().getValue();
if(i==2)second = arg0.tocNumber().getValue();
}
else{
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
}
else if(arg0 instanceof cString || arg0 instanceof cEmpty){
var val = arg0.tocNumber();
if(val instanceof cError) return this.setCA( val ,true);
else {
if(i==0)hour = arg0.tocNumber().getValue();
if(i==1)minute = arg0.tocNumber().getValue();
if(i==2)second = arg0.tocNumber().getValue();
}
}
}
var v = (hour*60*60+minute*60+second)/c_sPerDay;
this.setCA( new cNumber( v - Math.floor(v) ) ,true);
if( arguments[1].getNumFormatStr().toLowerCase() === "general" )
this.value.numFormat = 18;
return this.value;
}
r.getInfo = function(){
return {
name:this.name,
args:"( hour, minute, second )"
};
}
return r;
},
'TIMEVALUE' : function(){
var r = new cBaseFunction("TIMEVALUE");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
arg0 = arg0.tocString();
if ( arg0 instanceof cError )
return this.value = arg0;
if( arg0.tocNumber() instanceof cNumber && arg0.tocNumber().getValue() > 0 )
return this.value = new cNumber( parseInt(arg0.tocNumber().getValue()) );
var res = g_oFormatParser.parse(arg0.getValue());
if( res && res.bDateTime )
return this.value = new cNumber( res.value - parseInt(res.value) );
else
return this.value = new cError( cErrorType.wrong_value_type );
}
r.getInfo = function(){
return {
name:this.name,
args:"( date-time-string )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'TODAY' : function(){
var r = new cBaseFunction("TODAY");
r.setArgumentsMin(0);
r.setArgumentsMax(0);
r.Calculate = function(){
var d = new Date();
// 1 2 3 3 4 5 5 4 2 1
this.setCA( new cNumber( Math.floor( ( d.getTime()/1000 - d.getTimezoneOffset()*60 )/c_sPerDay+( c_DateCorrectConst + (g_bDate1904?0:1) ) ) ) ,true);
if( arguments[1].getNumFormatStr().toLowerCase() === "general" )
this.value.numFormat = 14;
return this.value;
}
r.getInfo = function(){
return {
name:this.name,
args:"()"
};
}
return r;
//Math.floor(((new Date()).getTime()/1000)/c_sPerDay+(c_DateCorrectConst+1)) from UTC-timestamp to excel 2010
},
'WEEKDAY' : function(){
var r = new cBaseFunction("WEEKDAY");
r.setArgumentsMin(1);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1] ? arg[1] : new cNumber(1);
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if ( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
arg0 = arg0.tocNumber();
arg1 = arg1.tocNumber();
if ( arg0 instanceof cError )
return this.value = arg0;
if ( arg1 instanceof cError )
return this.value = arg1;
var weekday;
switch (arg1.getValue()){
case 1: /* 1 (Sunday) through 7 (Saturday) */
case 17:/* 1 (Sunday) through 7 (Saturday) */
weekday = [1, 2, 3, 4, 5, 6, 7];
break;
case 2: /* 1 (Monday) through 7 (Sunday) */
case 11:/* 1 (Monday) through 7 (Sunday) */
weekday = [7, 1, 2, 3, 4, 5, 6];
break;
case 3: /* 0 (Monday) through 6 (Sunday) */
weekday = [ 6, 0, 1, 2, 3, 4, 5];
break;
case 12:/* 1 (Tuesday) through 7 (Monday) */
weekday = [6, 7, 1, 2, 3, 4, 5];
break;
case 13:/* 1 (Wednesday) through 7 (Tuesday) */
weekday = [5, 6, 7, 1, 2, 3, 4];
break;
case 14:/* 1 (Thursday) through 7 (Wednesday) */
weekday = [ 4, 5, 6, 7, 1, 2, 3];
break;
case 15:/* 1 (Friday) through 7 (Thursday) */
weekday = [3, 4, 5, 6, 7, 1, 2];
break;
case 16:/* 1 (Saturday) through 7 (Friday) */
weekday = [2, 3, 4, 5, 6, 7, 1];
break;
default:
return this.value = new cError( cErrorType.not_numeric );
}
if( arg0.getValue() < 0 )
return this.value = new cError( cErrorType.wrong_value_type );
return this.value = new cNumber( weekday[new Date((arg0.getValue()-(c_DateCorrectConst+1))*c_msPerDay).getDay()] );
}
r.getInfo = function(){
return {
name:this.name,
args:"( serial-value [ , weekday-start-flag ] )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'WEEKNUM' : function(){
var r = new cBaseFunction("WEEKNUM");
//var d = new Date(), iso = [6,7,8,9,10,4,5]; console.log(d); var b = new Date(d.getFullYear(), 0, 1); var c = ((d-b)/(86400000)+iso[b.getDay()])/7; console.log(c)
r.setArgumentsMin(1);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1] ? arg[1] : new cNumber(1), type = 0;
function WeekNumber(dt,iso,type){
dt.setHours(0,0,0);
var startOfYear = new Date(dt.getFullYear(), 0, 1);
var endOfYear = new Date(dt);
endOfYear.setMonth(11);
endOfYear.setDate(31);
var wk = parseInt(((dt-startOfYear)/c_msPerDay+iso[startOfYear.getDay()])/7);
if(type)
switch (wk){
case 0:
// Возвращаем номер недели от 31 декабря предыдущего года
startOfYear.setDate(0);
return WeekNumber(startOfYear,iso,type);
case 53:
// Если 31 декабря выпадает до четверга 1 недели следующего года
if (endOfYear.getDay() < 4)
return new cNumber(1);
else
return new cNumber(wk);
default:
return new cNumber(wk);
}
else
return new cNumber(wk);
}
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if ( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
arg0 = arg0.tocNumber();
arg1 = arg1.tocNumber();
if ( arg0 instanceof cError )
return this.value = arg0;
if ( arg1 instanceof cError )
return this.value = arg1;
if( arg0.getValue() < 0 )
return this.value = new cError( cErrorType.not_numeric );
var weekdayStartDay;
switch (arg1.getValue()){
case 1: /* 1 (Sunday) through 7 (Saturday) */
case 17:/* 1 (Sunday) through 7 (Saturday) */
// weekdayStartDay = [10,4,5,6,7,8,9];
// type = 0;
//weekdayStartDay = [13,7,8,9,10,11,12];
// break;
case 2: /* 1 (Monday) through 7 (Sunday) */
case 11:/* 1 (Monday) through 7 (Sunday) */
// weekdayStartDay = [6,0,1,2,3,4,5];
// type = 0;
// break;
case 12:/* 1 (Tuesday) through 7 (Monday) */
// weekdayStartDay = [6,7,8,2,3,4,5];
// type = 0;
// break;
case 13:/* 1 (Wednesday) through 7 (Tuesday) */
// weekdayStartDay = [6,7,8,9,3,4,5];
// type = 0;
// break;
case 14:/* 1 (Thursday) through 7 (Wednesday) */
// weekdayStartDay = [6,7,8,9,10,4,5];
// type = 0;
// break;
case 15:/* 1 (Friday) through 7 (Thursday) */
// weekdayStartDay = [6,7,8,9,10,11,5];
// weekdayStartDay = [5,6,7,8,9,10,4];
// weekdayStartDay = [6,7,1,2,3,4,5];
weekdayStartDay = [5,6,7,8,9,10,4];
type = 0;
break;
case 16:/* 1 (Saturday) through 7 (Friday) */
// weekdayStartDay = [6,7,8,9,10,11,12];
weekdayStartDay = [6,0,1,2,3,4,5];
type = 0;
break;
case 21:
weekdayStartDay = [6,7,1,2,3,4,5];
// { 6, 7, 8, 9, 10, 4, 5 }
type = 1;
break;
default:
return this.value = new cError( cErrorType.not_numeric );
}
return this.value = new cNumber(WeekNumber(new Date((arg0.getValue()-(c_DateCorrectConst+1))*c_msPerDay),weekdayStartDay,type));
}
r.getInfo = function(){
return {
name:this.name,
args:"( serial-value [ , weekday-start-flag ] )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'WORKDAY' : function(){
var r = new cBaseFunction("WORKDAY");
r.setArgumentsMin(2);
r.setArgumentsMax(3);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], arrDateIncl = [];
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if ( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
arg0 = arg0.tocNumber();
arg1 = arg1.tocNumber();
if( arg0 instanceof cError) return this.value = arg0;
if( arg1 instanceof cError) return this.value = arg1;
var val0 = arg0.getValue(), val1 = arg1.getValue(), holidays = []
if(val0 < 0)
return this.setCA(new cError( cErrorType.not_numeric ),true);
else if(!g_bDate1904){
if( val0 < 60 )
val0 = new Date((val0-c_DateCorrectConst)*c_msPerDay);
else if( val0 == 60 )
val0 = new Date((val0-c_DateCorrectConst-1)*c_msPerDay);
else
val0 = new Date((val0-c_DateCorrectConst-1)*c_msPerDay);
}
else
val0 = new Date((val0-c_DateCorrectConst)*c_msPerDay);
if( arg2 ){
if( arg2 instanceof cArea || arg2 instanceof cArea3D ){
var arr = arg2.getValue();
for(var i = 0; i < arr.length; i++){
if( arr[i] instanceof cNumber && arr[i].getValue() >= 0 ){
holidays.push(arr[i]);
}
}
}
else if( arg2 instanceof cArray ){
arg2.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
holidays.push(elem);
}
else if( elem instanceof cString ){
var res = g_oFormatParser.parse(elem.getValue());
if( res && res.bDateTime && res.value >= 0 )
holidays.push( new cNumber(parseInt(res.value)) );
}
})
}
}
for(var i = 0; i < holidays.length; i++ ){
if(!g_bDate1904){
if( holidays[i].getValue() < 60 )
holidays[i] = new Date((holidays[i].getValue()-c_DateCorrectConst)*c_msPerDay);
else if( holidays[i] == 60 )
holidays[i] = new Date((holidays[i].getValue()-c_DateCorrectConst-1)*c_msPerDay);
else
holidays[i] = new Date((holidays[i].getValue()-c_DateCorrectConst-1)*c_msPerDay);
}
else
holidays[i] = new Date((holidays[i].getValue()-c_DateCorrectConst)*c_msPerDay);
}
function notAHolidays(date){
for(var i = 0; i < holidays.length; i++ ){
if( date.getTime() == holidays[i].getTime() )
return false;
}
return true;
}
var dif = arg1.getValue(), count = 1, dif1 = dif>0?1:dif<0?-1:0, val, date = val0;
while( Math.abs(dif) > count ){
date = new Date( val0.getTime() + dif1*c_msPerDay );
if( date.getDay() != 6 && date.getDay() != 0 && notAHolidays(date) )
count++;
dif>=0?dif1++:dif1--;
}
date = new Date( val0.getTime() + dif1*c_msPerDay );
val = parseInt(( date.getTime()/1000 - date.getTimezoneOffset()*60 )/c_sPerDay+( c_DateCorrectConst + (g_bDate1904?0:1) ));
if( val < 0 )
return this.setCA(new cError( cErrorType.not_numeric ),true);
if( arguments[1].getNumFormatStr().toLowerCase() === "general" )
return this.setCA( new cNumber( val ) ,true,14);
else
return this.setCA( new cNumber( val ) ,true);
}
r.getInfo = function(){
return {
name:this.name,
args:"( start-date , day-offset [ , holidays ] )"
};
}
// r.setFormat(r.formatType.noneFormat);
return r;
},
'WORKDAY.INTL' : function(){
var r = new cBaseFunction("WORKDAY.INTL");
return r;
},
'YEAR' : function(){
var r = new cBaseFunction("YEAR");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0],val;
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
else if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
}
if(arg0 instanceof cError) return this.setCA( arg0 ,true);
else if( arg0 instanceof cNumber || arg0 instanceof cBool){
val = arg0.tocNumber().getValue();
}
else if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
else if ( arg0 instanceof cRef || arg0 instanceof cRef3D ){
val = arg0.getValue();
if(val instanceof cError) return this.setCA( val ,true);
else if( val instanceof cNumber || val instanceof cBool ){
val = arg0.tocNumber().getValue();
}
else{
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
}
else if(arg0 instanceof cString){
val = arg0.tocNumber();
if(val instanceof cError || val instanceof cEmpty ){
var d = new Date( arg0.getValue() );
if( isNaN(d) ){
return this.setCA( new cError( cErrorType.wrong_value_type ) ,true);
}
else
val = Math.floor( ( d.getTime()/1000 - d.getTimezoneOffset()*60 )/c_sPerDay+( c_DateCorrectConst + (g_bDate1904?0:1) ) );
}
else {
val = arg0.tocNumber().getValue();
}
}
if(val < 0)
return this.setCA( new cError( cErrorType.not_numeric ) ,true,0);
else
return this.setCA( new cNumber( (new Date((val-(c_DateCorrectConst+1))*c_msPerDay)).getUTCFullYear() ) ,true,0);
}
r.getInfo = function(){
return {
name:this.name,
args:"( date-value )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'YEARFRAC' : function(){
var r = new cBaseFunction("YEARFRAC");
return r;
}
}
\ No newline at end of file
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 12:25
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.Engineering = {
'groupName':"Engineering",
'BESSELI':function () {
var r = new cBaseFunction( "BESSELI" );
return r;
},
'BESSELJ':function () {
var r = new cBaseFunction( "BESSELJ" );
return r;
},
'BESSELK':function () {
var r = new cBaseFunction( "BESSELK" );
return r;
},
'BESSELY':function () {
var r = new cBaseFunction( "BESSELY" );
return r;
},
'BIN2DEC':function () {
var r = new cBaseFunction( "BIN2DEC" );
return r;
},
'BIN2HEX':function () {
var r = new cBaseFunction( "BIN2HEX" );
return r;
},
'BIN2OCT':function () {
var r = new cBaseFunction( "BIN2OCT" );
return r;
},
'COMPLEX':function () {
var r = new cBaseFunction( "COMPLEX" );
return r;
},
'CONVERT':function () {
var r = new cBaseFunction( "CONVERT" );
return r;
},
'DEC2BIN':function () {
var r = new cBaseFunction( "DEC2BIN" );
return r;
},
'DEC2HEX':function () {
var r = new cBaseFunction( "DEC2HEX" );
return r;
},
'DEC2OCT':function () {
var r = new cBaseFunction( "DEC2OCT" );
return r;
},
'DELTA':function () {
var r = new cBaseFunction( "DELTA" );
return r;
},
'ERF':function () {
var r = new cBaseFunction( "ERF" );
return r;
},
'ERFC':function () {
var r = new cBaseFunction( "ERFC" );
return r;
},
'GESTEP':function () {
var r = new cBaseFunction( "GESTEP" );
return r;
},
'HEX2BIN':function () {
var r = new cBaseFunction( "HEX2BIN" );
return r;
},
'HEX2DEC':function () {
var r = new cBaseFunction( "HEX2DEC" );
return r;
},
'HEX2OCT':function () {
var r = new cBaseFunction( "HEX2OCT" );
return r;
},
'IMABS':function () {
var r = new cBaseFunction( "IMABS" );
return r;
},
'IMAGINARY':function () {
var r = new cBaseFunction( "IMAGINARY" );
return r;
},
'IMARGUMENT':function () {
var r = new cBaseFunction( "IMARGUMENT" );
return r;
},
'IMCONJUGATE':function () {
var r = new cBaseFunction( "IMCONJUGATE" );
return r;
},
'IMCOS':function () {
var r = new cBaseFunction( "IMCOS" );
return r;
},
'IMDIV':function () {
var r = new cBaseFunction( "IMDIV" );
return r;
},
'IMEXP':function () {
var r = new cBaseFunction( "IMEXP" );
return r;
},
'IMLN':function () {
var r = new cBaseFunction( "IMLN" );
return r;
},
'IMLOG10':function () {
var r = new cBaseFunction( "IMLOG10" );
return r;
},
'IMLOG2':function () {
var r = new cBaseFunction( "IMLOG2" );
return r;
},
'IMPOWER':function () {
var r = new cBaseFunction( "IMPOWER" );
return r;
},
'IMPRODUCT':function () {
var r = new cBaseFunction( "IMPRODUCT" );
return r;
},
'IMREAL':function () {
var r = new cBaseFunction( "IMREAL" );
return r;
},
'IMSIN':function () {
var r = new cBaseFunction( "IMSIN" );
return r;
},
'IMSQRT':function () {
var r = new cBaseFunction( "IMSQRT" );
return r;
},
'IMSUB':function () {
var r = new cBaseFunction( "IMSUB" );
return r;
},
'IMSUM':function () {
var r = new cBaseFunction( "IMSUM" );
return r;
},
'OCT2BIN':function () {
var r = new cBaseFunction( "OCT2BIN" );
return r;
},
'OCT2DEC':function () {
var r = new cBaseFunction( "OCT2DEC" );
return r;
},
'OCT2HEX':function () {
var r = new cBaseFunction( "OCT2HEX" );
return r;
}
}
\ No newline at end of file
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 15:19
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.Financial = {
'groupName' : "Financial",
'ACCRINT' : function(){
var r = new cBaseFunction("ACCRINT");
return r;
},
'ACCRINTM' : function(){
var r = new cBaseFunction("ACCRINTM");
return r;
},
'AMORDEGRC' : function(){
var r = new cBaseFunction("AMORDEGRC");
return r;
},
'AMORLINC' : function(){
var r = new cBaseFunction("AMORLINC");
return r;
},
'COUPDAYBS' : function(){
var r = new cBaseFunction("COUPDAYBS");
return r;
},
'COUPDAYS' : function(){
var r = new cBaseFunction("COUPDAYS");
return r;
},
'COUPDAYSNC' : function(){
var r = new cBaseFunction("COUPDAYSNC");
return r;
},
'COUPNCD' : function(){
var r = new cBaseFunction("COUPNCD");
return r;
},
'COUPNUM' : function(){
var r = new cBaseFunction("COUPNUM");
return r;
},
'COUPPCD' : function(){
var r = new cBaseFunction("COUPPCD");
return r;
},
'CUMIPMT' : function(){
var r = new cBaseFunction("CUMIPMT");
return r;
},
'CUMPRINC' : function(){
var r = new cBaseFunction("CUMPRINC");
return r;
},
'DB' : function(){
var r = new cBaseFunction("DB");
return r;
},
'DDB' : function(){
var r = new cBaseFunction("DDB");
return r;
},
'DISC' : function(){
var r = new cBaseFunction("DISC");
return r;
},
'DOLLARDE' : function(){
var r = new cBaseFunction("DOLLARDE");
return r;
},
'DOLLARFR' : function(){
var r = new cBaseFunction("DOLLARFR");
return r;
},
'DURATION' : function(){
var r = new cBaseFunction("DURATION");
return r;
},
'EFFECT' : function(){
var r = new cBaseFunction("EFFECT");
return r;
},
'FV' : function(){
var r = new cBaseFunction("FV");
r.setArgumentsMin(3);
r.setArgumentsMax(5);
r.Calculate = function(arg){
var rate = arg[0], nper = arg[1], pmt = arg[2], pv = arg[3] ? arg[3] : new cNumber(0), type = arg[4] ? arg[4] : new cNumber(0);
if ( rate instanceof cArea || rate instanceof cArea3D ){
rate = rate.cross(arguments[1].first);
}
else if( rate instanceof cArray ){
rate = rate.getElementRowCol(0,0);
}
if ( nper instanceof cArea || nper instanceof cArea3D ){
nper = nper.cross(arguments[1].first);
}
else if( nper instanceof cArray ){
nper = nper.getElementRowCol(0,0);
}
if ( pmt instanceof cArea || pmt instanceof cArea3D ){
pmt = pmt.cross(arguments[1].first);
}
else if( pmt instanceof cArray ){
pmt = pmt.getElementRowCol(0,0);
}
if ( pv instanceof cArea || pv instanceof cArea3D ){
pv = pv.cross(arguments[1].first);
}
else if( pv instanceof cArray ){
pv = pv.getElementRowCol(0,0);
}
if ( type instanceof cArea || type instanceof cArea3D ){
type = type.cross(arguments[1].first);
}
else if( type instanceof cArray ){
type = type.getElementRowCol(0,0);
}
rate = rate.tocNumber();
nper = nper.tocNumber();
pmt = pmt.tocNumber();
pv = pv.tocNumber();
type = type.tocNumber();
if ( rate instanceof cError ) return this.value = rate;
if ( nper instanceof cError ) return this.value = nper;
if ( pmt instanceof cError ) return this.value = pmt;
if ( pv instanceof cError ) return this.value = pv;
if ( type instanceof cError ) return this.value = type;
if ( type.getValue() != 1 && type.getValue() != 0 ) return this.value = new cError( cErrorType.not_numeric );
var res;
if( rate.getValue() != 0 ){
res = -1*( pv.getValue()*Math.pow(1+rate.getValue(),nper.getValue())+pmt.getValue()*( 1 + rate.getValue()*type.getValue() )*(Math.pow((1+rate.getValue()),nper.getValue())-1)/rate.getValue() );
}
else{
res = -1*( pv.getValue()+pmt.getValue()*nper.getValue() );
}
return this.value = new cNumber(res);
}
r.getInfo = function(){
return {
name:this.name,
args:"( rate , nper , pmt [ , [ pv ] [ ,[ type ] ] ] )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'FVSCHEDULE' : function(){
var r = new cBaseFunction("FVSCHEDULE");
return r;
},
'INTRATE' : function(){
var r = new cBaseFunction("INTRATE");
return r;
},
'IPMT' : function(){
var r = new cBaseFunction("IPMT");
return r;
},
'IRR' : function(){
var r = new cBaseFunction("IRR");
return r;
},
'ISPMT' : function(){
var r = new cBaseFunction("ISPMT");
return r;
},
'MDURATION' : function(){
var r = new cBaseFunction("MDURATION");
return r;
},
'MIRR' : function(){
var r = new cBaseFunction("MIRR");
return r;
},
'NOMINAL' : function(){
var r = new cBaseFunction("NOMINAL");
return r;
},
'NPER' : function(){
var r = new cBaseFunction("NPER");
return r;
},
'NPV' : function(){
var r = new cBaseFunction("NPV");
r.setArgumentsMin(2);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], iStart = 1, res = 0, rate;
function elemCalc(rate,value,step){
return value / Math.pow(1+rate,step);
}
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
arg0 = arg0.tocNumber();
if ( arg0 instanceof cError )
return this.value = arg0;
rate = arg0.getValue();
if( rate == -1)
return this.value = new cError( cErrorType.division_by_zero );
for(var i = 1; i < this.getArguments(); i++){
var argI = arg[i];
if( argI instanceof cArea || argI instanceof cArea3D ){
var argIArr = argI.getValue();
for (var j = 0; j < argIArr.length; j++){
if( argIArr[j] instanceof cNumber ){
res += elemCalc( rate, argIArr[j].getValue(), iStart++ );
}
}
continue;
}
else if( argI instanceof cArray ){
argI.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
res += elemCalc( rate, elem.getValue(), iStart++ );
}
})
continue;
}
argI = argI.tocNumber();
if( argI instanceof cError )
continue;
res += elemCalc( rate, argI.getValue(), iStart++ );
}
return this.value = new cNumber(res);
};
r.getInfo = function(){
return {
name:this.name,
args:"( rate , argument-list )"
};
};
r.setFormat(r.formatType.noneFormat);
return r;
},
'ODDFPRICE' : function(){
var r = new cBaseFunction("ODDFPRICE");
return r;
},
'ODDFYIELD' : function(){
var r = new cBaseFunction("ODDFYIELD");
return r;
},
'ODDLPRICE' : function(){
var r = new cBaseFunction("ODDLPRICE");
return r;
},
'ODDLYIELD' : function(){
var r = new cBaseFunction("ODDLYIELD");
return r;
},
'PMT' : function(){
var r = new cBaseFunction("PMT");
r.setArgumentsMin(3);
r.setArgumentsMax(5);
r.Calculate = function(arg){
var rate = arg[0], nper = arg[1], pv = arg[2], fv = arg[3] ? arg[3] : new cNumber(0), type = arg[4] ? arg[4] : new cNumber(0);
if ( rate instanceof cArea || rate instanceof cArea3D ){
rate = rate.cross(arguments[1].first);
}
else if( rate instanceof cArray ){
rate = rate.getElementRowCol(0,0);
}
if ( nper instanceof cArea || nper instanceof cArea3D ){
nper = nper.cross(arguments[1].first);
}
else if( nper instanceof cArray ){
nper = nper.getElementRowCol(0,0);
}
if ( pv instanceof cArea || pv instanceof cArea3D ){
pv = pv.cross(arguments[1].first);
}
else if( pv instanceof cArray ){
pv = pv.getElementRowCol(0,0);
}
if ( fv instanceof cArea || fv instanceof cArea3D ){
fv = fv.cross(arguments[1].first);
}
else if( fv instanceof cArray ){
fv = fv.getElementRowCol(0,0);
}
if ( type instanceof cArea || type instanceof cArea3D ){
type = type.cross(arguments[1].first);
}
else if( type instanceof cArray ){
type = type.getElementRowCol(0,0);
}
rate = rate.tocNumber();
nper = nper.tocNumber();
pv = pv.tocNumber();
fv = fv.tocNumber();
type = type.tocNumber();
if ( rate instanceof cError ) return this.value = rate;
if ( nper instanceof cError ) return this.value = nper;
if ( nper.getValue() == 0 ) return this.value = new cError( cErrorType.division_by_zero );
if ( pv instanceof cError ) return this.value = pv;
if ( fv instanceof cError ) return this.value = fv;
if ( type instanceof cError ) return this.value = type;
if ( type.getValue() != 1 && type.getValue() != 0 ) return this.value = new cError( cErrorType.not_numeric );
var res;
if( rate.getValue() != 0 ){
res = -1*( pv.getValue() * Math.pow( 1 + rate.getValue(), nper.getValue() ) + fv.getValue() ) /
( ( 1 + rate.getValue() * type.getValue() ) * ( Math.pow((1 + rate.getValue()), nper.getValue() ) - 1 ) / rate.getValue() );
}
else{
res = -1*( pv.getValue()+fv.getValue() )/ nper.getValue();
}
return this.value = new cNumber(res);
}
r.getInfo = function(){
return {
name:this.name,
args:"( rate , nper , pv [ , [ fv ] [ ,[ type ] ] ] )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'PPMT' : function(){
var r = new cBaseFunction("PPMT");
return r;
},
'PRICE' : function(){
var r = new cBaseFunction("PRICE");
return r;
},
'PRICEDISC' : function(){
var r = new cBaseFunction("PRICEDISC");
return r;
},
'PRICEMAT' : function(){
var r = new cBaseFunction("PRICEMAT");
return r;
},
'PV' : function(){
var r = new cBaseFunction("PV");
r.setArgumentsMin(3);
r.setArgumentsMax(5);
r.Calculate = function(arg){
var rate = arg[0], nper = arg[1], pmt = arg[2], fv = arg[3] ? arg[3] : new cNumber(0), type = arg[4] ? arg[4] : new cNumber(0);
if ( rate instanceof cArea || rate instanceof cArea3D ){
rate = rate.cross(arguments[1].first);
}
else if( rate instanceof cArray ){
rate = rate.getElementRowCol(0,0);
}
if ( nper instanceof cArea || nper instanceof cArea3D ){
nper = nper.cross(arguments[1].first);
}
else if( nper instanceof cArray ){
nper = nper.getElementRowCol(0,0);
}
if ( pmt instanceof cArea || pmt instanceof cArea3D ){
pmt = pmt.cross(arguments[1].first);
}
else if( pmt instanceof cArray ){
pmt = pmt.getElementRowCol(0,0);
}
if ( fv instanceof cArea || fv instanceof cArea3D ){
fv = fv.cross(arguments[1].first);
}
else if( fv instanceof cArray ){
fv = fv.getElementRowCol(0,0);
}
if ( type instanceof cArea || type instanceof cArea3D ){
type = type.cross(arguments[1].first);
}
else if( type instanceof cArray ){
type = type.getElementRowCol(0,0);
}
rate = rate.tocNumber();
nper = nper.tocNumber();
pmt = pmt.tocNumber();
fv = fv.tocNumber();
type = type.tocNumber();
if ( rate instanceof cError ) return this.value = rate;
if ( nper instanceof cError ) return this.value = nper;
if ( pmt instanceof cError ) return this.value = pmt;
if ( fv instanceof cError ) return this.value = fv;
if ( type instanceof cError ) return this.value = type;
if ( type.getValue() != 1 && type.getValue() != 0 ) return this.value = new cError( cErrorType.not_numeric );
var res;
if( rate.getValue() != 0 ){
res = -1*( fv.getValue() + pmt.getValue()*(1+rate.getValue()*type.getValue())*( (Math.pow((1+rate.getValue()),nper.getValue())-1)/rate.getValue() ) )/Math.pow(1+rate.getValue(),nper.getValue())
}
else{
res = -1*( fv.getValue()+pmt.getValue()*nper.getValue() );
}
return this.value = new cNumber(res);
}
r.getInfo = function(){
return {
name:this.name,
args:"( rate , nper , pmt [ , [ fv ] [ ,[ type ] ] ] )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'RATE' : function(){
var r = new cBaseFunction("RATE");
return r;
},
'RECEIVED' : function(){
var r = new cBaseFunction("RECEIVED");
return r;
},
'SLN' : function(){
var r = new cBaseFunction("SLN");
return r;
},
'SYD' : function(){
var r = new cBaseFunction("SYD");
return r;
},
'TBILLEQ' : function(){
var r = new cBaseFunction("TBILLEQ");
return r;
},
'TBILLPRICE' : function(){
var r = new cBaseFunction("TBILLPRICE");
return r;
},
'TBILLYIELD' : function(){
var r = new cBaseFunction("TBILLYIELD");
return r;
},
'VDB' : function(){
var r = new cBaseFunction("VDB");
return r;
},
'XIRR' : function(){
var r = new cBaseFunction("XIRR");
return r;
},
'XNPV' : function(){
var r = new cBaseFunction("XNPV");
return r;
},
'YIELD' : function(){
var r = new cBaseFunction("YIELD");
return r;
},
'YIELDDISC' : function(){
var r = new cBaseFunction("YIELDDISC");
return r;
},
'YIELDMAT' : function(){
var r = new cBaseFunction("YIELDMAT");
return r;
}
}
\ No newline at end of file
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 15:21
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.Information = {
'groupName' : "Information",
"CELL" :function(){
var r = new cBaseFunction("CELL");
return r;
},
"ERROR.TYPE" :function(){
var r = new cBaseFunction("ERROR.TYPE");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
function typeError(elem){
if ( !(elem instanceof cError) )
return new cError( cErrorType.not_available );
else {
switch (elem.errorType){
case cErrorType.null_value:
return new cNumber( 1 );
case cErrorType.division_by_zero:
return new cNumber( 2 );
case cErrorType.wrong_value_type:
return new cNumber( 3 );
case cErrorType.bad_reference:
return new cNumber( 4 );
case cErrorType.wrong_name:
return new cNumber( 5 );
case cErrorType.not_numeric:
return new cNumber( 6 );
case cErrorType.not_available:
return new cNumber( 7 );
case cErrorType.getting_data:
return new cNumber( 8 );
default:
return new cError( cErrorType.not_available );
}
}
}
var arg0 = arg[0];
if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
arg0 = arg0.getValue();
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cArray ){
var ret = new cArray();
arg0.foreach(function(elem,r,c){
if(!ret.array[r])
ret.addRow();
ret.addElement(found_operand)
})
return this.value = ret;
}
return this.value = typeError(arg0);
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"INFO" :function(){
var r = new cBaseFunction("INFO");
return r;
},
"ISBLANK" :function(){
var r = new cBaseFunction("ISBLANK");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef ){
arg0 = arg0.getValue();
}
if ( arg0 instanceof cEmpty)
return this.value = new cBool( true );
else
return this.value = new cBool( false );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"ISERR" :function(){
var r = new cBaseFunction("ISERR");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef ){
arg0 = arg0.getValue();
}
if ( arg0 instanceof cError && arg0.errorType != cErrorType.not_available )
return this.value = new cBool( true );
else
return this.value = new cBool( false );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"ISERROR" :function(){
var r = new cBaseFunction("ISERROR");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef ){
arg0 = arg0.getValue();
}
if ( arg0 instanceof cError )
return this.value = new cBool( true );
else
return this.value = new cBool( false );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"ISEVEN" :function(){
var r = new cBaseFunction("ISEVEN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if ( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
arg0 = arg0.getValue();
}
var arg0 = arg0.tocNumber();
if ( arg0 instanceof cError )
return this.value = arg0;
else
return this.value = new cBool( (arg0.getValue() & 1) == 0 );
}
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
"ISLOGICAL" :function(){
var r = new cBaseFunction("ISLOGICAL");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef ){
arg0 = arg0.getValue();
}
if ( arg0 instanceof cBool )
return this.value = new cBool( true );
else return this.value = new cBool( false );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"ISNA" :function(){
var r = new cBaseFunction("ISNA");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef ){
arg0 = arg0.getValue();
}
if ( arg0 instanceof cError && arg0.errorType == cErrorType.not_available )
return this.value = new cBool( true );
else
return this.value = new cBool( false );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"ISNONTEXT" :function(){
var r = new cBaseFunction("ISNONTEXT");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef ){
arg0 = arg0.getValue();
}
if( !(arg0 instanceof cString) )
return this.value = new cBool( true );
else
return this.value = new cBool( false );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"ISNUMBER" :function(){
var r = new cBaseFunction("ISNUMBER");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef ){
arg0 = arg0.getValue();
}
if( arg0 instanceof cNumber )
return this.value = new cBool( true );
else
return this.value = new cBool( false );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"ISODD" :function(){
var r = new cBaseFunction("ISODD");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if ( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
arg0 = arg0.getValue();
}
var arg0 = arg0.tocNumber();
if ( arg0 instanceof cError )
return this.value = arg0;
else
return this.value = new cBool( (arg0.getValue() & 1) == 1 );
}
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
"ISREF" :function(){
var r = new cBaseFunction("ISREF");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
if ( (arg[0] instanceof cRef || arg[0] instanceof cArea || arg[0] instanceof cArea3D|| arg[0] instanceof cRef3D) && arg[0].isValid && arg[0].isValid() )
return this.value = new cBool( true );
else return this.value = new cBool( false );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"ISTEXT" :function(){
var r = new cBaseFunction("ISTEXT");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef ){
arg0 = arg0.getValue();
}
if( arg0 instanceof cString )
return this.value = new cBool( true );
else
return this.value = new cBool( false );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
},
"N" :function(){
var r = new cBaseFunction("N");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
var arr = new cArray();
arg.foreach(function(elem,r,c){
if( elem instanceof cNumber || elem instanceof cError )
arr.array[r][c] = elem;
else if ( elem instanceof cBool )
arr.array[r][c] = elem.tocNumber();
else
arr.array[r][c] = new cNumber( 0 );
})
return this.value = arr;
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
arg0 = arg0.getValue();
}
if( arg0 instanceof cNumber || arg0 instanceof cError )
return this.value = arg0;
else if ( arg0 instanceof cBool )
return this.value = arg0.tocNumber();
else
return this.value = new cNumber( 0 );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
"NA" :function(){
var r = new cBaseFunction("NA");
r.setArgumentsMin(0);
r.setArgumentsMax(0);
r.Calculate = function(){
return this.value = new cError(cErrorType.not_available);
}
r.getInfo = function(){
return {
name:this.name,
args:"()"
};
}
return r;
},
"TYPE" :function(){
var r = new cBaseFunction("TYPE");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cRef ){
arg0 = arg0.getValue();
}
if( arg0 instanceof cNumber )
return this.value = new cNumber ( 1 );
else if( arg0 instanceof cString )
return this.value = new cNumber ( 2 )
else if( arg0 instanceof cBool )
return this.value = new cNumber ( 4 )
else if( arg0 instanceof cError )
return this.value = new cNumber ( 16 )
else
return this.value = new cNumber( 64 );
}
r.getInfo = function(){
return {
name:this.name,
args:"(value)"
};
}
return r;
}
}
\ No newline at end of file
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 15:22
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.Logical = {
'groupName' : "Logical",
'AND' : function(){
var r = new cBaseFunction("AND");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var argResult = null;
for (var i = 0; i < arg.length; i++){
if ( arg[i] instanceof cArea || arg[i] instanceof cArea3D ){
var argArr = arg[i].getValue();
for( var j=0; j < argArr.length; j++ ){
if (argArr[j] instanceof cString || argArr[j] instanceof cEmpty) continue;
else if ( argArr[j] instanceof cError ) return this.value = argArr[j];
else {
if ( argResult == null )
argResult = argArr[j].tocBool();
else
argResult = new cBool ( argResult.value && argArr[j].tocBool().value );
if ( argResult.value == false ) return this.value = new cBool( false );
}
}
}
else{
if (arg[i] instanceof cString) return this.value = new cError( cErrorType.wrong_value_type );
else if ( arg[i] instanceof cError ) { return this.value = arg[i]; }
else if( arg[i] instanceof cArray ){
var thas = this;
arg[i].foreach(function(elem){
if ( elem instanceof cError ) {
argResult = elem;
return true;
}
else if (elem instanceof cString || elem instanceof cEmpty) { return; }
else{
if ( argResult == null )
argResult = elem.tocBool();
else
argResult = new cBool ( argResult.value && elem.tocBool().value );
if ( argResult.value == false ){
return true;
}
}
})
}
else {
if ( argResult == null )
argResult = arg[i].tocBool();
else
argResult = new cBool ( argResult.value && arg[i].tocBool().value );
if ( argResult.value == false ) return this.value = new cBool( false );
}
}
}
if ( argResult == null )
return this.value = new cError( cErrorType.wrong_value_type );
return this.value = argResult;
}
r.getInfo = function(){
return {
name:this.name,
args:"(logical1, logical2, ...)"
};
}
return r;
},
'FALSE' : function(){
var r = new cBaseFunction("FALSE");
r.setArgumentsMin(0);
r.setArgumentsMax(0);
r.Calculate = function(){
return this.value = new cBool(false);
}
r.getInfo = function(){
return {
name:this.name,
args:"()"
};
}
return r;
},
'IF' : function(){
var r = new cBaseFunction("IF");
r.setArgumentsMin(1);
r.setArgumentsMax(3);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2];
if( arg0 instanceof cArray )
arg0 = arg0.getElement(0);
if( arg1 instanceof cArray )
arg1 = arg1.getElement(0);
if( arg2 instanceof cArray )
arg2 = arg2.getElement(0);
if ( arg0 instanceof cError )
return this.value = arg0;
else{
arg0 = arg0.tocBool();
if( arg0 instanceof cString )
return this.value = new cError( cErrorType.wrong_value_type );
else if ( arg0.value )
return this.value = arg1 ?
arg1 instanceof cEmpty ?
new cNumber(0) :
arg1 :
new cBool(true) ;
else return this.value = arg2 ?
arg2 instanceof cEmpty ?
new cNumber(0) :
arg2 :
new cBool(false) ;
}
}
r.getInfo = function(){
return {
name:this.name,
args:"(logical_test, value_if_true, value_if_false)"
};
}
return r;
},
'IFERROR' : function(){
var r = new cBaseFunction("IFERROR");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
arg0 = arg0.getElement(0);
}
if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
arg0 = arg0.getValue();
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if (arg0 instanceof cError)
return this.value = arg[1] instanceof cArray ? arg[1].getElement(0) : arg[1];
else return this.value = arg[0];
}
r.getInfo = function(){
return {
name:this.name,
args:"(value, value_if_error)"
};
}
return r;
},
'NOT' : function(){
var r = new cBaseFunction("NOT");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray )
arg0 = arg0.getElement(0);
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if ( arg0 instanceof cString ){
var res = arg0.tocBool();
if( res instanceof cString )
return this.value = new cError( cErrorType.wrong_value_type );
else
return this.value = new cBool( ! res.value );
}
else if( arg0 instanceof cError )
return this.value = arg0;
else
return this.value = new cBool( ! arg0.tocBool().value );
}
r.getInfo = function(){
return {
name:this.name,
args:"(logical)"
};
}
return r;
},
'OR' : function(){
var r = new cBaseFunction("OR");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var argResult = null;
for (var i = 0; i < arg.length; i++){
if ( arg[i] instanceof cArea || arg[i] instanceof cArea3D ){
var argArr = arg[i].getValue();
for( var j=0; j < argArr.length; j++ ){
if (argArr[j] instanceof cString || argArr[j] instanceof cEmpty) continue;
else if ( argArr[j] instanceof cError ) return this.value = argArr[j];
else {
if ( argResult == null )
argResult = argArr[j].tocBool();
else
argResult = new cBool ( argResult.value || argArr[j].tocBool().value );
if ( argResult.value === true ) return this.value = new cBool( true );
}
}
}
else{
if (arg[i] instanceof cString) return this.value = new cError( cErrorType.wrong_value_type );
else if ( arg[i] instanceof cError ) return this.value = arg[i];
else if( arg[i] instanceof cArray ){
var thas = this;
arg[i].foreach(function(elem){
if ( elem instanceof cError ) {
argResult = elem;
return true;
}
else if (elem instanceof cString || elem instanceof cEmpty) { return; }
else{
if ( argResult == null )
argResult = elem.tocBool();
else
argResult = new cBool ( argResult.value || elem.tocBool().value );
}
})
}
else {
if ( argResult == null )
argResult = arg[i].tocBool();
else
argResult = new cBool ( argResult.value || arg[i].tocBool().value );
if ( argResult.value === true ) return this.value = new cBool( true );
}
}
}
if ( argResult == null )
return this.value = new cError( cErrorType.wrong_value_type );
return this.value = argResult;
}
r.getInfo = function(){
return {
name:this.name,
args:"(logical1, logical2, ...)"
};
}
return r;
},
'TRUE' : function(){
var r = new cBaseFunction("TRUE");
r.setArgumentsMin(0);
r.setArgumentsMax(0);
r.Calculate = function(){
return this.value = new cBool(true);
}
r.getInfo = function(){
return {
name:this.name,
args:"()"
};
}
return r;
}
}
\ No newline at end of file
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 15:21
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.LookupAndReference = {
'groupName' : "LookupAndReference",
'ADDRESS' : function(){
var r = new cBaseFunction("ADDRESS");
r.setArgumentsMin(2);
r.setArgumentsMax(5);
r.Calculate = function(arg){
function _getColumnTitle(col) {
var q = col < 26 ? undefined : Asc.floor(col / 26) - 1;
var r = col % 26;
var text = String.fromCharCode( ("A").charCodeAt(0) + r );
return col < 26 ? text : _getColumnTitle(q) + text;
}
function _getRowTitle(row) {
return "" + (row + 1);
}
var rowNumber = arg[0], colNumber = arg[1],
refType = arg[2] ? arg[2] : new cNumber(1),
A1RefType = arg[3] ? arg[3] : new cBool(true),
sheetName = arg[4] ? arg[4] : new cEmpty();
if ( rowNumber instanceof cArea || rowNumber instanceof cArea3D ){
rowNumber = rowNumber.cross(arguments[1].first);
}
else if( rowNumber instanceof cArray ){
rowNumber = rowNumber.getElementRowCol(0,0);
}
if ( colNumber instanceof cArea || colNumber instanceof cArea3D ){
colNumber = colNumber.cross(arguments[1].first);
}
else if( colNumber instanceof cArray ){
colNumber = colNumber.getElementRowCol(0,0);
}
if ( refType instanceof cArea || refType instanceof cArea3D ){
refType = refType.cross(arguments[1].first);
}
else if( refType instanceof cArray ){
refType = refType.getElementRowCol(0,0);
}
if ( A1RefType instanceof cArea || A1RefType instanceof cArea3D ){
A1RefType = A1RefType.cross(arguments[1].first);
}
else if( A1RefType instanceof cArray ){
A1RefType = A1RefType.getElementRowCol(0,0);
}
if ( sheetName instanceof cArea || sheetName instanceof cArea3D ){
sheetName = sheetName.cross(arguments[1].first);
}
else if( sheetName instanceof cArray ){
sheetName = sheetName.getElementRowCol(0,0);
}
rowNumber = rowNumber.tocNumber();
colNumber = colNumber.tocNumber();
refType = refType.tocNumber();
A1RefType = A1RefType.tocBool();
if ( rowNumber instanceof cError ) return this.value = rowNumber;
if ( colNumber instanceof cError ) return this.value = colNumber;
if ( refType instanceof cError ) return this.value = refType;
if ( A1RefType instanceof cError ) return this.value = A1RefType;
if ( sheetName instanceof cError ) return this.value = sheetName;
if( refType.getValue() > 4 && refType.getValue() < 1 || rowNumber.getValue() < 1 || colNumber.getValue() < 1 ){
return this.value = new cError( cErrorType.not_numeric );
}
var strRef;
switch( refType.getValue() ){
case 1: strRef = "$" + _getColumnTitle( colNumber.getValue()-1 ) + "$" + _getRowTitle( rowNumber.getValue()-1 ); break;
case 2: strRef = _getColumnTitle( colNumber.getValue()-1 ) + "$" + _getRowTitle( rowNumber.getValue()-1 ); break;
case 3: strRef = "$" + _getColumnTitle( colNumber.getValue()-1 ) + _getRowTitle( rowNumber.getValue()-1 ); break;
case 4: strRef = _getColumnTitle( colNumber.getValue()-1 ) + _getRowTitle( rowNumber.getValue()-1 ); break;
}
if( sheetName instanceof cEmpty ){
return this.value = new cString( strRef );
}
else{
if( !rx_test_ws_name.test(sheetName.toString()) ){
return this.value = "'"+sheetName.toString().replace(/'/g,"''")+"'"+"!"+strRef;
}
else{
return this.value = sheetName.toString()+"!"+strRef;
}
}
}
r.getInfo = function(){
return {
name:this.name,
args:"( row-number , col-number [ , [ ref-type ] [ , [ A1-ref-style-flag ] [ , sheet-name ] ] ] )"
};
}
return r;
},
'AREAS' : function(){
var r = new cBaseFunction("AREAS");
return r;
},
'CHOOSE' : function(){
var r = new cBaseFunction("CHOOSE");
r.setArgumentsMin(2);
r.setArgumentsMax(30);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError ){
return this.value = arg0;
}
if( arg0 instanceof cNumber ){
if( arg0.getValue() < 1 || arg0.getValue() > this.getArguments() ){
return this.value = new cError( cErrorType.wrong_value_type );
}
return this.value = arg[arg0.getValue()];
}
return this.value = new cError( cErrorType.wrong_value_type );
}
r.getInfo = function(){
return {
name:this.name,
args:"( index , argument-list )"
};
}
return r;
},
'COLUMN' : function(){
var r = new cBaseFunction("COLUMN");
r.setArgumentsMin(0);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0;
if( this.argumentsCurrent == 0 ){
arg0 = arguments[1];
return this.value = new cNumber( arg0.getFirst().getCol() );
}
arg0 = arg[0];
if( arg0 instanceof cRef || arg0 instanceof cRef3D || arg0 instanceof cArea ){
var range = arg0.getRange();
if( range )
return this.value = new cNumber( range.getFirst().getCol() );
else
return this.value = new cError( cErrorType.bad_reference );
}
else if( arg0 instanceof cArea3D ){
var r = arg0.getRange();
if( r && r[0] && r[0].getFirst() ){
return this.value = new cNumber( r[0].getFirst().getCol() );
}
else{
return this.value = new cError( cErrorType.bad_reference );
}
}
else
return this.value = new cError( cErrorType.bad_reference );
}
r.getInfo = function(){
return {
name:this.name,
args:"( [ reference ] )"
};
}
return r;
},
'COLUMNS' : function(){
var r = new cBaseFunction("COLUMNS");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
return this.value = new cNumber(arg0.getCountElementInRow());
}
else if( arg0 instanceof cArea || arg0 instanceof cRef || arg0 instanceof cRef3D ){
var range = arg0.getRange();
return this.value = new cNumber( Math.abs( range.getBBox().c1 - range.getBBox().c2 ) + 1 );
}
else if( arg0 instanceof cArea3D ){
var range = arg0.getRange();
if( range.length > 1 )
return this.value = new cError( cErrorType.wrong_value_type );
return this.value = new cNumber( Math.abs(range[0].getBBox().c1 - range[0].getBBox().c2) + 1 );
}
else
return this.value = new cError( cErrorType.wrong_value_type );
}
r.getInfo = function(){
return {
name:this.name,
args:"( array )"
};
}
return r;
},
'GETPIVOTDATA' : function(){
var r = new cBaseFunction("GETPIVOTDATA");
return r;
},
'HLOOKUP' : function(){
var r = new cBaseFunction("HLOOKUP");
r.setArgumentsMin(3);
r.setArgumentsMax(4);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], arg3 = this.argumentsCurrent == 4 ? arg[3].tocBool() : new cBool( true );
var numberRow = arg2.getValue()-1, valueForSearching = arg0.getValue(), resC = -1, min, regexp;
if( isNaN( numberRow ) )
return this.value = new cError( cErrorType.bad_reference );
if( numberRow < 0 )
return this.value = new cError( cErrorType.wrong_value_type );
if( arg0 instanceof cString ){
valueForSearching = arg0.getValue();
valueForSearching = valueForSearching
.replace(/(~)?\*/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]*';
})
.replace(/(~)?\?/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]{1,1}';
})
.replace(/\~/g, "\\");
regexp = new XRegExp(valueForSearching+"$","i");
}
else if( arg0 instanceof cError )
return this.value = arg0;
else{
valueForSearching = arg0.getValue();
}
var found = false, bb,
f = function(cell, r, c, r1, c1){
if( r == r1 ){
var cv = cell.getValueWithoutFormat();
if( c == c1 )
min = cv;
if( arg3.value == true ){
if( valueForSearching == cv ){
resC = c;
found = true;
}
else if( valueForSearching > cv && !found ){
resC = c;
}
}
else{
if( arg0 instanceof cString ){
if( regexp.test(cv) )
resC = c;
}
else if( valueForSearching == cv ){
resC = c;
}
}
if( resC > -1 ){
min = Math.min( min , cv );
if( arg3.value == false )
return true;
}
}
};
if( arg1 instanceof cRef || arg1 instanceof cRef3D || arg1 instanceof cArea ){
var range = arg1.getRange();
bb = range.getBBox0();
if( numberRow > bb.r2-bb.r1 )
return this.value = new cError( cErrorType.bad_reference );
range._foreachColNoEmpty(/*func for col*/ null, /*func for cell in col*/ f);
}
else if( arg1 instanceof cArea3D ){
var range = arg1.getRange()[0];
bb = range.getBBox0();
if( numberRow > bb.r2-bb.r1 )
return this.value = new cError( cErrorType.bad_reference );
range._foreachColNoEmpty(/*func for col*/ null, /*func for cell in col*/ f);
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
if( c == 0 )
min = elem.getValue();
if( arg3.value == true ){
if( valueForSearching == elem.getValue() ){
resC = c;
found = true;
}
else if( valueForSearching > elem.getValue() && !found ){
resC = c;
}
}
else{
if( arg0 instanceof cString ){
if( regexp.test(elem.getValue()) )
resC = c;
}
else if( valueForSearching == elem.getValue() ){
resC = c;
}
}
min = Math.min( min , elem.getValue() );
})
if ( min > valueForSearching ){
return this.value = new cError( cErrorType.not_available );
}
if( resC == -1 ){
return this.value = new cError( cErrorType.not_available );
}
if( numberRow > arg1.getRowCount()-1 ){
return this.value = new cError( cErrorType.bad_reference );
}
return this.value = arg1.getElementRowCol(numberRow,resC);
}
if ( min > valueForSearching ){
return this.value = new cError( cErrorType.not_available );
}
if( resC == -1 ){
return this.value = new cError( cErrorType.not_available );
}
var c = new CellAddress(bb.r1+numberRow,resC,0);
var v = arg1.getWS()._getCellNoEmpty(c.getRow0(),c.getCol0()).getValueWithoutFormat();
return this.value = checkTypeCell(v);
}
r.getInfo = function(){
return {
name:this.name,
args:"( lookup-value , table-array , row-index-num [ , [ range-lookup-flag ] ] )"
};
}
return r;
},
'HYPERLINK' : function(){
var r = new cBaseFunction("HYPERLINK");
return r;
},
'INDEX' : function(){
var r = new cBaseFunction("INDEX");
return r;
},
'INDIRECT' : function(){
var r = new cBaseFunction("INDIRECT");
r.setArgumentsMin(0);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0].tocString(), r = arguments[1], wb = r.worksheet.workbook, o = { Formula:"", pCurrPos : 0 }, ref, found_operand;
function parseReference() {
if( (ref = parserHelp.is3DRef.call(o,o.Formula,o.pCurrPos))[0] ){
var _wsFrom = ref[1],
_wsTo = ( (ref[2] !== null) && (ref[2] !== undefined) )? ref[2] : _wsFrom;
if( !(wb.getWorksheetByName( _wsFrom ) && wb.getWorksheetByName( _wsTo )) ){
return this.value = new cError( cErrorType.bad_reference );
}
if ( parserHelp.isArea.call(o,o.Formula,o.pCurrPos) ){
found_operand = new cArea3D(o.operand_str.toUpperCase(), _wsFrom, _wsTo, wb);
if(o.operand_str.indexOf("$") > -1)
found_operand.isAbsolute = true;
}
else if ( parserHelp.isRef.call(o,o.Formula,o.pCurrPos) ){
if( _wsTo != _wsFrom ){
found_operand = new cArea3D(o.operand_str.toUpperCase(), _wsFrom, _wsTo, wb);
}
else{
found_operand = new cRef3D(o.operand_str.toUpperCase(), _wsFrom, wb);
}
if(o.operand_str.indexOf("$") > -1)
found_operand.isAbsolute = true;
}
}
else if ( parserHelp.isName.call(o,o.Formula,o.pCurrPos, wb)[0] ){
found_operand = new cName(o.operand_str, wb);
}
else if ( parserHelp.isArea.call(o,o.Formula,o.pCurrPos) ){
found_operand = new cArea(o.operand_str.toUpperCase(), r.worksheet);
if(o.operand_str.indexOf("$") > -1)
found_operand.isAbsolute = true;
}
else if ( parserHelp.isRef.call(o,o.Formula,o.pCurrPos,true) ){
found_operand = new cRef(o.operand_str.toUpperCase(), r.worksheet);
if(o.operand_str.indexOf("$") > -1)
found_operand.isAbsolute = true;
}
}
if( arg0 instanceof cArray ){
var ret = new cArray();
arg0.foreach(function(elem,r,c){
o = { Formula:elem.toString(), pCurrPos : 0 };
parseReference();
if(!ret.array[r])
ret.addRow();
ret.addElement(found_operand)
})
return this.value = ret;
}
else {
o.Formula = arg0.toString();
parseReference();
}
if( found_operand )
return this.value = found_operand;
return this.value = new cError( cErrorType.bad_reference );
}
r.getInfo = function(){
return {
name:this.name,
args:"( ref-text [ , [ A1-ref-style-flag ] ] )"
};
}
return r;
},
'LOOKUP' : function(){
var r = new cBaseFunction("LOOKUP");
r.setArgumentsMin(2);
r.setArgumentsMax(3);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = this.argumentsCurrent == 2 ? arg1 : arg[2],
valueForSearching, resC = -1, resR = -1;
if( arg0 instanceof cError ){
return this.value = arg0;
}
if( arg0 instanceof cRef ){
arg0 = arg0.tryConvert();
}
function arrFinder(arr){
if( arr.getRowCount() > arr.getCountElementInRow() ){
//ищем в первом столбце
resC = arr.getCountElementInRow()>1?1:0;
var arrCol = arr.getCol(0);
resR = _func.binarySearch( arg0, arrCol);
}
else{
//ищем в первой строке
resR = arr.getRowCount()>1?1:0;
var arrRow = arr.getRow(0);
resC = _func.binarySearch( arg0, arrRow);
}
}
if( !( arg1 instanceof cArea || arg1 instanceof cArea3D || arg1 instanceof cArray || arg2 instanceof cArea || arg2 instanceof cArea3D || arg2 instanceof cArray) ){
return this.value = new cError( cErrorType.not_available );
}
if( arg1 instanceof cArray && arg2 instanceof cArray ){
if( arg1.getRowCount() != arg2.getRowCount() && arg1.getCountElementInRow() != arg2.getCountElementInRow() ){
return this.value = new cError( cErrorType.not_available );
}
arrFinder(arg1);
if( resR <= -1 && resC <= -1 || resR <= -2 || resC <= -2 ){
return this.value = new cError( cErrorType.not_available );
}
return this.value = arg2.getElementRowCol(resR,resC);
}
else if( arg1 instanceof cArray || arg2 instanceof cArray ){
var _arg1, _arg2;
_arg1 = arg1 instanceof cArray ? arg1 : arg2;
_arg2 = arg2 instanceof cArray ? arg1 : arg2;
var BBox = _arg2.getBBox();
if( _arg1.getRowCount() != (BBox.r2-BBox.r1) && _arg1.getCountElementInRow() != (BBox.c2-BBox.c1) ){
return this.value = new cError( cErrorType.not_available );
}
arrFinder(_arg1);
if( resR <= -1 && resC <= -1 || resR <= -2 || resC <= -2 ){
return this.value = new cError( cErrorType.not_available );
}
var c = new CellAddress(BBox.r1+resR,BBox.c1+resC)
return this.value = checkTypeCell( _arg2.getWS()._getCellNoEmpty(c.getRow0(),c.getCol0()).getValueWithoutFormat() );
}
else{
var arg1Range = arg1.getRange(), arg2Range = arg2.getRange();
if( arg1 instanceof cArea3D && arg1Range.length > 1 || arg2 instanceof cArea3D && arg2Range.length > 1 )
return this.value = new cError( cErrorType.not_available );
if( arg1 instanceof cArea3D )
arg1Range = arg1Range[0];
if( arg2 instanceof cArea3D )
arg2Range = arg2Range[0];
var oBBox1 = arg1Range.getBBox0(), oBBox2 = arg2Range.getBBox0();
if( !(oBBox1.r1 == oBBox1.r2 || oBBox1.c1 == oBBox1.c2) && !(oBBox2.r1 == oBBox2.r2 || oBBox2.c1 == oBBox2.c2) ){
return this.value = new cError( cErrorType.not_available );
}
var index;
if( (oBBox1.r1 == oBBox1.r2 && ( (oBBox2.r1 == oBBox2.r2 && oBBox1.r1 - oBBox1.r2 == oBBox2.r1 - oBBox2.r2) || (oBBox2.c1 == oBBox2.c2 && oBBox1.r1 - oBBox1.r2 == oBBox2.c1 - oBBox2.c2)))
||
(oBBox1.c1 == oBBox1.c2 && ( (oBBox2.r1 == oBBox2.r2 && oBBox1.c1 - oBBox1.c2 == oBBox2.r1 - oBBox2.r2) || (oBBox2.c1 == oBBox2.c2 && oBBox1.c1 - oBBox1.c2 == oBBox2.c1 - oBBox2.c2)))
){
index = _func.binarySearch( arg0, arg1.getValue() )
if( index < 0 ) return this.value = new cError( cErrorType.not_available );
}
else{
return this.value = new cError( cErrorType.not_available );
}
return this.value = arg2.getValue()[index];
}
}
r.getInfo = function(){
return {
name:this.name,
args:"( lookup-value , lookup-vector , result-vector )"
};
}
return r;
},
'MATCH' : function(){
var r = new cBaseFunction("MATCH");
return r;
},
'OFFSET' : function(){
var r = new cBaseFunction("OFFSET");
return r;
},
'ROW' : function(){
var r = new cBaseFunction("ROW");
r.setArgumentsMin(0);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0;
if( this.argumentsCurrent == 0 ){
arg0 = arguments[1];
return this.value = new cNumber( arg0.getFirst().getRow() );
}
arg0 = arg[0];
if( arg0 instanceof cRef || arg0 instanceof cRef3D || arg0 instanceof cArea ){
var range = arg0.getRange();
if( range )
return this.value = new cNumber( range.getFirst().getRow() );
else
return this.value = new cError( cErrorType.bad_reference );
}
else if( arg0 instanceof cArea3D ){
var r = arg0.getRange();
if( r && r[0] && r[0].getFirst() ){
return this.value = new cNumber( r[0].getFirst().getRow() );
}
else{
return this.value = new cError( cErrorType.bad_reference );
}
}
else
return this.value = new cError( cErrorType.bad_reference );
}
r.getInfo = function(){
return {
name:this.name,
args:"( [ reference ] )"
};
}
return r;
},
'ROWS' : function(){
var r = new cBaseFunction("ROWS");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArray ){
return this.value = new cNumber(arg0.getRowCount());
}
else if( arg0 instanceof cArea || arg0 instanceof cRef || arg0 instanceof cRef3D ){
var range = arg0.getRange();
return this.value = new cNumber( Math.abs( range.getBBox().r1 - range.getBBox().r2 ) + 1 );
}
else if( arg0 instanceof cArea3D ){
var range = arg0.getRange();
if( range.length > 1 )
return this.value = new cError( cErrorType.wrong_value_type );
return this.value = new cNumber( Math.abs(range[0].getBBox().r1 - range[0].getBBox().r2) + 1 );
}
else
return this.value = new cError( cErrorType.wrong_value_type );
}
r.getInfo = function(){
return {
name:this.name,
args:"( array )"
};
}
return r;
},
'RTD' : function(){
var r = new cBaseFunction("RTD");
return r;
},
'TRANSPOSE' : function(){
var r = new cBaseFunction("TRANSPOSE");
return r;
},
'VLOOKUP' : function(){
var r = new cBaseFunction("VLOOKUP");
r.setArgumentsMin(3);
r.setArgumentsMax(4);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], arg3 = this.argumentsCurrent == 4 ? arg[3].tocBool() : new cBool( true );
var numberCol = arg2.getValue()-1, valueForSearching, resR = -1, min, regexp;
if( isNaN( numberCol ) )
return this.value = new cError( cErrorType.bad_reference );
if( numberCol < 0 )
return this.value = new cError( cErrorType.wrong_value_type );
if( arg0 instanceof cString ){
valueForSearching = arg0.getValue();
valueForSearching = valueForSearching
.replace(/(~)?\*/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]*';
})
.replace(/(~)?\?/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]{1,1}';
})
.replace(/\~/g, "\\");
regexp = new XRegExp(valueForSearching+"$","i");
}
else if( arg0 instanceof cError )
return this.value = arg0;
else{
valueForSearching = arg0.getValue();
}
var found = false, bb,
f = function(cell, r, c, r1, c1){
if( c == c1 ){
var cv = cell.getValueWithoutFormat();
if( r == r1 )
min = cv;
if( arg3.value == true ){
if( valueForSearching == cv ){
resR = r;
found = true;
}
else if( valueForSearching > cv && !found ){
resR = r;
}
}
else{
if( arg0 instanceof cString ){
if( regexp.test(cv) )
resR = r;
}
else if( valueForSearching == cv ){
resR = r;
}
}
if( resR > -1 ){
min = Math.min( min , cv );
if( arg3.value == false )
return true;
}
}
};
if( arg1 instanceof cRef || arg1 instanceof cRef3D || arg1 instanceof cArea ){
var range = arg1.getRange();
bb = range.getBBox0();
if( numberCol > bb.c2-bb.c1 )
return this.value = new cError( cErrorType.bad_reference );
range._foreachRowNoEmpty(/*func for col*/ null, /*func for cell in col*/ f);
}
else if( arg1 instanceof cArea3D ){
var range = arg1.getRange()[0];
bb = range.getBBox0();
if( numberCol > bb.c2-bb.c1 )
return this.value = new cError( cErrorType.bad_reference );
range._foreachRowNoEmpty(/*func for col*/ null, /*func for cell in col*/ f);
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
if( r == 0 )
min = elem.getValue();
if( arg3.value == true ){
if( valueForSearching == elem.getValue() ){
resR = r;
found = true;
}
else if( valueForSearching > elem.getValue() && !found ){
resR = r;
}
}
else{
if( arg0 instanceof cString ){
if( regexp.test(elem.getValue()) )
resR = r;
}
else if( valueForSearching == elem.getValue() ){
resR = r;
}
}
min = Math.min( min , elem.getValue() );
})
if ( min > valueForSearching ){
return this.value = new cError( cErrorType.not_available );
}
if( resR == -1 ){
return this.value = new cError( cErrorType.not_available );
}
if( numberCol > arg1.getCountElementInRow()-1 ){
return this.value = new cError( cErrorType.bad_reference );
}
return this.value = arg1.getElementRowCol(resR,numberCol);
}
if ( min > valueForSearching ){
return this.value = new cError( cErrorType.not_available );
}
if( resR == -1 ){
return this.value = new cError( cErrorType.not_available );
}
var c = new CellAddress(resR,bb.c1+numberCol,0);
var v = arg1.getWS()._getCellNoEmpty(c.getRow0(),c.getCol0()).getValueWithoutFormat();
return this.value = checkTypeCell(v);
}
r.getInfo = function(){
return {
name:this.name,
args:"( lookup-value , table-array , col-index-num [ , [ range-lookup-flag ] ] )"
};
}
return r;
}
}
\ No newline at end of file
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 15:20
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.Mathematic = {
'groupName' : "Mathematic",
'ABS' : function(){
var r = new cBaseFunction("ABS");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
this.array[r][c] = new cNumber( Math.abs( elem.getValue() ) );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
return this.value = new cNumber( Math.abs( arg0.getValue() ) );
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'ACOS' : function(){
var r = new cBaseFunction("ACOS");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = Math.acos( elem.getValue() );
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
var a = Math.acos( arg0.getValue() );
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'ACOSH' : function(){
var r = new cBaseFunction("ACOSH");
return r;
},
'ASIN' : function(){
var r = new cBaseFunction("ASIN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = Math.asin( elem.getValue() );
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
var a = Math.asin( arg0.getValue() );
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'ASINH' : function(){
var r = new cBaseFunction("ASINH");
return r;
},
'ATAN' : function(){
var r = new cBaseFunction("ATAN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = Math.atan( elem.getValue() );
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
return this.value = arg0;
}
else {
var a = Math.atan( arg0.getValue() );
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'ATAN2' : function(){
var r = new cBaseFunction("ATAN2");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0],arg1 = arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg0.tocNumber(); arg1 = arg1.tocNumber();
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
var a = elem,
b = arg1.getElementRowCol(r,c);
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] =
a.getValue() == 0 && b.getValue() == 0 ?
new cError( cErrorType.division_by_zero ) :
new cNumber( Math.atan2( b.getValue(), a.getValue() ) )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
var a = elem,
b = arg1;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] =
a.getValue() == 0 && b.getValue() == 0 ?
new cError( cErrorType.division_by_zero ) :
new cNumber( Math.atan2( b.getValue(), a.getValue() ) )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
var a = arg0,
b = elem;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] =
a.getValue() == 0 && b.getValue() == 0 ?
new cError( cErrorType.division_by_zero ) :
new cNumber( Math.atan2( b.getValue(), a.getValue() ) )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg1;
}
return this.value = ( arg0 instanceof cError ? arg0 :
arg1 instanceof cError ? arg1 :
arg1.getValue() == 0 && arg0.getValue() == 0 ? new cError( cErrorType.division_by_zero ) :
new cNumber( Math.atan2( arg1.getValue(), arg0.getValue() ) )
)
}
r.getInfo = function(){
return {
name:this.name,
args:"( x, y )"
};
}
return r;
},
'ATANH' : function(){
var r = new cBaseFunction("ATANH");
return r;
},
'CEILING' : function(){
var r = new cBaseFunction("CEILING");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0],arg1 = arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg[0].tocNumber(); arg1 = arg[1].tocNumber();
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
function ceilingHelper( number, significance ){
if ( significance == 0 )
return new cNumber( 0.0 );
if ( ( number > 0 && significance < 0 ) || ( number < 0 && significance > 0 ) )
return new cError( cErrorType.not_numeric );
else if ( number / significance === Infinity )
return new cError( cErrorType.not_numeric );
else{
var quotient = number / significance;
if (quotient == 0){
return new cNumber( 0.0 );
}
var quotientTr = Math.floor(quotient);
var nolpiat = 5 * ( quotient < 0 ? -1.0 : quotient > 0 ? 1.0 : 0.0 ) * Math.pow( 10, Math.floor ( Math.log( Math.abs( quotient ) ) / Math.log( 10 ) ) - cExcelSignificantDigits );
if (Math.abs(quotient - quotientTr) > nolpiat){
++quotientTr;
}
return new cNumber ( quotientTr * significance );
}
}
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
var a = elem,
b = arg1.getElementRowCol(r,c);
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = ceilingHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
var a = elem,
b = arg1;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = ceilingHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
var a = arg0,
b = elem;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = ceilingHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg1;
}
return this.value = ceilingHelper( arg0.getValue(), arg1.getValue() );
}
r.getInfo = function(){
return {
name:this.name,
args:"(number, significance)"
};
}
return r;
},
'COMBIN' : function(){
var r = new cBaseFunction("COMBIN");
return r;
},
'COS' : function (){
var r = new cBaseFunction("COS");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = Math.cos( elem.getValue() );
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
var a = Math.cos( arg0.getValue() );
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
return this.value = arg0;
}
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'COSH' : function(){
var r = new cBaseFunction("COSH");
return r;
},
'DEGREES' : function(){
var r = new cBaseFunction("DEGREES");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = elem.getValue();
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a * 180 / Math.PI );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
var a = arg0.getValue();
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a * 180 / Math.PI );
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'ECMA.CEILING' : function(){
var r = new cBaseFunction("ECMA_CEILING");
return r;
},
'EVEN' : function(){
var r = new cBaseFunction("EVEN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
function evenHelper( arg ){
var arg0 = arg.getValue();
if (arg0 >= 0)
{
arg0 = Math.ceil( arg0 );
if ( (arg0 & 1) == 0 )
return new cNumber ( arg0 );
else
return new cNumber ( arg0 + 1 );
}
else {
arg0 = Math.floor( arg0 );
if ( (arg0 & 1) == 0 )
return new cNumber ( arg0 );
else
return new cNumber ( arg0 - 1 );
}
}
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if ( arg0 instanceof cError )
return this.value = arg0;
if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
this.array[r][c] = evenHelper( elem );
}
else{
this.array[r][c] = new cError ( cErrorType.wrong_value_type );
}
})
return this.value = arg0;
}
else if ( arg0 instanceof cNumber ){
return this.value = evenHelper( arg0 );
}
return this.value = new cError ( cErrorType.wrong_value_type );
}
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'EXP' : function(){
var r = new cBaseFunction("EXP");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = Math.exp( elem.getValue() );
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
if( !(arg0 instanceof cNumber) ){
return this.value = new cError( cErrorType.not_numeric );
}
else {
var a = Math.exp( arg0.getValue() );
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
return this.value = arg0;
}
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'FACT' : function(){
var r = new cBaseFunction("FACT");
return r;
},
'FACTDOUBLE' : function(){
var r = new cBaseFunction("FACTDOUBLE");
return r;
},
'FLOOR' : function(){
var r = new cBaseFunction("FLOOR");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0],arg1 = arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg[0].tocNumber(); arg1 = arg[1].tocNumber();
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
function floorHelper( number, significance ){
if ( significance == 0 )
return new cNumber( 0.0 );
if ( ( number > 0 && significance < 0 ) || ( number < 0 && significance > 0 ) )
return new cError( cErrorType.not_numeric );
else if ( number / significance === Infinity )
return new cError( cErrorType.not_numeric );
else{
var quotient = number / significance;
if (quotient == 0){
return new cNumber( 0.0 );
}
var nolpiat = 5 * ( quotient < 0 ? -1.0 : quotient > 0 ? 1.0 : 0.0 ) * Math.pow( 10, Math.floor ( Math.log( Math.abs( quotient ) ) / Math.log( 10 ) ) - cExcelSignificantDigits );
return new cNumber ( Math.floor(quotient + nolpiat) * significance );
}
}
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1.getElementRowCol(r,c);
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = floorHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = floorHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
var a = arg0;
b = elem;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = floorHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg1;
}
if( arg0 instanceof cString || arg1 instanceof cString )
return this.value = new cError( cErrorType.wrong_value_type );
return this.value = floorHelper( arg0.getValue(), arg1.getValue() );
}
r.getInfo = function(){
return {
name:this.name,
args:"(number, significance)"
};
}
return r;
},
'GCD' : function(){
var r = new cBaseFunction("GCD");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var _gcd = 0;
function gcd(a,b){
var _a = parseInt(a), _b = parseInt(b);
while (_b != 0)
_b = _a % (_a = _b);
return _a;
}
for( var i = 0; i < this.getArguments(); i++ ){
var argI = arg[i];
if( argI instanceof cArea || argI instanceof cArea3D ){
var argArr = argI.getValue();
for( var j = 0; j < argArr.length; j++ ){
if( argArr[j] instanceof cError )
return this.value = argArr[j];
if( argArr[j] instanceof cString )
continue;
if( argArr[j] instanceof cBool )
argArr[j] = argArr[j].tocNumber();
if( argArr[j].getValue() < 0 )
return this.value = new cError( cErrorType.not_numeric );
_gcd = gcd(_gcd,argArr[j].getValue());
}
}
else if( argI instanceof cArray ){
var argArr = argI.tocNumber();
if(
argArr.foreach(function(arrElem){
if( arrElem instanceof cError ){
_gcd = arrElem;
return true;
}
if( arrElem instanceof cBool )
arrElem = arrElem.tocNumber();
if( arrElem instanceof cString )
return;
if( arrElem.getValue() < 0 ){
_gcd = new cError( cErrorType.not_numeric );
return true;
}
_gcd = gcd(_gcd,arrElem.getValue());
})
){
return this.value = _gcd;
}
}
else{
argI = argI.tocNumber();
if( argI.getValue() < 0 )
return this.value = new cError( cErrorType.not_numeric );
if( argI instanceof cError )
return this.value = argI;
_gcd = gcd(_gcd,argI.getValue())
}
}
return this.value = new cNumber( _gcd );
}
r.getInfo = function(){
return {
name:this.name,
args:"( argument-list )"
};
}
return r;
},
'INT' : function(){
var r = new cBaseFunction("INT");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError) return this.value = arg0;
if( arg0 instanceof cString ) this.value = new cError ( cErrorType.wrong_value_type );
if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
this.array[r][c] = new cNumber( Math.floor( elem.getValue() ) )
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
return this.value = new cNumber( Math.floor( arg0.getValue() ) )
}
return this.value = new cNumber( Math.floor( arg0.getValue() ) );
}
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'ISO.CEILING' : function(){
var r = new cBaseFunction("ISO_CEILING");
return r;
},
'LCM' : function(){
var r = new cBaseFunction("LCM");
return r;
},
'LN' : function(){
var r = new cBaseFunction("LN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
if( arg0 instanceof cString )
return this.value = new cError( cErrorType.wrong_value_type );
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
if( elem.getValue() <= 0 )
this.array[r][c] = new cError( cErrorType.not_numeric );
else
this.array[r][c] = new cNumber( Math.log( elem.getValue() ) );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
if( arg0.getValue() <= 0 )
return this.value = new cError( cErrorType.not_numeric );
else
return this.value = new cNumber( Math.log( arg0.getValue() ) );
}
}
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'LOG' : function(){
var r = new cBaseFunction("LOG");
r.setArgumentsMin(1);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0],arg1 = undefined;
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( this.argumentsCurrent == 2 ){
arg1 = arg[1];
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg1 = arg1.tocNumber();
if ( arg1 instanceof cError )
return this.value = arg1;
}
if ( arg0 instanceof cError )
return this.value = arg0;
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1.getElementRowCol(r,c);
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = new cNumber( Math.log ( a.getValue() ) / Math.log ( b.getValue() ) );
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
var a = elem,
b = arg1 ? arg1 : new cNumber( 10 );
if( a instanceof cNumber && b instanceof cNumber ){
if ( a.getValue() <= 0 || a.getValue() <= 0 )
this.array[r][c] = new cError( cErrorType.not_numeric );
this.array[r][c] = new cNumber( Math.log ( a.getValue() ) / Math.log ( b.getValue() ) );
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
var a = arg0,
b = elem;
if( a instanceof cNumber && b instanceof cNumber ){
if ( a.getValue() <= 0 || a.getValue() <= 0 )
this.array[r][c] = new cError( cErrorType.not_numeric );
this.array[r][c] = new cNumber( Math.log ( a.getValue() ) / Math.log ( b.getValue() ) );
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg1;
}
if( !(arg0 instanceof cNumber) || ( arg1 && !(arg0 instanceof cNumber) ) )
return this.value = new cError( cErrorType.wrong_value_type );
if ( arg0.getValue() <= 0 || ( arg1 && arg1.getValue() <= 0 ) )
return this.value = new cError( cErrorType.not_numeric );
if ( this.argumentsCurrent == 1)
return this.value = new cNumber( Math.log( arg0.getValue() ) / Math.log( 10 ) );
else
return this.value = new cNumber( Math.log( arg0.getValue() ) / Math.log( arg1.getValue() ) );
};
r.getInfo = function(){
return {
name:this.name,
args:"(number [ , base ])"
};
}
return r;
},
'LOG10' : function(){
var r = new cBaseFunction("LOG10");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
if( arg0 instanceof cString )
return this.value = new cError( cErrorType.wrong_value_type );
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
if( elem.getValue() <= 0 )
this.array[r][c] = new cError( cErrorType.not_numeric );
else
this.array[r][c] = new cNumber( Math.log( elem.getValue() ) / Math.log( 10 ) );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
if( arg0.getValue() <= 0 )
return this.value = new cError( cErrorType.not_numeric );
else
return this.value = new cNumber( Math.log( arg0.getValue() ) / Math.log( 10 ) );
}
}
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'MDETERM' : function(){
var r = new cBaseFunction("MDETERM");
return r;
},
'MINVERSE' : function(){
var r = new cBaseFunction("MINVERSE");
return r;
},
'MMULT' : function(){
var r = new cBaseFunction("MMULT");
return r;
},
'MOD' : function(){
var r = new cBaseFunction("MOD");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0],arg1 = arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg0.tocNumber(); arg1 = arg1.tocNumber();
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1.getElementRowCol(r,c);
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = new cNumber( (b.getValue() < 0 ? -1 : 1) * ( Math.abs(a.getValue()) % Math.abs(b.getValue()) ) );
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
var a = elem,
b = arg1;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = new cNumber( (b.getValue() < 0 ? -1 : 1) * ( Math.abs(a.getValue()) % Math.abs(b.getValue()) ) );
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
var a = arg0,
b = elem;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = new cNumber( (b.getValue() < 0 ? -1 : 1) * ( Math.abs(a.getValue()) % Math.abs(b.getValue()) ) );
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg1;
}
if( !(arg0 instanceof cNumber) || ( arg1 && !(arg0 instanceof cNumber) ) )
return this.value = new cError( cErrorType.wrong_value_type );
if ( arg1.getValue() == 0 )
return this.value = new cError( cErrorType.division_by_zero );
return this.value = new cNumber( (arg1.getValue() < 0 ? -1 : 1) * ( Math.abs(arg0.getValue()) % Math.abs(arg1.getValue()) ) );
}
r.getInfo = function(){
return {
name:this.name,
args:"(number, divisor)"
};
}
return r;
},
'MROUND' : function(){
var r = new cBaseFunction("MROUND");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var multiple;
function mroundHelper(num){
var multiplier = Math.pow(10, Math.floor(Math.log(Math.abs(num))/Math.log(10)) - cExcelSignificantDigits + 1);
var nolpiat = 0.5 * (num>0?1:num<0?-1:0) * multiplier;
var y = (num + nolpiat) / multiplier;
y = y/Math.abs(y)*Math.floor(Math.abs(y))
var x = y * multiplier / multiple
// var x = number / multiple;
var nolpiat = 5 * (x/Math.abs(x)) * Math.pow(10, Math.floor(Math.log(Math.abs(x))/Math.log(10)) - cExcelSignificantDigits);
x = x + nolpiat;
x = x | x;
return x*multiple;
}
function f(a,b,r,c){
if( a instanceof cNumber && b instanceof cNumber ){
if( a.getValue() == 0 )
this.array[r][c] = new cNumber( 0 );
else if( a.getValue() < 0 && b.getValue() > 0 || arg0.getValue() > 0 && b.getValue() < 0)
this.array[r][c] = new cError( cErrorType.not_numeric );
else{
multiple = b.getValue();
this.array[r][c] = new cNumber( mroundHelper( a.getValue() + b.getValue() / 2 ) )
}
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
var arg0 = arg[0], arg1 = arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
arg1 = arg1.tocNumber();
if( arg0 instanceof cError ) return this.value = arg0;
if( arg1 instanceof cError ) return this.value = arg1;
if( arg0 instanceof cString || arg1 instanceof cString ) { return this.value = new cError( cErrorType.wrong_value_type ); }
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
f.call(this,elem,arg1.getElementRowCol(r,c),r,c)
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
f.call(this,elem,arg1,r,c);
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
f.call(this,arg0,elem,r,c)
})
return this.value = arg1;
}
if( arg1.getValue() == 0 )
return this.value = new cNumber( 0 );
if( arg0.getValue() < 0 && arg1.getValue() > 0 || arg0.getValue() > 0 && arg1.getValue() < 0)
return this.value = new cError( cErrorType.not_numeric );
multiple = arg1.getValue();
return this.value = new cNumber( mroundHelper( arg0.getValue() + arg1.getValue() / 2 ) );
}
r.getInfo = function(){
return {
name:this.name,
args:"(number, multiple)"
};
}
return r;
},
'MULTINOMIAL' : function(){
var r = new cBaseFunction("MULTINOMIAL");
return r;
},
'ODD' : function(){
var r = new cBaseFunction("ODD");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
function oddHelper( arg ){
var arg0 = arg.getValue();
if (arg0 >= 0){
arg0 = Math.ceil( arg0 );
if ( (arg0 & 1) == 1 )
return new cNumber ( arg0 );
else
return new cNumber ( arg0 + 1 );
}
else {
arg0 = Math.floor( arg0 );
if ( (arg0 & 1) == 1 )
return new cNumber ( arg0 );
else
return new cNumber ( arg0 - 1 );
}
}
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if ( arg0 instanceof cError )
return this.value = arg0;
if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
this.array[r][c] = oddHelper( elem );
}
else{
this.array[r][c] = new cError ( cErrorType.wrong_value_type );
}
})
return this.value = arg0;
}
else if ( arg0 instanceof cNumber ){
return this.value = oddHelper( arg0 );
}
return this.value = new cError ( cErrorType.wrong_value_type );
}
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'PI' : function(){
var r = new cBaseFunction("PI");
r.setArgumentsMin(0);
r.setArgumentsMax(0);
r.Calculate = function(){
return new cNumber(Math.PI);
};
r.getInfo = function(){
return {
name:this.name,
args:"()"
}
}
return r;
},
'POWER' : function(){
var r = new cBaseFunction("POWER");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
function powerHelper(a,b){
if ( a == 0 && b < 0 )
return new cError( cErrorType.division_by_zero );
if ( a == 0 && b == 0 )
return new cError( cErrorType.not_numeric );
return new cNumber( Math.pow( a, b ) );
}
function f(a,b,r,c){
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = powerHelper( a.getValue(), b.getValue() );
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
var arg0 = arg[0], arg1 = arg[1];
if( arg0 instanceof cArea || arg1 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg0.tocNumber(); arg1 = arg1.tocNumber();
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
f.call(this,elem, arg1.getElementRowCol(r,c),r,c);
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
f.call(this,elem,arg1,r,c)
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
f.call(this,arg0,elem,r,c);
})
return this.value = arg1;
}
if( !(arg0 instanceof cNumber) || ( arg1 && !(arg0 instanceof cNumber) ) )
return this.value = new cError( cErrorType.wrong_value_type );
return this.value = powerHelper( arg0.getValue(), arg1.getValue() );
};
r.getInfo = function(){
return {
name:this.name,
args:"(number, power)"
};
}
return r;
},
'PRODUCT' : function(){
var r = new cBaseFunction("PRODUCT");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var arg0 = new cNumber(1), _aVal = null;
for(var i = 0; i < arg.length; i++){
if(arg[i] instanceof cArea || arg[i] instanceof cArea3D){
var _arrVal = arg[i].getValue();
for (var j = 0 ; j < _arrVal.length; j++){
arg0 = _func[arg0.type][_arrVal[j].type](arg0,_arrVal[j],"*")
if( arg0 instanceof cError)
return this.value = arg0;
}
}
else if( arg[i] instanceof cRef || arg[i] instanceof cRef3D ){
var _arg = arg[i].getValue();
arg0 = _func[arg0.type][_arg.type](arg0,_arg,"*");
}
else if( arg[i] instanceof cArray ){
arg[i].foreach(function(elem){
if( elem instanceof cString || elem instanceof cBool || elem instanceof cEmpty )
return;
arg0 = _func[arg0.type][elem.type](arg0,elem,"*");
})
}
else{
arg0 = _func[arg0.type][arg[i].type](arg0,arg[i],"*");
}
if( arg0 instanceof cError)
return this.value = arg0;
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"( number1, number2, ... )"
};
}
return r;
},
'QUOTIENT' : function(){
var r = new cBaseFunction("QUOTIENT");
return r;
},
'RADIANS' : function(){
var r = new cBaseFunction("RADIANS");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
function radiansHelper( ang ){
return ang * Math.PI / 180
}
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
this.array[r][c] = new cNumber( radiansHelper( elem.getValue() ) );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else{
return this.value = ( arg0 instanceof cError ? arg0 : new cNumber( radiansHelper( arg0.getValue() ) ) );
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'RAND' : function(){
var r = new cBaseFunction("RAND");
r.setArgumentsMin(0);
r.setArgumentsMax(0);
r.Calculate = function(){
return this.setCA(new cNumber( Math.random() ),true);
};
r.getInfo = function(){
return {
name:this.name,
args:"()"
};
}
return r;
},
'RANDBETWEEN' : function(){
var r = new cBaseFunction("RANDBETWEEN");
return r;
},
'ROMAN' : function(){
var r = new cBaseFunction("ROMAN");
return r;
},
'ROUND' : function(){
var r = new cBaseFunction("ROUND");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
function SignZeroPositive(number){
return number < 0 ? -1 : 1;
}
function truncate(n) {
return Math[n > 0 ? "floor" : "ceil"](n);
}
function sign(n){
return n == 0 ? 0 : n < 0 ? -1 : 1
}
function Floor(number, significance){
var quotient = number / significance;
if (quotient == 0){
return 0;
}
var nolpiat = 5 * sign(quotient) * Math.pow(10, Math.floor( Math.log ( Math.abs(quotient) ) / Math.log ( 10 ) ) - cExcelSignificantDigits);
return truncate(quotient + nolpiat) * significance;
}
function roundHelper(number,num_digits){
if(num_digits > cExcelMaxExponent){
if (Math.abs(number) < 1 || num_digits < 1e10) // The values are obtained experimentally
{
return new cNumber(number);
}
return new cNumber(0);
}
else if (num_digits < cExcelMinExponent)
{
if (Math.abs(number) < 0.01) // The values are obtained experimentally
{
return new cNumber(number);
}
return new cNumber(0);
}
var significance = SignZeroPositive(number) * Math.pow(10, -truncate(num_digits));
number += significance / 2;
if ( number/significance == Infinity){
return new cNumber( number );
}
return new cNumber( Floor(number, significance) );
}
var arg0 = arg[0], arg1 = arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
if( arg0 instanceof cError ) return this.value = arg0;
if( arg1 instanceof cError ) return this.value = arg1;
if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
arg0 = arg0.getValue();
if(arg0 instanceof cError) return this.value = arg0;
else if(arg0 instanceof cString) return this.value = new cError(cErrorType.wrong_value_type);
else arg0 = arg0.tocNumber();
}
else arg0 = arg0.tocNumber();
if( arg1 instanceof cRef || arg1 instanceof cRef3D ){
arg1 = arg1.getValue();
if(arg1 instanceof cError) return this.value = arg1;
else if(arg1 instanceof cString) return this.value = new cError(cErrorType.wrong_value_type);
else arg1 = arg1.tocNumber();
}
else arg1 = arg1.tocNumber();
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1.getElementRowCol(r,c);
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = roundHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = roundHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
var a = arg0;
b = elem;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = roundHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg1;
}
var number = arg0.getValue(), num_digits = arg1.getValue();
return this.value = roundHelper(number,num_digits);
}
r.getInfo = function(){
return {
name:this.name,
args:"(number, num_digits)"
};
}
return r;
},
'ROUNDDOWN' : function(){
var r = new cBaseFunction("ROUNDDOWN");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
function rounddownHelper(number,num_digits){
if(num_digits > cExcelMaxExponent){
if (Math.abs(number) >= 1e-100 || num_digits <= 98303){ // The values are obtained experimentally
return new cNumber(number);
}
return new cNumber(0);
}
else if (num_digits < cExcelMinExponent){
if (Math.abs(number) >= 1e100){ // The values are obtained experimentally
return new cNumber(number);
}
return new cNumber(0);
}
var significance = Math.pow(10, - ( num_digits | num_digits ) );
if (Number.POSITIVE_INFINITY == Math.abs(number / significance)){
return new cNumber(number);
}
var x = number*Math.pow(10, num_digits);
x = x | x;
return new cNumber( x * significance );
}
var arg0 = arg[0], arg1 = arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
if( arg0 instanceof cError ) return this.value = arg0;
if( arg1 instanceof cError ) return this.value = arg1;
if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
arg0 = arg0.getValue();
if(arg0 instanceof cError) return this.value = arg0;
else if(arg0 instanceof cString) return this.value = new cError(cErrorType.wrong_value_type);
else arg0 = arg0.tocNumber();
}
else arg0 = arg0.tocNumber();
if( arg1 instanceof cRef || arg1 instanceof cRef3D ){
arg1 = arg1.getValue();
if(arg1 instanceof cError) return this.value = arg1;
else if(arg1 instanceof cString) return this.value = new cError(cErrorType.wrong_value_type);
else arg1 = arg1.tocNumber();
}
else arg1 = arg1.tocNumber();
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1.getElementRowCol(r,c);
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = rounddownHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = rounddownHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
var a = arg0;
b = elem;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = rounddownHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg1;
}
var number = arg0.getValue(), num_digits = arg1.getValue();
return this.value = rounddownHelper(number,num_digits);
}
r.getInfo = function(){
return {
name:this.name,
args:"(number, num_digits)"
};
}
return r;
},
'ROUNDUP' : function(){
var r = new cBaseFunction("ROUNDUP");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
function roundupHelper(number, num_digits){
if(num_digits > cExcelMaxExponent){
if (Math.abs(number) >= 1e-100 || num_digits <= 98303){ // The values are obtained experimentally
return new cNumber(number);
}
return new cNumber(0);
}
else if (num_digits < cExcelMinExponent){
if (Math.abs(number) >= 1e100){ // The values are obtained experimentally
return new cNumber(number);
}
return new cNumber(0);
}
var significance = Math.pow(10, - ( num_digits | num_digits ) );
if (Number.POSITIVE_INFINITY == Math.abs(number / significance)){
return new cNumber(number);
}
var x = number*Math.pow(10, num_digits);
x = (x | x) + (x>0?1:x<0?-1:0)*1;
return new cNumber( x * significance );
}
var arg0 = arg[0], arg1 = arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
if( arg0 instanceof cError ) return this.value = arg0;
if( arg1 instanceof cError ) return this.value = arg1;
if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
arg0 = arg0.getValue();
if(arg0 instanceof cError) return this.value = arg0;
else if(arg0 instanceof cString) return this.value = new cError(cErrorType.wrong_value_type);
else arg0 = arg0.tocNumber();
}
else arg0 = arg0.tocNumber();
if( arg1 instanceof cRef || arg1 instanceof cRef3D ){
arg1 = arg1.getValue();
if(arg1 instanceof cError) return this.value = arg1;
else if(arg1 instanceof cString) return this.value = new cError(cErrorType.wrong_value_type);
else arg1 = arg1.tocNumber();
}
else arg1 = arg1.tocNumber();
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1.getElementRowCol(r,c);
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = roundupHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = roundupHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
var a = arg0;
b = elem;
if( a instanceof cNumber && b instanceof cNumber ){
this.array[r][c] = roundupHelper( a.getValue(), b.getValue() )
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg1;
}
var number = arg0.getValue(), num_digits = arg1.getValue();
return this.value = roundupHelper(number, num_digits);
}
r.getInfo = function(){
return {
name:this.name,
args:"(number, num_digits)"
};
}
return r;
},
'SERIESSUM' : function(){
var r = new cBaseFunction("SERIESSUM");
return r;
},
'SIGN' : function(){
var r = new cBaseFunction("SIGN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
function signHelper(arg){
if ( arg < 0)
return new cNumber(-1.0);
else if ( arg == 0)
return new cNumber(0.0);
else
return new cNumber(1.0);
}
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = elem.getValue();
this.array[r][c] = signHelper(a)
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
var a = arg0.getValue();
return this.value = signHelper(a);
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'SIN' : function(){
var r = new cBaseFunction("SIN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = Math.sin( elem.getValue() );
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
var a = Math.sin( arg0.getValue() );
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
return this.value = arg0;
}
r.getInfo = function(){
return { name:this.name, args:"(number)" }
}
return r;
},
'SINH' : function(){
var r = new cBaseFunction("SINH");
return r;
},
'SQRT' : function(){
var r = new cBaseFunction("SQRT");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = Math.sqrt( elem.getValue() );
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
var a = Math.sqrt( arg0.getValue() );
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
return this.value = arg0;
}
r.getInfo = function(){
return {
name:this.name,
args:"( number )"
};
}
return r;
},
'SQRTPI' : function(){
var r = new cBaseFunction("SQRTPI");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = Math.sqrt( elem.getValue() * Math.PI );
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
var a = Math.sqrt( arg0.getValue() * Math.PI );
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
return this.value = arg0;
}
r.getInfo = function(){
return {
name:this.name,
args:"( number )"
};
}
return r;
},
'SUBTOTAL' : function(){
var r = new cBaseFunction("SUBTOTAL");
return r;
},
'SUM' : function(){
var r = new cBaseFunction("SUM");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var arg0 = new cNumber(0), _aVal = null;
for(var i = 0; i < arg.length; i++){
if(arg[i] instanceof cArea || arg[i] instanceof cArea3D){
var _arrVal = arg[i].getValue();
for (var j = 0 ; j < _arrVal.length; j++){
if( !(_arrVal[j] instanceof cBool || _arrVal[j] instanceof cString) )
arg0 = _func[arg0.type][_arrVal[j].type](arg0,_arrVal[j],"+")
if( arg0 instanceof cError)
return this.value = arg0;
}
}
else if( arg[i] instanceof cRef || arg[i] instanceof cRef3D ){
var _arg = arg[i].getValue();
if( !(_arg instanceof cBool || _arg instanceof cString) )
arg0 = _func[arg0.type][_arg.type](arg0,_arg,"+");
}
else if( arg[i] instanceof cArray ){
arg[i].foreach(
function(arrElem){
if( !(arrElem instanceof cBool || arrElem instanceof cString || arrElem instanceof cEmpty) )
arg0 = _func[arg0.type][arrElem.type](arg0,arrElem,"+");
}
)
}
else{
var _arg = arg[i].tocNumber();
arg0 = _func[arg0.type][_arg.type](arg0,_arg,"+");
}
if( arg0 instanceof cError)
return this.value = arg0;
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(number1, number2, ...)"
};
}
return r;
},
'SUMIF' : function(){
var r = new cBaseFunction("SUMIF");
r.setArgumentsMin(2);
r.setArgumentsMax(3);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2] ? arg[2] : arg[0], _count = 0, valueForSearching, regexpSearch;
if( !(arg0 instanceof cRef || arg0 instanceof cRef3D || arg0 instanceof cArea) ){
return this.value = new cError( cErrorType.wrong_value_type );
}
if( !(arg2 instanceof cRef || arg2 instanceof cRef3D || arg2 instanceof cArea) ){
return this.value = new cError( cErrorType.wrong_value_type );
}
if ( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
arg1 = arg1.tocString();
if( !(arg1 instanceof cString) ){
return this.value = new cError( cErrorType.wrong_value_type );
}
function matching(x, y, oper, startCell, pos){
var res = false;
if( typeof x === typeof y ){
switch(oper){
case "<>":
res = (x.value != y.value);
break;
case ">":
res = (x.value > y.value);
break;
case "<":
res = (x.value < y.value);
break;
case ">=":
res = (x.value >= y.value);
break;
case "<=":
res = (x.value <= y.value);
break;
case "=":
default:
res = (x.value == y.value);
break;
}
}
return res;
}
arg1 = arg1.toString();
var operators = new RegExp("^ *[<=> ]+ *"), searchOperators = new RegExp("^ *[*?]")
var match = arg1.match(operators);
if( match || parseNum(arg1) ){
var search, oper, val, calcVal;
if( match ){
search = arg1.substr( match[0].length );
oper = match[0].replace(/\s/g,"");
}
else{
search = arg1;
}
valueForSearching = parseNum( search ) ? new cNumber( search ) : new cString( search );
if( arg0 instanceof cArea ){
val = arg0.getValue();
for( var i = 0; i < val.length; i++ ){
if( matching( val[i], valueForSearching, oper) ){
var r = arg0.getRange(), ws = arg0.getWS(),
r1 = r.first.getRow0() + i, c1 = arg2.getRange().first.getCol0();
r = new cRef(ws.getRange3( r1, c1, r1, c1).getName(),ws);
if( r.getValue() instanceof cNumber ){
_count += r.getValue().getValue();
}
}
}
}
else{
val = arg0.getValue();
if( matching( val, valueForSearching, oper) ){
var r = arg0.getRange(), ws = arg0.getWS(),
r1 = r.first.getRow0() + 0, c1 = arg2.getRange().first.getCol0();
r = new cRef(ws.getRange3( r1, c1, r1, c1).getName(),ws);
if( r.getValue() instanceof cNumber ){
_count += r.getValue().getValue();
}
}
}
}
else{
valueForSearching = arg1
.replace(/(~)?\*/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]*';
})
.replace(/(~)?\?/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]{1,1}';
})
.replace(/\~/g, "\\");
regexpSearch = new RegExp(valueForSearching+"$","i");
if( arg0 instanceof cArea ){
val = arg0.getValue();
for( var i = 0; i < val.length; i++ ){
if( regexpSearch.test(val[i].value) ){
var r = arg0.getRange(), ws = arg0.getWS(),
r1 = r.first.getRow0() + i, c1 = arg2.getRange().first.getCol0();
r = new cRef(ws.getRange3( r1, c1, r1, c1).getName(),ws);
if( r.getValue() instanceof cNumber ){
_count += r.getValue().getValue();
}
}
}
}
else{
val = arg0.getValue();
if( regexpSearch.test(val.value) ){
var r = arg0.getRange(), ws = arg0.getWS(),
r1 = r.first.getRow0() + 0, c1 = arg2.getRange().first.getCol0();
r = new cRef(ws.getRange3( r1, c1, r1, c1).getName(),ws);
if( r.getValue() instanceof cNumber ){
_count += r.getValue().getValue();
}
}
}
}
return this.value = new cNumber(_count);
}
r.getInfo = function(){
return {
name:this.name,
args:"( cell-range, selection-criteria )"
};
}
return r;
},
'SUMIFS' : function(){
var r = new cBaseFunction("SUMIFS");
return r;
},
'SUMPRODUCT' : function(){
var r = new cBaseFunction("SUMPRODUCT");
/*r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var arg0 = new cNumber(0), resArr = [];
for(var i = 0; i < arg.length; i++){
if( arg[i] instanceof cArea3D )
return this.value = new cError( bad_reference );
if( arg[i] instanceof cArea ){
function retCell(_cell){
if(!_cell)
return new cNumber(0);
switch( _cell.getType() ){
case CellValueType.Number:
_cell.getValueWithoutFormat() == ""? return new cNumber(0) : return new cNumber( _cell.getValueWithoutFormat() );
case CellValueType.Bool:
return new cBool( _cell.getValueWithoutFormat() ).tocNumber();
case CellValueType.Error:
return new cError( _cell.getValueWithoutFormat() );
case CellValueType.String:
default:
return new cNumber(0);
}
}
var argIBBox = arg[i].getBBox0(),
colCount = Math.abs(argIBBox.c2-argIBBox.c1)+1,
rowCount = Math.abs(argIBBox.r2-argIBBox.r1)+1,
range = arg[i].getRange();
range._foreachIndex(function(i,j){
})
if( resArr.length == 0 ){
for( var i = 0; i < rowCount; i++ ){
resArr.push(new Array(colCount));
}
}
else{
if( resArr.length == rowCount ){
if( resArr[0] == colCount ){
}
else
return this.value = new cError( not_numeric );
}
else
return this.value = new cError( not_numeric );
}
arg[i].foreach(function(oCurCell, i, j, r, c){
if( resArr[i] !== undefined && resArr[i] !== null ){
if( resArr[i][j] !== undefined && resArr[i][j] !== null ){
}
else{
resArr[i][j] = retCell(oCurCell);
}
}
else{
resArr[i] = [];
resArr[i][j] = !oCurCell ? new cNumber(0) :
}
})
}
else if( arg[i] instanceof cRef || arg[i] instanceof cRef3D ){
}
else if( arg[i] instanceof cArray ){
}
else{
}
if( arg[i] instanceof cError )
return this.value = arg0;
}
return this.value = arg0;
}
r.getInfo = function(){
return {
name:this.name,
args:"( argument-lists )"
};
}*/
return r;
},
'SUMSQ' : function(){
var r = new cBaseFunction("SUMSQ");
return r;
},
'SUMX2MY2' : function(){
var r = new cBaseFunction("SUMX2MY2");
return r;
},
'SUMX2PY2' : function(){
var r = new cBaseFunction("SUMX2PY2");
return r;
},
'SUMXMY2' : function(){
var r = new cBaseFunction("SUMXMY2");
return r;
},
'TAN' : function(){
var r = new cBaseFunction("TAN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
if( elem instanceof cNumber ){
var a = Math.tan( elem.getValue() );
this.array[r][c] = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
else{
this.array[r][c] = new cError( cErrorType.wrong_value_type );
}
})
}
else {
var a = Math.tan( arg0.getValue() );
return this.value = isNaN( a ) ? new cError( cErrorType.not_numeric ) : new cNumber( a );
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(number)"
};
}
return r;
},
'TANH' : function(){
var r = new cBaseFunction("TANH");
return r;
},
'TRUNC' : function(){
var r = new cBaseFunction("TRUNC");
return r;
}
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 15:18
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.Statistical = {
'groupName' : "Statistical",
'AVEDEV' : function(){
var r = new cBaseFunction("AVEDEV");
return r;
},
'AVERAGE' : function(){
var r = new cBaseFunction("AVERAGE");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var count = 0, sum = new cNumber(0);
for ( var i = 0; i < arg.length; i++){
var _arg = arg[i];
if( _arg instanceof cRef || _arg instanceof cRef3D){
var _argV = _arg.getValue();
if ( _argV instanceof cNumber ){
sum = _func[sum.type][_argV.type](sum,_argV,"+");
count++;
}
}
else if ( _arg instanceof cArea || _arg instanceof cArea3D){
var _argAreaValue = _arg.getValue();
for ( var j = 0; j < _argAreaValue.length; j++){
var __arg = _argAreaValue[j];
if ( __arg instanceof cNumber ){
sum = _func[sum.type][__arg.type](sum,__arg,"+");
count++;
}
}
}
else if( _arg instanceof cArray ){
_arg.foreach(function(elem){
var e = elem.tocNumber();
if( e instanceof cNumber ){
sum = _func[sum.type][e.type](sum,e,"+");
count++;
}
})
}
else{
if( _arg instanceof cError )
continue;
sum = _func[sum.type][_arg.type](sum,_arg,"+");
count++;
}
}
return this.value = new cNumber (sum.getValue() / count);
};
r.getInfo = function(){
return {
name:this.name,
args:"(number1, number2, ...)"
};
}
return r;
},
'AVERAGEA' : function(){
var r = new cBaseFunction("AVERAGEA");
return r;
},
'AVERAGEIF' : function(){
var r = new cBaseFunction("AVERAGEIF");
return r;
},
'AVERAGEIFS' : function(){
var r = new cBaseFunction("AVERAGEIFS");
return r;
},
'BETADIST' : function(){
var r = new cBaseFunction("BETADIST");
return r;
},
'BETAINV' : function(){
var r = new cBaseFunction("BETAINV");
return r;
},
'BINOMDIST' : function(){
var r = new cBaseFunction("BINOMDIST");
return r;
},
'CHIDIST' : function(){
var r = new cBaseFunction("CHIDIST");
return r;
},
'CHIINV' : function(){
var r = new cBaseFunction("CHIINV");
return r;
},
'CHITEST' : function(){
var r = new cBaseFunction("CHITEST");
return r;
},
'CONFIDENCE' : function(){
var r = new cBaseFunction("CONFIDENCE");
return r;
},
'CORREL' : function(){
var r = new cBaseFunction("CORREL");
return r;
},
'COUNT' : function(){
var r = new cBaseFunction("COUNT");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var count = 0;
for ( var i = 0; i < arg.length; i++){
var _arg = arg[i];
if( _arg instanceof cRef || _arg instanceof cRef3D){
var _argV = _arg.getValue();
if ( _argV instanceof cNumber ){
count++;
}
}
else if ( _arg instanceof cArea || _arg instanceof cArea3D){
var _argAreaValue = _arg.getValue();
for ( var j = 0; j < _argAreaValue.length; j++){
if ( _argAreaValue[j] instanceof cNumber ){
count++;
}
}
}
else if( _arg instanceof cNumber || _arg instanceof cBool || _arg instanceof cEmpty ){
count++;
}
else if( _arg instanceof cString ){
if (_arg.tocNumber() instanceof cNumber)
count++;
}
else if( _arg instanceof cArray ){
_arg.foreach(function(elem){
var e = elem.tocNumber();
if( e instanceof cNumber ){
count++;
}
})
}
}
return this.value = new cNumber( count );
};
r.getInfo = function(){
return {
name:this.name,
args:"( argument-list )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'COUNTA' : function(){
var r = new cBaseFunction("COUNTA");
return r;
},
'COUNTBLANK' : function(){
var r = new cBaseFunction("COUNTBLANK");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D )
return this.value = arg0.countCells();
else if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
return this.value = new cNumber( 1 );
}
else
return this.value = new cError( cErrorType.bad_reference );
}
r.getInfo = function(){
return {
name:this.name,
args:"( argument-list )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'COUNTIF' : function(){
var r = new cBaseFunction("COUNTIF");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], _count = 0, valueForSearching, regexpSearch;
if( !(arg0 instanceof cRef || arg0 instanceof cRef3D || arg0 instanceof cArea || arg0 instanceof cArea3D) ){
return this.value = new cError( cErrorType.wrong_value_type );
}
if ( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
arg1 = arg1.tocString();
if( !(arg1 instanceof cString) ){
return this.value = new cError( cErrorType.wrong_value_type );
}
function matching(x,y,oper){
var res = 0;
if( typeof x === typeof y ){
switch(oper){
case "<>":
res = (x.value != y.value);
break;
case ">":
res = (x.value > y.value);
break;
case "<":
res = (x.value < y.value);
break;
case ">=":
res = (x.value >= y.value);
break;
case "<=":
res = (x.value <= y.value);
break;
case "=":
default:
res = (x.value == y.value);
break;
}
}
_count += res;
}
arg1 = arg1.toString();
var operators = new RegExp("^ *[<=> ]+ *"), searchOperators = new RegExp("^ *[*?]")
var match = arg1.match(operators);
if( match || parseNum(arg1) ){
var search, oper, val;
if( match ){
search = arg1.substr( match[0].length );
oper = match[0].replace(/\s/g,"");
}
else{
search = arg1;
}
valueForSearching = parseNum( search ) ? new cNumber( search ) : new cString( search );
if( arg0 instanceof cArea ){
val = arg0.getValue();
for( var i = 0; i < val.length; i++ ){
matching( val[i], valueForSearching, oper);
}
}
else if( arg0 instanceof cArea3D ){
val = arg0.getValue();
for(var i = 0; i < val.length; i++){
matching( val[i], valueForSearching, oper);
}
}
else{
val = arg0.getValue();
matching( val, valueForSearching, oper);
}
}
else{
match = arg1.match(searchOperators)
if( match ){
valueForSearching = arg1
.replace(/(~)?\*/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]*';
})
.replace(/(~)?\?/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]{1,1}';
})
.replace(/\~/g, "\\");
regexpSearch = new RegExp(valueForSearching+"$","i");
if( arg0 instanceof cArea ){
val = arg0.getValue();
for( var i = 0; i < val.length; i++ ){
_count += regexpSearch.test(val[i].value);
}
}
else if( arg0 instanceof cArea3D ){
val = arg0.getValue();
for(var i in val){
for(var j in val[i]){
_count += regexpSearch.test(val[i][j].value);
}
}
}
else{
val = arg0.getValue();
_count += regexpSearch.test(val.value);
}
}
}
return this.value = new cNumber(_count);
}
r.getInfo = function(){
return {
name:this.name,
args:"( cell-range, selection-criteria )"
};
}
return r;
},
'COUNTIFS' : function(){
var r = new cBaseFunction("COUNTIFS");
return r;
},
'COVAR' : function(){
var r = new cBaseFunction("COVAR");
return r;
},
'CRITBINOM' : function(){
var r = new cBaseFunction("CRITBINOM");
return r;
},
'DEVSQ' : function(){
var r = new cBaseFunction("DEVSQ");
return r;
},
'EXPONDIST' : function(){
var r = new cBaseFunction("EXPONDIST");
return r;
},
'FDIST' : function(){
var r = new cBaseFunction("FDIST");
return r;
},
'FINV' : function(){
var r = new cBaseFunction("FINV");
return r;
},
'FISHER' : function(){
var r = new cBaseFunction("FISHER");
return r;
},
'FISHERINV' : function(){
var r = new cBaseFunction("FISHERINV");
return r;
},
'FORECAST' : function(){
var r = new cBaseFunction("FORECAST");
return r;
},
'FREQUENCY' : function(){
var r = new cBaseFunction("FREQUENCY");
return r;
},
'FTEST' : function(){
var r = new cBaseFunction("FTEST");
return r;
},
'GAMMADIST' : function(){
var r = new cBaseFunction("GAMMADIST");
return r;
},
'GAMMAINV' : function(){
var r = new cBaseFunction("GAMMAINV");
return r;
},
'GAMMALN' : function(){
var r = new cBaseFunction("GAMMALN");
return r;
},
'GEOMEAN' : function(){
var r = new cBaseFunction("GEOMEAN");
return r;
},
'GROWTH' : function(){
var r = new cBaseFunction("GROWTH");
return r;
},
'HARMEAN' : function(){
var r = new cBaseFunction("HARMEAN");
return r;
},
'HYPGEOMDIST' : function(){
var r = new cBaseFunction("HYPGEOMDIST");
return r;
},
'INTERCEPT' : function(){
var r = new cBaseFunction("INTERCEPT");
return r;
},
'KURT' : function(){
var r = new cBaseFunction("KURT");
return r;
},
'LARGE' : function(){
var r = new cBaseFunction("LARGE");
return r;
},
'LINEST' : function(){
var r = new cBaseFunction("LINEST");
return r;
},
'LOGEST' : function(){
var r = new cBaseFunction("LOGEST");
return r;
},
'LOGINV' : function(){
var r = new cBaseFunction("LOGINV");
return r;
},
'LOGNORMDIST' : function(){
var r = new cBaseFunction("LOGNORMDIST");
return r;
},
'MAX' : function(){
var r = new cBaseFunction("MAX");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var argI, argIVal, max = new cNumber ( Number.NEGATIVE_INFINITY );
for ( var i = 0; i < this.argumentsCurrent; i++ ){
argI = arg[i], argIVal = argI.getValue();
if ( argI instanceof cRef || argI instanceof cRef3D ){
if( argIVal instanceof cError )
return this.value = argIVal;
if( argIVal instanceof cNumber || argIVal instanceof cBool || argIVal instanceof cEmpty){
var v = argIVal.tocNumber();
if ( v.getValue() > max.getValue() )
max = v;
}
}
else if ( argI instanceof cArea || argI instanceof cArea3D ){
var argArr = argI.getValue();
for( var j = 0; j < argArr.length; j++ ){
if ( argArr[j] instanceof cNumber || argArr[j] instanceof cBool || argArr[j] instanceof cEmpty){
var v = argArr[j].tocNumber();
if ( v.getValue() > max.getValue() )
max = v;
}
else if ( argArr[j] instanceof cError ){
return this.value = argArr[j];
}
}
}
else if( argI instanceof cError )
return this.value = argI;
else if ( argI instanceof cString ){
var v = argI.tocNumber();
if ( v instanceof cNumber )
if ( v.getValue() > max.getValue() )
max = v;
}
else if ( argI instanceof cBool || argI instanceof cEmpty ){
var v = argI.tocNumber();
if ( v.getValue() > max.getValue() )
max = v;
}
else if( argI instanceof cArray ){
argI.foreach(function(elem){
if( elem instanceof cNumber ){
if ( elem.getValue() > max.getValue() )
max = elem;
}
else if( elem instanceof cError ){
max = elem;
return true;
}
})
if( max instanceof cError ){
return this.value = max;
}
}
else{
if ( argI.getValue() > max.getValue() )
max = argI;
}
}
return this.value = ( max.value === Number.NEGATIVE_INFINITY ? new cNumber(0) : max );
};
r.getInfo = function(){
return {
name:this.name,
args:"(number1, number2, ...)"
};
}
return r;
},
'MAXA' : function(){
var r = new cBaseFunction("MAXA");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var argI, max = new cNumber ( Number.NEGATIVE_INFINITY );
for ( var i = 0; i < this.argumentsCurrent; i++ ){
argI = arg[i], argIVal = argI.getValue();
if ( argI instanceof cRef || argI instanceof cRef3D ){
if( argIVal instanceof cError )
return this.value = argIVal;
var v = argIVal.tocNumber();
if ( v instanceof cNumber && v.getValue() > max.getValue() )
max = v;
}
else if ( argI instanceof cArea || argI instanceof cArea3D ){
var argArr = argI.getValue();
for( var j = 0; j < argArr.length; j++ ){
if( argArr[j] instanceof cError )
return this.value = argArr[j];
var v = argArr[j].tocNumber();
if ( v instanceof cNumber && v.getValue() > max.getValue() )
max = v;
}
}
else if( argI instanceof cError )
return this.value = argI;
else if ( argI instanceof cString ){
var v = argI.tocNumber();
if ( v instanceof cNumber )
if ( v.getValue() > max.getValue() )
max = v;
}
else if ( argI instanceof cBool || argI instanceof cEmpty ){
var v = argI.tocNumber();
if ( v.getValue() > max.getValue() )
max = v;
}
else if( argI instanceof cArray ){
argI.foreach(function(elem){
if( elem instanceof cError ){
max = elem;
return true;
}
elem = elem.tocNumber();
if( elem instanceof cNumber && elem.getValue() > max.getValue() ){
max = elem;
}
})
if( max instanceof cError ){
return this.value = max;
}
}
else{
if ( argI.getValue() > max.getValue() )
max = argI;
}
}
return this.value = ( max.value === Number.NEGATIVE_INFINITY ? new cNumber(0) : max )
};
r.getInfo = function(){
return {
name:this.name,
args:"(number1, number2, ...)"
};
}
return r;
},
'MEDIAN' : function(){
var r = new cBaseFunction("MEDIAN");
return r;
},
'MIN' : function(){
var r = new cBaseFunction("MIN");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var argI, argIVal, min = new cNumber ( Number.POSITIVE_INFINITY );
for ( var i = 0; i < this.argumentsCurrent; i++ ){
argI = arg[i], argIVal = argI.getValue();
if ( argI instanceof cRef || argI instanceof cRef3D ){
if( argIVal instanceof cError )
return this.value = argIVal;
if( argIVal instanceof cNumber || argIVal instanceof cBool || argIVal instanceof cEmpty){
var v = argIVal.tocNumber();
if ( v.getValue() < min.getValue() )
min = v;
}
}
else if ( argI instanceof cArea || argI instanceof cArea3D ){
var argArr = argI.getValue();
for( var j = 0; j < argArr.length; j++ ){
if ( argArr[j] instanceof cNumber || argArr[j] instanceof cBool || argArr[j] instanceof cEmpty){
var v = argArr[j].tocNumber();
if ( v.getValue() < min.getValue() )
min = v;
continue;
}
else if ( argArr[j] instanceof cError ){
return this.value = argArr[j];
}
}
}
else if( argI instanceof cError )
return this.value = argI;
else if ( argI instanceof cString ){
var v = argI.tocNumber();
if ( v instanceof cNumber )
if ( v.getValue() < min.getValue() )
min = v;
}
else if ( argI instanceof cBool || argI instanceof cEmpty){
var v = argI.tocNumber();
if ( v.getValue() < min.getValue() )
min = v;
}
else if( argI instanceof cArray ){
argI.foreach(function(elem){
if( elem instanceof cNumber ){
if ( elem.getValue() < min.getValue() )
min = elem;
}
else if( elem instanceof cError ){
min = elem;
return true;
}
})
if( min instanceof cError ){
return this.value = min;
}
}
else{
if ( argI.getValue() < min.getValue() )
min = argI;
}
}
return this.value = ( min.value === Number.POSITIVE_INFINITY ? new cNumber(0) : min );
};
r.getInfo = function(){
return {
name:this.name,
args:"(number1, number2, ...)"
};
}
return r;
},
'MINA' : function(){
var r = new cBaseFunction("MINA");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var argI, min = new cNumber ( Number.POSITIVE_INFINITY );
for ( var i = 0; i < this.argumentsCurrent; i++ ){
argI = arg[i], argIVal = argI.getValue();
if ( argI instanceof cRef || argI instanceof cRef3D ){
if( argIVal instanceof cError )
return this.value = argIVal;
var v = argIVal.tocNumber();
if ( v instanceof cNumber && v.getValue() < min.getValue() )
min = v;
}
else if ( argI instanceof cArea || argI instanceof cArea3D ){
var argArr = argI.getValue();
for( var j = 0; j < argArr.length; j++ ){
if ( argArr[j] instanceof cError ){
return this.value = argArr[j];
}
var v = argArr[j].tocNumber();
if ( v instanceof cNumber && v.getValue() < min.getValue() )
min = v;
}
}
else if( argI instanceof cError )
return this.value = argI;
else if ( argI instanceof cString ){
var v = argI.tocNumber();
if ( v instanceof cNumber )
if ( v.getValue() < min.getValue() )
min = v;
}
else if ( argI instanceof cBool || argI instanceof cEmpty ){
var v = argI.tocNumber();
if ( v.getValue() < min.getValue() )
min = v;
}
else if( argI instanceof cArray ){
argI.foreach(function(elem){
if( elem instanceof cError ){
min = elem;
return true;
}
elem = elem.tocNumber();
if( elem instanceof cNumber && elem.getValue() < min.getValue() ){
min = elem;
}
})
if( min instanceof cError ){
return this.value = min;
}
}
else{
if ( argI.getValue() < min.getValue() )
min = argI;
}
}
return this.value = ( min.value === Number.POSITIVE_INFINITY ? new cNumber(0) : min );
};
r.getInfo = function(){
return {
name:this.name,
args:"(number1, number2, ...)"
};
}
return r;
},
'MODE' : function(){
var r = new cBaseFunction("MODE");
return r;
},
'NEGBINOMDIST' : function(){
var r = new cBaseFunction("NEGBINOMDIST");
return r;
},
'NORMDIST' : function(){
var r = new cBaseFunction("NORMDIST");
return r;
},
'NORMINV' : function(){
var r = new cBaseFunction("NORMINV");
return r;
},
'NORMSDIST' : function(){
var r = new cBaseFunction("NORMSDIST");
return r;
},
'NORMSINV' : function(){
var r = new cBaseFunction("NORMSINV");
return r;
},
'PEARSON' : function(){
var r = new cBaseFunction("PEARSON");
return r;
},
'PERCENTILE' : function(){
var r = new cBaseFunction("PERCENTILE");
return r;
},
'PERCENTRANK' : function(){
var r = new cBaseFunction("PERCENTRANK");
return r;
},
'PERMUT' : function(){
var r = new cBaseFunction("PERMUT");
return r;
},
'POISSON' : function(){
var r = new cBaseFunction("POISSON");
return r;
},
'PROB' : function(){
var r = new cBaseFunction("PROB");
return r;
},
'QUARTILE' : function(){
var r = new cBaseFunction("QUARTILE");
return r;
},
'RANK' : function(){
var r = new cBaseFunction("RANK");
return r;
},
'RSQ' : function(){
var r = new cBaseFunction("RSQ");
return r;
},
'SKEW' : function(){
var r = new cBaseFunction("SKEW");
return r;
},
'SLOPE' : function(){
var r = new cBaseFunction("SLOPE");
return r;
},
'SMALL' : function(){
var r = new cBaseFunction("SMALL");
return r;
},
'STANDARDIZE' : function(){
var r = new cBaseFunction("STANDARDIZE");
return r;
},
'STDEV' : function(){
var r = new cBaseFunction("STDEV");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var count = 0, sum = new cNumber(0), member = [];
for ( var i = 0; i < arg.length; i++){
var _arg = arg[i];
if( _arg instanceof cRef || _arg instanceof cRef3D){
var _argV = _arg.getValue();
if ( _argV instanceof cNumber ){
member.push(_argV);
sum = _func[sum.type][_argV.type](sum,_argV,"+");
count++;
}
}
else if ( _arg instanceof cArea || _arg instanceof cArea3D){
var _argAreaValue = _arg.getValue();
for ( var j = 0; j < _argAreaValue.length; j++){
var __arg = _argAreaValue[j];
if ( __arg instanceof cNumber ){
member.push(__arg);
sum = _func[sum.type][__arg.type](sum,__arg,"+");
count++;
}
}
}
else if( _arg instanceof cArray ){
_arg.foreach(function(elem){
var e = elem.tocNumber();
if( e instanceof cNumber ){
member.push(e);
sum = _func[sum.type][e.type](sum,e,"+");
count++;
}
})
}
else{
_arg = _arg.tocNumber();
if( _arg instanceof cNumber ){
member.push(_arg);
sum = _func[sum.type][_arg.type](sum,_arg,"+");
count++;
}
}
}
var average = sum.getValue() / count, res = 0, av;
for( var i = 0; i < member.length; i++ ){
av = member[i] - average;
res += av * av;
}
return this.value = new cNumber ( Math.sqrt(res / (count-1)) );STDEV(123,134,143,173,112,109)
};
r.getInfo = function(){
return {
name:this.name,
args:"( argument-list )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
},
'STDEVA' : function(){
var r = new cBaseFunction("STDEVA");
return r;
},
'STDEVP' : function(){
var r = new cBaseFunction("STDEVP");
return r;
},
'STDEVPA' : function(){
var r = new cBaseFunction("STDEVPA");
return r;
},
'STEYX' : function(){
var r = new cBaseFunction("STEYX");
return r;
},
'TDIST' : function(){
var r = new cBaseFunction("TDIST");
return r;
},
'TINV' : function(){
var r = new cBaseFunction("TINV");
return r;
},
'TREND' : function(){
var r = new cBaseFunction("TREND");
return r;
},
'TRIMMEAN' : function(){
var r = new cBaseFunction("TRIMMEAN");
return r;
},
'TTEST' : function(){
var r = new cBaseFunction("TTEST");
return r;
},
'VAR' : function(){
var r = new cBaseFunction("VAR");
return r;
},
'VARA' : function(){
var r = new cBaseFunction("VARA");
return r;
},
'VARP' : function(){
var r = new cBaseFunction("VARP");
return r;
},
'VARPA' : function(){
var r = new cBaseFunction("VARPA");
return r;
},
'WEIBULL' : function(){
var r = new cBaseFunction("WEIBULL");
return r;
},
'ZTEST' : function(){
var r = new cBaseFunction("ZTEST");
return r;
}
}
\ No newline at end of file
/**
* Created with JetBrains WebStorm.
* User: Dmitry.Shahtanov
* Date: 27.06.13
* Time: 15:17
* To change this template use File | Settings | File Templates.
*/
cFormulaFunction.TextAndData = {
'groupName' : "TextAndData",
'ASC' : function(){
var r = new cBaseFunction("ASC");
return r;
},
'BAHTTEXT' : function(){
var r = new cBaseFunction("BAHTTEXT");
return r;
},
'CHAR' : function(){
var r = new cBaseFunction("CHAR");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
}
else if( arg0 instanceof cArray){
var ret = new cArray();
arg0.foreach(function(elem,r,c){
var _elem = elem.tocNumber();
if(!ret.array[r])
ret.addRow();
if( _elem instanceof cError )
ret.addElement(_elem);
else
ret.addElement( new cString( String.fromCharCode(_elem.getValue()) ) );
})
return this.value = ret;
}
arg0 = arg0.tocNumber();
if( arg0 instanceof cError ){
return this.value = arg0;
}
return this.value = new cString( String.fromCharCode(arg0.getValue()) );
}
r.getInfo = function(){
return {
name:this.name,
args:"( number )"
};
}
return r;
},
'CLEAN' : function(){
var r = new cBaseFunction("CLEAN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocNumber();
}
if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
arg0 = arg0.tocString();
var v = arg0.getValue(), l = v.length, res = "";
for( var i = 0; i < l; i++){
if( v.charCodeAt(i) > 0x1f )
res+=v[i];
}
return this.value = new cString( res );
}
r.getInfo = function(){
return {
name:this.name,
args:"( string )"
};
}
return r;
},
'CODE' : function(){
var r = new cBaseFunction("CODE");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocString();
}
else if( arg0 instanceof cArray){
var ret = new cArray();
arg0.foreach(function(elem,r,c){
var _elem = elem.tocString();
if(!ret.array[r])
ret.addRow();
if( _elem instanceof cError )
ret.addElement(_elem);
else
ret.addElement(new cNumber( _elem.toString().charCodeAt() ));
})
return this.value = ret;
}
arg0 = arg0.tocString();
if( arg0 instanceof cError ){
return this.value = arg0;
}
return this.value = new cNumber( arg0.toString().charCodeAt() );
}
r.getInfo = function(){
return {
name:this.name,
args:"( string )"
};
}
return r;
},
'CONCATENATE' : function(){
var r = new cBaseFunction("CONCATENATE");
r.setArgumentsMin(1);
r.setArgumentsMax(255);
r.Calculate = function(arg){
var arg0 = new cString(""), argI;
for(var i = 0; i < this.argumentsCurrent; i++){
argI = arg[i];
if( argI instanceof cArea || argI instanceof cArea3D ){
argI = argI.cross(arguments[1].first);
}
argI = argI.tocString();
if (argI instanceof cError) {
return this.value = argI;
}
else if( argI instanceof cArray ){
argI.foreach(function(elem){
if( elem instanceof cError ){
arg0 = elem;
return true;
}
arg0 = new cString( arg0.toString().concat( elem.toString() ) );
})
if( arg0 instanceof cError ){
return this.value = arg0;
}
}
else
arg0 = new cString( arg0.toString().concat( argI.toString() ) );
}
return this.value = arg0;
};
r.getInfo = function(){
return {
name:this.name,
args:"(text1, text2, ...)"
};
}
return r;
},
'DOLLAR' : function(){
var r = new cBaseFunction("DOLLAR");
r.setArgumentsMin(1);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var res = cFormulaFunction.TextAndData["FIXED"]().Calculate(arg);
if( res instanceof cError )
return this.value = res;
return this.value = new cString("$"+res.getValue());
}
r.getInfo = function(){
return {
name:this.name,
args:"( number [ , num-decimal ] )"
};
}
return r;
},
'EXACT' : function(){
var r = new cBaseFunction("EXACT");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg0.tocString();
arg1 = arg1.tocString();
if( arg0 instanceof cArray && arg1 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
arg1 = arg1.getElementRowCol(0,0);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
var arg0val = arg0.getValue(), arg1val = arg1.getValue();
return this.value = new cBool( arg0val === arg1val );
}
r.getInfo = function(){
return {
name:this.name,
args:"(text1, text2)"
};
}
return r;
},
'FIND' : function(){
var r = new cBaseFunction("FIND");
r.setArgumentsMin(2);
r.setArgumentsMax(3);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = this.getArguments() == 3 ? arg[2] : null, res, str, searchStr, pos = -1;
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg0.tocString();
arg1 = arg1.tocString();
if( arg2 !== null ){
if( arg2 instanceof cArea || arg2 instanceof cArea3D ){
arg2 = arg2.cross(arguments[1].first);
}
arg2 = arg2.tocNumber();
if( arg2 instanceof cArray ){
arg2 = arg1.getElementRowCol(0,0);
}
if ( arg2 instanceof cError ) return this.value = arg2;
pos = arg2.getValue();
pos = pos > 0 ? pos-1 : pos;
}
if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
str = arg1.getValue();
searchStr = arg0.getValue();
if( arg2 ){
if( pos > str.length || pos < 0 )
return this.value = new cError( cErrorType.wrong_value_type );
str = str.substring(pos);
res = str.search( searchStr );
if( res >= 0 )
res += pos;
}
else
res = str.search( searchStr );
if( res < 0 )
return this.value = new cError( cErrorType.wrong_value_type );
return this.value = new cNumber( res+1 );
}
r.getInfo = function(){
return {
name:this.name,
args:"( string-1 , string-2 [ , start-pos ] )"
};
}
return r;
},
'FINDB' : function(){
var r = cFormulaFunction.TextAndData["FIND"]()
r.setName("FINDB");
return r;
},
'FIXED' : function(){
var r = new cBaseFunction("FIXED");
r.setArgumentsMin(1);
r.setArgumentsMax(3);
r.Calculate = function(arg){
function SignZeroPositive(number){
return number < 0 ? -1 : 1;
}
function truncate(n) {
return Math[n > 0 ? "floor" : "ceil"](n);
}
function sign(n){
return n == 0 ? 0 : n < 0 ? -1 : 1
}
function Floor(number, significance){
var quotient = number / significance;
if (quotient == 0){
return 0;
}
var nolpiat = 5 * sign(quotient) * Math.pow(10, Math.floor( Math.log ( Math.abs(quotient) ) / Math.log ( 10 ) ) - cExcelSignificantDigits);
return truncate(quotient + nolpiat) * significance;
}
function roundHelper(number,num_digits){
if(num_digits > cExcelMaxExponent){
if (Math.abs(number) < 1 || num_digits < 1e10) // The values are obtained experimentally
{
return new cNumber(number);
}
return new cNumber(0);
}
else if (num_digits < cExcelMinExponent)
{
if (Math.abs(number) < 0.01) // The values are obtained experimentally
{
return new cNumber(number);
}
return new cNumber(0);
}
var significance = SignZeroPositive(number) * Math.pow(10, -truncate(num_digits));
number += significance / 2;
if ( number/significance == Infinity){
return new cNumber( number );
}
return new cNumber( Floor(number, significance) );
}
function toFix(str,skip){
var res, _int, _dec, _tmp = ""
if(skip)
return str;
res = str.split(".");
_int = res[0];
if( res.length == 2)
_dec = res[1];
_int = _int.split("").reverse().join("").match(/([^]{1,3})/ig)
for( var i = _int.length-1; i >= 0; i--){
_tmp += _int[i].split("").reverse().join("");
if( i != 0 )
_tmp += ",";
}
if( undefined != _dec)
while( _dec.length < arg1.getValue() ) _dec+="0";
return "" + _tmp + ( res.length == 2 ? "."+_dec+"" : "");
}
var arg0 = arg[0],
arg1 = arg[1] ? arg[1] : new cNumber(2),
arg2 = arg[2] ? arg[2] : new cBool(false);
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
if( arg2 instanceof cArea || arg2 instanceof cArea3D ){
arg2 = arg2.cross(arguments[1].first);
}
if( arg0 instanceof cError ) return this.value = arg0;
if( arg1 instanceof cError ) return this.value = arg1;
if( arg2 instanceof cError ) return this.value = arg2;
if( arg0 instanceof cRef || arg0 instanceof cRef3D ){
arg0 = arg0.getValue();
if(arg0 instanceof cError) return this.value = arg0;
else if(arg0 instanceof cString) return this.value = new cError(cErrorType.wrong_value_type);
else arg0 = arg0.tocNumber();
}
else arg0 = arg0.tocNumber();
if( arg1 instanceof cRef || arg1 instanceof cRef3D ){
arg1 = arg1.getValue();
if(arg1 instanceof cError) return this.value = arg1;
else if(arg1 instanceof cString) return this.value = new cError(cErrorType.wrong_value_type);
else arg1 = arg1.tocNumber();
}
else arg1 = arg1.tocNumber();
if( arg0 instanceof cArray && arg1 instanceof cArray ){
if( arg0.getCountElement() != arg1.getCountElement() || arg0.getRowCount() != arg1.getRowCount() ){
return this.value = new cError( cErrorType.not_available );
}
else{
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1.getElementRowCol(r,c);
if( a instanceof cNumber && b instanceof cNumber ){
var res = roundHelper( a.getValue(), b.getValue() );
this.array[r][c] = toFix(res.toString(),arg2.toBool());
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(elem,r,c){
var a = elem;
b = arg1;
if( a instanceof cNumber && b instanceof cNumber ){
var res = roundHelper( a.getValue(), b.getValue() );
this.array[r][c] = toFix(res.toString(),arg2.toBool());
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg0;
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(elem,r,c){
var a = arg0;
b = elem;
if( a instanceof cNumber && b instanceof cNumber ){
var res = roundHelper( a.getValue(), b.getValue() );
this.array[r][c] = toFix(res.toString(),arg2.toBool());
}
else
this.array[r][c] = new cError( cErrorType.wrong_value_type );
})
return this.value = arg1;
}
var number = arg0.getValue(), num_digits = arg1.getValue();
return this.value = new cString(toFix(roundHelper(number,num_digits).toString(), arg2.toBool()));
}
r.getInfo = function(){
return {
name:this.name,
args:"( number [ , [ num-decimal ] [ , suppress-commas-flag ] ] )"
};
}
return r;
},
'JIS' : function(){
var r = new cBaseFunction("JIS");
return r;
},
'LEFT' : function(){
var r = new cBaseFunction("LEFT");
r.setArgumentsMin(1);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = this.argumentsCurrent == 1 ? new cNumber(1) : arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg0.tocString();
arg1 = arg1.tocNumber();
if( arg0 instanceof cArray && arg1 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
arg1 = arg1.getElementRowCol(0,0);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
if( arg1.getValue() < 0 ) return this.value = new cError( cErrorType.wrong_value_type );
return this.value = new cString( arg0.getValue().substring(0, arg1.getValue()) )
}
r.getInfo = function(){
return {
name:this.name,
args:"( string [ , number-chars ] )"
};
}
return r;
},
'LEFTB' : function(){
var r = cFormulaFunction.TextAndData["LEFT"]()
r.setName("LEFTB");
return r;
},
'LEN' : function(){
var r = new cBaseFunction("LEN");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocString();
if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if ( arg0 instanceof cError ) return this.value = arg0;
return this.value = new cNumber( arg0.getValue().length )
}
r.getInfo = function(){
return {
name:this.name,
args:"( string )"
};
}
return r;
},
'LENB' : function(){
var r = cFormulaFunction.TextAndData["LEN"]();
r.setName("LENB");
return r;
},
'LOWER' : function(){
var r = new cBaseFunction("LOWER");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D )
arg0 = arg0.cross(arguments[1].first);
arg0 = arg0.tocString();
if( arg0 instanceof cArray )
arg0 = arg0.getElementRowCol(0,0);
if( arg0 instanceof cError ) return this.value = arg0;
return this.value = new cString( arg0.getValue().toLowerCase() );
}
r.getInfo = function(){
return {
name:this.name,
args:"(text)"
};
}
return r;
},
'MID' : function(){
var r = new cBaseFunction("MID");
r.setArgumentsMin(3);
r.setArgumentsMax(3);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
if( arg2 instanceof cArea || arg2 instanceof cArea3D ){
arg2 = arg2.cross(arguments[1].first);
}
arg0 = arg0.tocString();
arg1 = arg1.tocNumber();
arg2 = arg2.tocNumber();
if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
if( arg2 instanceof cArray ){
arg2 = arg2.getElementRowCol(0,0);
}
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
if ( arg2 instanceof cError ) return this.value = arg2;
if( arg1.getValue() < 0 ) return this.value = new cError( cErrorType.wrong_value_type );
if( arg2.getValue() < 0 ) return this.value = new cError( cErrorType.wrong_value_type );
var l = arg0.getValue().length;
if( arg1.getValue() > l )
return this.value = new cString("");
/* if( arg1.getValue() < l )
return this.value = arg0; */
return this.value = new cString( arg0.getValue().substr( arg1.getValue() == 0 ? 0 : arg1.getValue()-1, arg2.getValue() ) )
}
r.getInfo = function(){
return {
name:this.name,
args:"( string , start-pos , number-chars )"
};
}
return r;
},
'MIDB' : function(){
var r = cFormulaFunction.TextAndData["MID"]();
r.setName("MIDB");
return r;
},
'PHONETIC' : function(){
var r = new cBaseFunction("PHONETIC");
return r;
},
'PROPER' : function(){
var r = new cBaseFunction("PROPER");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var reg_PROPER = new RegExp("[-#$+*/^&%<\\[\\]='?_\\@!~`\">: ;.\\)\\(,]|\\d|\\s"), arg0 = arg[0];
function proper(str){
var canUpper = true, retStr = "", regTest;
for( var i=0; i< str.length; i++ ){
regTest = reg_PROPER.test(str[i]);
if( regTest ){
canUpper = true;
}
else{
if( canUpper ){
retStr += str[i].toUpperCase();
canUpper = false;
continue;
}
}
retStr += str[i].toLowerCase();
}
return retStr;
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocString();
}
else if( arg0 instanceof cArray){
var ret = new cArray();
arg0.foreach(function(elem,r,c){
var _elem = elem.tocString();
if(!ret.array[r])
ret.addRow();
if( _elem instanceof cError )
ret.addElement(_elem);
else
ret.addElement(new cString( proper(_elem.toString()) ));
})
return this.value = ret;
}
arg0 = arg0.tocString();
if( arg0 instanceof cError ){
return this.value = arg0;
}
return this.value = new cString( proper(arg0.toString() ) );
}
r.getInfo = function(){
return {
name:this.name,
args:"( string )"
};
}
return r;
},
'REPLACE' : function(){
var r = new cBaseFunction("REPLACE");
r.setArgumentsMin(4);
r.setArgumentsMax(4);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], arg3 = arg[3];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocString();
}
else if( arg0 instanceof cArray){
arg0 = arg0.getElement(0).tocString();
}
arg0 = arg0.tocString();
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first).tocNumber();
}
else if( arg1 instanceof cArray){
arg1 = arg1.getElement(0).tocNumber();
}
arg1 = arg1.tocNumber();
if( arg2 instanceof cArea || arg2 instanceof cArea3D ){
arg2 = arg2.cross(arguments[1].first).tocNumber();
}
else if( arg2 instanceof cArray){
arg2 = arg2.getElement(0).tocNumber();
}
arg2 = arg2.tocNumber();
if( arg3 instanceof cArea || arg3 instanceof cArea3D ){
arg3 = arg3.cross(arguments[1].first).tocString();
}
else if( arg3 instanceof cArray){
arg3 = arg3.getElement(0).tocString();
}
arg3 = arg3.tocString();
if( arg0 instanceof cError )
return this.value = arg0;
if( arg1 instanceof cError )
return this.value = arg1;
if( arg2 instanceof cError )
return this.value = arg2;
if( arg3 instanceof cError )
return this.value = arg3;
if( arg1.getValue() < 1 || arg2.getValue() < 0 ){
return this.value = new cError( cErrorType.wrong_value_type );
}
var string1 = arg0.getValue(), string2 = arg3.getValue(), res = "";
string1 = string1.split("");
string1.splice(arg1.getValue()-1,arg2.getValue(),string2);
for( var i = 0; i < string1.length; i++){
res += string1[i];
}
return this.value = new cString(res);
}
r.getInfo = function(){
return {
name:this.name,
args:"( string-1, start-pos, number-chars, string-2 )"
};
}
return r;
},
'REPLACEB' : function(){
var r = cFormulaFunction.TextAndData["REPLACE"]();
r.setName("REPLACEB");
return r;
},
'REPT' : function(){
var r = new cBaseFunction("REPT");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], res = "";
if( arg0 instanceof cError ) return this.value = arg0;
if( arg1 instanceof cError ) return this.value = arg1;
if( arg0 instanceof cArray && arg1 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
arg1 = arg1.getElementRowCol(0,0);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
arg0 = arg0.tocString();
if (arg0 instanceof cError)
return this.value = arg0;
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first).tocNumber();
}
else if( arg1 instanceof cRef || arg1 instanceof cRef3D ){
arg1 = arg1.getValue();
}
if(arg1 instanceof cError)
return this.value = arg1;
else if(arg1 instanceof cString)
return this.value = new cError(cErrorType.wrong_value_type);
else
arg1 = arg1.tocNumber();
if( arg1.getValue() < 0 ) return this.value = new cError(cErrorType.wrong_value_type);
for ( var i = 0; i < arg1.getValue(); i++){
res = res.concat(arg0.getValue());
}
return this.value = new cString( res );
}
r.getInfo = function(){
return {
name:this.name,
args:"(text, number_of_times)"
};
}
return r;
},
'RIGHT' : function(){
var r = new cBaseFunction("RIGHT");
r.setArgumentsMin(1);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = this.argumentsCurrent == 1 ? new cNumber(1) : arg[1];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
arg0 = arg0.tocString();
arg1 = arg1.tocNumber();
if( arg0 instanceof cArray && arg1 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
arg1 = arg1.getElementRowCol(0,0);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
if( arg1.getValue() < 0 ) return this.value = new cError( cErrorType.wrong_value_type );
var l = arg0.getValue().length, _number = l-arg1.getValue();
return this.value = new cString( arg0.getValue().substring( _number < 0 ? 0 : _number , l) )
}
r.getInfo = function(){
return {
name:this.name,
args:"( string [ , number-chars ] )"
};
}
return r;
},
'RIGHTB' : function(){
var r = cFormulaFunction.TextAndData["RIGHT"]()
r.setName("RIGHTB");
return r;
},
'SEARCH' : function(){
var r = new cBaseFunction("SEARCH");
r.setArgumentsMin(2);
r.setArgumentsMax(3);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2] ? arg[2] : new cNumber(1);
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocString();
}
else if( arg0 instanceof cArray){
arg0 = arg0.getElement(0).tocString();
}
arg0 = arg0.tocString();
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first).tocString();
}
else if( arg1 instanceof cArray){
arg1 = arg1.getElement(0).tocString();
}
arg1 = arg1.tocString();
if( arg2 instanceof cArea || arg2 instanceof cArea3D ){
arg2 = arg2.cross(arguments[1].first).tocNumber();
}
else if( arg2 instanceof cArray){
arg2 = arg2.getElement(0).tocNumber();
}
arg2 = arg2.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
if( arg1 instanceof cError )
return this.value = arg1;
if( arg2 instanceof cError )
return this.value = arg2;
if( arg2.getValue() < 1 || arg2.getValue() > arg1.getValue().length ){
return this.value = new cError( cErrorType.wrong_value_type );
}
var string1 = arg0.getValue(), string2 = arg1.getValue(), res = 0,
valueForSearching = string1
.replace(/(~)?\*/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]*';
})
.replace(/(~)?\?/g, function($0, $1){
return $1 ? $0 : '[\\w\\W]{1,1}';
})
.replace(/\~/g, "\\");
valueForSearching = new RegExp( valueForSearching, "ig")
if( string1 == "" )
return this.value = arg2;
res = string2.substring(arg2.getValue()-1).search( valueForSearching ) + arg2.getValue()-1;
if( res < 0 )
return this.value = new cError( cErrorType.wrong_value_type );
return this.value = new cNumber(res+1);
}
r.getInfo = function(){
return {
name:this.name,
args:"( string-1 , string-2 [ , start-pos ] )"
};
}
return r;
},
'SEARCHB' : function(){
var r = cFormulaFunction.TextAndData["SEARCH"]();
r.setName("SEARCHB");
return r;
},
'SUBSTITUTE' : function(){
var r = new cBaseFunction("SUBSTITUTE");
r.setArgumentsMin(3);
r.setArgumentsMax(4);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], arg3 = arg[3] ? arg[3] : new cNumber(0);
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocString();
}
else if( arg0 instanceof cArray){
arg0 = arg0.getElement(0).tocString();
}
arg0 = arg0.tocString();
if( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first).tocString();
}
else if( arg1 instanceof cArray){
arg1 = arg1.getElement(0).tocString();
}
arg1 = arg1.tocString();
if( arg2 instanceof cArea || arg2 instanceof cArea3D ){
arg2 = arg2.cross(arguments[1].first).tocString();
}
else if( arg2 instanceof cArray){
arg2 = arg2.getElement(0).tocString();
}
arg2 = arg2.tocString();
if( arg3 instanceof cArea || arg3 instanceof cArea3D ){
arg3 = arg3.cross(arguments[1].first).tocNumber();
}
else if( arg3 instanceof cArray){
arg3 = arg3.getElement(0).tocNumber();
}
arg3 = arg3.tocNumber();
if( arg0 instanceof cError )
return this.value = arg0;
if( arg1 instanceof cError )
return this.value = arg1;
if( arg2 instanceof cError )
return this.value = arg2;
if( arg3 instanceof cError )
return this.value = arg3;
if( arg3.getValue() < 0 ){
return this.value = new cError( cErrorType.wrong_value_type );
}
var string = arg0.getValue(), old_string = arg1.getValue(), new_string = arg2.getValue(), index = 0, res;
res = string.replace(new RegExp(old_string,"g"),function(equal, p1, source){
index++;
if( arg3.getValue() == 0 || arg3.getValue() > source.length )
return new_string;
else if( arg3.getValue() == index ){
return new_string;
}
return equal;
})
return this.value = new cString( res );
}
r.getInfo = function(){
return {
name:this.name,
args:"( string , old-string , new-string [ , occurence ] )"
};
}
return r;
},
'T' : function(){
var r = new cBaseFunction("T");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cRef || arg0 instanceof cRef3D){
arg0 = arg0.getValue();
}
else if( arg0 instanceof cString || arg0 instanceof cError )
return this.value = arg0;
else if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg[0] instanceof cArray ){
arg0 = arg[0].getElementRowCol(0,0);
}
if( arg0 instanceof cString || arg0 instanceof cError )
return this.value = arg[0];
else
return this.value = new cEmpty();
}
r.getInfo = function(){
return {
name:this.name,
args:"( value )"
};
}
return r;
},
'TEXT' : function(){
var r = new cBaseFunction("TEXT");
r.setArgumentsMin(2);
r.setArgumentsMax(2);
r.Calculate = function(arg){
var arg0 = arg[0], arg1 = arg[1];
if( arg0 instanceof cRef || arg0 instanceof cRef3D){
arg0 = arg0.getValue();
}
else if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
if( arg1 instanceof cRef || arg1 instanceof cRef3D){
arg1 = arg1.getValue();
}
else if ( arg1 instanceof cArea || arg1 instanceof cArea3D ){
arg1 = arg1.cross(arguments[1].first);
}
else if( arg1 instanceof cArray ){
arg1 = arg1.getElementRowCol(0,0);
}
arg1 = arg1.tocString();
if ( arg0 instanceof cError ) return this.value = arg0;
if ( arg1 instanceof cError ) return this.value = arg1;
var _tmp = arg0.tocNumber();
if( _tmp instanceof cNumber )
arg0 = _tmp;
var oFormat = oNumFormatCache.get(arg1.toString());
var aText = oFormat.format(arg0.getValue(), arg0 instanceof cNumber ? CellValueType.Number : CellValueType.String, gc_nMaxDigCountView, null);
var text = "";
for(var i = 0, length = aText.length; i < length; ++i)
{
if(aText[i].format && aText[i].format.skip ){
text += " ";
continue;
}
if(aText[i].format && aText[i].format.repeat )
continue;
text += aText[i].text;
}
return this.value = new cString(text);
}
r.getInfo = function(){
return {
name:this.name,
args:"( value , format )"
};
}
return r;
},
'TRIM' : function(){
var r = new cBaseFunction("TRIM");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first).tocString();
}
else if( arg0 instanceof cArray){
arg0 = arg0.getElement(0).tocString();
}
arg0 = arg0.tocString();
if( arg0 instanceof cError )
return this.value = arg0;
return this.value = new cString( arg0.getValue().replace(/\s/g, function($0, $1, $2){ var r; /\s/.test($2[$1+1]) ? r = "" : r = $2[$1]; return r; }).replace(/^\s|\s$/g,"") )
}
r.getInfo = function(){
return {
name:this.name,
args:"( string )"
};
}
return r;
},
'UPPER' : function(){
var r = new cBaseFunction("UPPER");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
if( arg0 instanceof cArray )
arg0 = arg0.getElementRowCol(0,0);
arg0 = arg0.tocString();
if( arg0 instanceof cError ) return this.value = arg0;
return this.value = new cString( arg0.getValue().toUpperCase() );
}
r.getInfo = function(){
return {
name:this.name,
args:"(text)"
};
}
return r;
},
'VALUE' : function(){
var r = new cBaseFunction("VALUE");
r.setArgumentsMin(1);
r.setArgumentsMax(1);
r.Calculate = function(arg){
var arg0 = arg[0];
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ){
arg0 = arg0.cross(arguments[1].first);
}
else if( arg0 instanceof cArray ){
arg0 = arg0.getElementRowCol(0,0);
}
arg0 = arg0.tocString();
if ( arg0 instanceof cError )
return this.value = arg0;
var res = g_oFormatParser.parse(arg0.getValue());
if( res )
return this.value = new cNumber( res.value );
else
return this.value = new cError( cErrorType.wrong_value_type );
}
r.getInfo = function(){
return {
name:this.name,
args:"( string )"
};
}
r.setFormat(r.formatType.noneFormat);
return r;
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment