Commit 4c3099c9 authored by Dmitry.Shahtanov's avatar Dmitry.Shahtanov Committed by Alexander.Trofimov

Добавлены функции в формулах:

ACCRINT, ACCRINTM, AMORDEGRC, AMORLINC, CUMIPMT, EDDECT, IRR, ISPMT, XIRR, XNPV, INDEX, MATCH, OFFSET, TRANSPOSE, STDEVP, STDEVPA, STDEYX, VARA, VARP, VARPA.

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@55076 954022d7-b5bf-4e40-9824-e11837661b57
parent 31d63dac
......@@ -20,16 +20,18 @@
</script>
<script type="text/javascript" src="../../Common/browser.js"></script>
<script type="text/javascript" src="../../Common/editorscommon.js"></script>
<script type="text/javascript" src="../../Common/downloaderfiles.js"></script>
<script src="../../Common/downloaderfiles.js"></script>
<script type="text/javascript" src="../../Common/3rdparty/Underscore/underscore-min.js"></script>
<script type="text/javascript" src="../../Common/3rdparty/Sockjs/sockjs-0.3.min.js"></script>
<script type="text/javascript" src="../../Common/docscoapicommon.js"></script>
<script type="text/javascript" src="../../Common/docscoapi.js"></script>
<script type="text/javascript" src="../../Common/Charts/DrawingArea.js"></script>
<script type="text/javascript" src="../../Common/Charts/DrawingObjects.js"></script>
<script type="text/javascript" src="../../Common/Charts/charts.js"></script>
<script type="text/javascript" src="../../Common/Charts/ChartsDrawer.js"></script>
<script type="text/javascript" src="../../Common/commonDefines.js"></script>
<script type="text/javascript" src="../../Common/editorscommon.js"></script>
<script type="text/javascript" src="../../Common/apiCommon.js"></script>
<script type="text/javascript" src="../../Common/FontsFreeType/font_engine.js"></script>
......@@ -52,7 +54,6 @@
<script type="text/javascript" src="../graphics/DrawingContext.js"></script>
<script type="text/javascript" src="../graphics/pdfprinter.js"></script>
<!--<script type="text/javascript" src="../../OfflineDocuments/Excel/test-workbook9/Editor.js"></script>-->
<script type="text/javascript" src="../../OfflineDocuments/Excel/test-workbook2/editor.js"></script>
<script type="text/javascript" src="../model/CollaborativeEditing.js"></script>
......@@ -81,6 +82,8 @@
<script type="text/javascript" src="../../Common/scroll.js"></script>
<script type="text/javascript" src="../view/mobileTouch.js"></script>
<script type="text/javascript" src="../view/iscroll.js"></script>
<script type="text/javascript" src="../view/StringRender.js"></script>
<script type="text/javascript" src="../view/CellTextRender.js"></script>
<script type="text/javascript" src="../view/CellEditorView.js"></script>
......@@ -103,63 +106,75 @@
<!--for chart-->
<script type="text/javascript" src="../../Common/SerializeCommonWordExcel.js"></script>
<script type="text/javascript" src="../../Common/Charts/libraries/OfficeExcel.common.core.js"></script>
<script src="../../Common/Charts/libraries/OfficeExcel.common.core.js"></script>
<script type="text/javascript" src="../../Common/Charts/libraries/OfficeExcel.common.key.js"></script>
<script src="../../Common/Charts/libraries/OfficeExcel.common.key.js"></script>
<script type="text/javascript" src="../../Common/Charts/libraries/OfficeExcel.bar.js"></script>
<script type="text/javascript" src="../../Common/Charts/libraries/OfficeExcel.hbar.js"></script>
<script type="text/javascript" src="../../Common/Charts/libraries/OfficeExcel.line.js"></script>
<script type="text/javascript" src="../../Common/Charts/libraries/OfficeExcel.pie.js"></script>
<script type="text/javascript" src="../../Common/Charts/libraries/OfficeExcel.scatter.js"></script>
<script type="text/javascript" src="../../Common/Charts/libraries/OfficeExcel.chartProperties.js"></script>
<script src="../../Common/Charts/libraries/OfficeExcel.bar.js"></script>
<script src="../../Common/Charts/libraries/OfficeExcel.hbar.js"></script>
<script src="../../Common/Charts/libraries/OfficeExcel.line.js"></script>
<script src="../../Common/Charts/libraries/OfficeExcel.pie.js"></script>
<script src="../../Common/Charts/libraries/OfficeExcel.scatter.js"></script>
<script src="../../Common/Charts/libraries/OfficeExcel.chartProperties.js"></script>
<!--for shapes-->
<script type="text/javascript" src="../../Common/Drawings/Hit.js"></script>
<script type="text/javascript" src="../../Common/Drawings/States.js"></script>
<script type="text/javascript" src="../../Common/Drawings/TrackObjects/AdjustmentTracks.js"></script>
<script type="text/javascript" src="../../Common/Drawings/TrackObjects/ResizeTracks.js"></script>
<script type="text/javascript" src="../../Common/Drawings/TrackObjects/RotateTracks.js"></script>
<script type="text/javascript" src="../../Common/Drawings/TrackObjects/NewShapeTracks.js"></script>
<script type="text/javascript" src="../../Common/Drawings/TrackObjects/PolyLine.js"></script>
<script type="text/javascript" src="../../Common/Drawings/TrackObjects/Spline.js"></script>
<script type="text/javascript" src="../../Common/Drawings/TrackObjects/MoveTracks.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/ChartFormat.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/ChartSpace.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/Constants.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/CreateGeometry.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/Format.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/Geometry.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/Path.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/GroupShape.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/Image.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/Shape.js"></script>
<script type="text/javascript" src="../../Common/Drawings/Format/TextBody.js"></script>
<script type="text/javascript" src="../../Common/Drawings/CommonController.js"></script>
<script type="text/javascript" src="../view/DrawingObjectsController.js"></script>
<script type="text/javascript" src="../../Excel/model/DrawingObjects/Graphics.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Overlay.js"></script>
<script type="text/javascript" src="../../Word/Drawing/Controls.js"></script>
<script type="text/javascript" src="../../Word/Drawing/ShapeDrawer.js"></script>
<script type="text/javascript" src="../../Word/Editor/CollaborativeEditing.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/DrawingDocument.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/GlobalLoaders.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Format/ShapePrototype.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Format/GroupPrototype.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Format/ChartSpacePrototype.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Format/Styles.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Format/Numbering.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Format/ParagraphContent.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Format/Paragraph.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Format/DocumentContent.js"></script>
<script type="text/javascript" src="../model/DrawingObjects/Format/FontClassification.js"></script>
<script src="../../Common/Drawings/Hit.js"></script>
<script src="../../Common/Drawings/States.js"></script>
<script src="../../Common/Drawings/TrackObjects/AdjustmentTracks.js"></script>
<script src="../../Common/Drawings/TrackObjects/ResizeTracks.js"></script>
<script src="../../Common/Drawings/TrackObjects/RotateTracks.js"></script>
<script src="../../Common/Drawings/TrackObjects/NewShapeTracks.js"></script>
<script src="../../Common/Drawings/TrackObjects/PolyLine.js"></script>
<script src="../../Common/Drawings/TrackObjects/Spline.js"></script>
<script src="../../Common/Drawings/TrackObjects/MoveTracks.js"></script>
<script src="../../Common/Drawings/Format/Constants.js"></script>
<script src="../../Common/Drawings/Format/Format.js"></script>
<script src="../../Common/Drawings/Format/CreateGeometry.js"></script>
<script src="../../Common/Drawings/Format/Geometry.js"></script>
<script src="../../Common/Drawings/Format/Path.js"></script>
<script src="../../Common/Drawings/Format/GroupShape.js"></script>
<script src="../../Common/Drawings/Format/Image.js"></script>
<script src="../../Common/Drawings/Format/Shape.js"></script>
<script src="../../Common/Drawings/Format/ChartSpace.js"></script>
<script src="../../Common/Drawings/Format/ChartFormat.js"></script>
<script src="../../Common/Drawings/Format/TextBody.js"></script>
<script src="../../Common/Drawings/CommonController.js"></script>
<script src="../view/DrawingObjectsController.js"></script>
<script src="../model/DrawingObjects/Graphics.js"></script>
<script src="../model/DrawingObjects/Overlay.js"></script>
<script src="../../Word/Drawing/Controls.js"></script>
<script src="../model/DrawingObjects/ShapeDrawer.js"></script>
<script src="../../Word/Editor/CollaborativeEditing.js"></script>
<script src="../model/DrawingObjects/DrawingDocument.js"></script>
<script src="../model/DrawingObjects/GlobalLoaders.js"></script>
<script src="../model/DrawingObjects/Format/ShapePrototype.js"></script>
<script src="../model/DrawingObjects/Format/ImagePrototype.js"></script>
<script src="../model/DrawingObjects/Format/GroupPrototype.js"></script>
<script src="../model/DrawingObjects/Format/ChartSpacePrototype.js"></script>
<script src="../../Word/Editor/Styles.js"></script>
<script src="../../Word/Editor/Numbering.js"></script>
<script src="../../Word/Editor/ParagraphContent.js"></script>
<script src="../../Word/Editor/Run.js"></script>
<script src="../../Word/Editor/Hyperlink.js"></script>
<script src="../../Word/Editor/Comments.js"></script>
<script src="../../Word/Editor/FlowObjects.js"></script>
<script src="../../Word/Editor/Paragraph.js"></script>
<script src="../../Word/Editor/Document.js"></script>
<script src="../../Word/Editor/DocumentContent.js"></script>
<script src="../../Word/Editor/HeaderFooter.js"></script>
<script src="../../Word/Editor/Table.js"></script>
<!-- <script src="../../Word/Editor/Spelling.js"></script> -->
<script src="../../Word/Editor/FontClassification.js"></script>
<script type="text/javascript" src="FormulaTests.js"></script>
</head>
......
......@@ -426,7 +426,7 @@
test( "Test: rename sheet #1", function () {
oParser = new parserFormula( "Лист2!A2", "A1", ws );
ok( oParser.parse() === true );
ok( oParser.parse() );
// strictEqual( oParser.parse(), true)
strictEqual( oParser.changeSheet( "Лист2", "Лист3" ).assemble(), "Лист3!A2" );
} )
......@@ -459,7 +459,7 @@
test( "Test: wrong ref", function () {
oParser = new parserFormula( "1+XXX1", "A1", ws );
ok( oParser.parse() );
notStrictEqual( oParser.calculate().getValue(), "1" );
notStrictEqual( oParser.calculate().getValue(), "#NAME?" );
} )
test( "Test: \"CODE\"", function () {
......@@ -763,36 +763,6 @@
} )
test( "Test: \"PV\"", function () {
function pv( rate, nper, pmt, fv, type ) {
if ( rate != 0 ) {
return -1 * ( fv + pmt * (1 + rate * type) * ( (Math.pow( (1 + rate), nper ) - 1) / rate ) ) / Math.pow( 1 + rate, nper )
}
else {
return -1 * ( fv + pmt * nper );
}
}
oParser = new parserFormula( "PV(0.08/12,12*20,500,,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), pv( 0.08 / 12, 12 * 20, 500, 0, 0 ) );
oParser = new parserFormula( "PV(0,12*20,500,,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), pv( 0, 12 * 20, 500, 0, 0 ) );
} )
test( "Test: \"NPV\"", function () {
oParser = new parserFormula( "NPV(0.1,-10000,3000,4200,6800)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 1188.4434123352216 );
} )
test( "Test: \"SUMIF\"", function () {
ws.getRange2( "A2" ).setValue( "100000" );
......@@ -899,113 +869,6 @@
} )
test( "Test: \"FV\"", function () {
function fv( rate, nper, pmt, pv, type ) {
var res;
if ( type === undefined || type === null )
type = 0;
if ( pv === undefined || pv === null )
pv = 0;
if ( rate != 0 ) {
res = -1 * ( pv * Math.pow( 1 + rate, nper ) + pmt * ( 1 + rate * type ) * ( Math.pow( 1 + rate, nper ) - 1) / rate );
}
else {
res = -1 * ( pv + pmt * nper );
}
return res;
}
oParser = new parserFormula( "FV(0.06/12,10,-200,-500,1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), fv( 0.06 / 12, 10, -200, -500, 1 ) );
oParser = new parserFormula( "FV(0.12/12,12,-1000)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), fv( 0.12 / 12, 12, -1000 ) );
oParser = new parserFormula( "FV(0.11/12,35,-2000,,1)", "A2", ws );
ok( oParser.parse() );
ok( Math.abs( oParser.calculate().getValue() - fv( 0.11 / 12, 35, -2000, null, 1 ) ) < dif );
oParser = new parserFormula( "FV(0.06/12,12,-100,-1000,1)", "A2", ws );
ok( oParser.parse() );
ok( Math.abs( oParser.calculate().getValue() - fv( 0.06 / 12, 12, -100, -1000, 1 ) ) < dif );
} )
test( "Test: \"PMT\"", function () {
function pmt( rate, nper, pv, fv, type ) {
var res;
if ( type === undefined || type === null )
type = 0;
if ( fv === undefined || fv === null )
fv = 0;
if ( rate != 0 ) {
res = -1 * ( pv * Math.pow( 1 + rate, nper ) + fv ) /
( ( 1 + rate * type ) * ( Math.pow( 1 + rate, nper ) - 1 ) / rate );
}
else {
res = -1 * ( pv + fv ) / nper;
}
return res;
}
oParser = new parserFormula( "PMT(0.08/12,10,10000)", "A2", ws );
ok( oParser.parse() );
ok( Math.abs( oParser.calculate().getValue() - pmt( 0.08 / 12, 10, 10000 ) ) < dif );
oParser = new parserFormula( "PMT(0.08/12,10,10000,0,1)", "A2", ws );
ok( oParser.parse() );
ok( Math.abs( oParser.calculate().getValue() - pmt( 0.08 / 12, 10, 10000, 0, 1 ) ) < dif );
} )
test( "Test: \"NPER\"", function () {
function nper(rate,pmt,pv,fv,type){
if ( rate === undefined || rate === null )
rate = 0;
if ( pmt === undefined || pmt === null )
pmt = 0;
if ( pv === undefined || pv === null )
pv = 0;
if ( type === undefined || type === null )
type = 0;
if ( fv === undefined || fv === null )
fv = 0;
var res;
if ( rate != 0 ) {
res = (-fv * rate + pmt * (1 + rate * type)) / (rate * pv + pmt * (1 + rate * type))
res = Math.log( res ) / Math.log( 1+rate )
}
else {
res = (- pv - fv )/ pmt ;
}
return res;
}
oParser = new parserFormula( "NPER(0.12/12,-100,-1000,10000,1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), nper(0.12/12,-100,-1000,10000,1) );
oParser = new parserFormula( "NPER(0.12/12,-100,-1000)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), nper(0.12/12,-100,-1000) );
} )
test( "Test: \"TIMEVALUE\"", function () {
oParser = new parserFormula( "timevalue(\"10:02:34\")", "A2", ws );
ok( oParser.parse() );
......@@ -2956,4 +2819,513 @@
} )
test( "Test: \"MATCH\"", function () {
ws.getRange2( "A551" ).setValue( "28" );
ws.getRange2( "A552" ).setValue( "29" );
ws.getRange2( "A553" ).setValue( "31" );
ws.getRange2( "A554" ).setValue( "45" );
ws.getRange2( "A555" ).setValue( "89" );
ws.getRange2( "B551" ).setValue( "89" );
ws.getRange2( "B552" ).setValue( "45" );
ws.getRange2( "B553" ).setValue( "31" );
ws.getRange2( "B554" ).setValue( "29" );
ws.getRange2( "B555" ).setValue( "28" );
ws.getRange2( "C551" ).setValue( "89" );
ws.getRange2( "C552" ).setValue( "45" );
ws.getRange2( "C553" ).setValue( "31" );
ws.getRange2( "C554" ).setValue( "29" );
ws.getRange2( "C555" ).setValue( "28" );
oParser = new parserFormula( "MATCH(30;A551:A555;-1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), "#N/A" );
oParser = new parserFormula( "MATCH(30;A551:A555;1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 2 );
oParser = new parserFormula( "MATCH(30;A551:A555;0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), "#N/A" );
oParser = new parserFormula( "MATCH(30;B551:B555)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), "#N/A" );
oParser = new parserFormula( "MATCH(30;B551:B555;-1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 3 );
oParser = new parserFormula( "MATCH(30;B551:B555;0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), "#N/A" );
oParser = new parserFormula( "MATCH(31;C551:C555;0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 3 );
oParser = new parserFormula( "MATCH(\"b\",{\"a\";\"b\";\"c\"},0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 2 );
} )
test( "Test: \"INDEX\"", function () {
ws.getRange2( "A651" ).setValue( "1" );
ws.getRange2( "A652" ).setValue( "2" );
ws.getRange2( "A653" ).setValue( "3" );
ws.getRange2( "A654" ).setValue( "4" );
ws.getRange2( "A655" ).setValue( "5" );
ws.getRange2( "B651" ).setValue( "6" );
ws.getRange2( "B652" ).setValue( "7" );
ws.getRange2( "B653" ).setValue( "8" );
ws.getRange2( "B654" ).setValue( "9" );
ws.getRange2( "B655" ).setValue( "10" );
ws.getRange2( "C651" ).setValue( "11" );
ws.getRange2( "C652" ).setValue( "12" );
ws.getRange2( "C653" ).setValue( "13" );
ws.getRange2( "C654" ).setValue( "14" );
ws.getRange2( "C655" ).setValue( "15" );
oParser = new parserFormula( "INDEX({\"Apples\",\"Lemons\";\"Bananas\",\"Pears\"},2,2)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), "Pears" );
oParser = new parserFormula( "INDEX({\"Apples\",\"Lemons\";\"Bananas\",\"Pears\"},1,2)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), "Lemons" );
oParser = new parserFormula( "INDEX(\"Apples\",2,2)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), "#REF!" );
oParser = new parserFormula( "INDEX({\"Apples\",\"Lemons\"},,2)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), "Lemons" );
oParser = new parserFormula( "INDEX(A651:C655,,2)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 6 );
oParser = new parserFormula( "INDEX(A651:C655,3,2)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 8 );
oParser = new parserFormula( "INDEX(A651:C655,10,2)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), "#REF!" );
} )
/*
* Financial
*/
test( "Test: \"FV\"", function () {
function fv( rate, nper, pmt, pv, type ) {
var res;
if ( type === undefined || type === null )
type = 0;
if ( pv === undefined || pv === null )
pv = 0;
if ( rate != 0 ) {
res = -1 * ( pv * Math.pow( 1 + rate, nper ) + pmt * ( 1 + rate * type ) * ( Math.pow( 1 + rate, nper ) - 1) / rate );
}
else {
res = -1 * ( pv + pmt * nper );
}
return res;
}
oParser = new parserFormula( "FV(0.06/12,10,-200,-500,1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), fv( 0.06 / 12, 10, -200, -500, 1 ) );
oParser = new parserFormula( "FV(0.12/12,12,-1000)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), fv( 0.12 / 12, 12, -1000 ) );
oParser = new parserFormula( "FV(0.11/12,35,-2000,,1)", "A2", ws );
ok( oParser.parse() );
ok( Math.abs( oParser.calculate().getValue() - fv( 0.11 / 12, 35, -2000, null, 1 ) ) < dif );
oParser = new parserFormula( "FV(0.06/12,12,-100,-1000,1)", "A2", ws );
ok( oParser.parse() );
ok( Math.abs( oParser.calculate().getValue() - fv( 0.06 / 12, 12, -100, -1000, 1 ) ) < dif );
} )
test( "Test: \"PMT\"", function () {
function pmt( rate, nper, pv, fv, type ) {
var res;
if ( type === undefined || type === null )
type = 0;
if ( fv === undefined || fv === null )
fv = 0;
if ( rate != 0 ) {
res = -1 * ( pv * Math.pow( 1 + rate, nper ) + fv ) /
( ( 1 + rate * type ) * ( Math.pow( 1 + rate, nper ) - 1 ) / rate );
}
else {
res = -1 * ( pv + fv ) / nper;
}
return res;
}
oParser = new parserFormula( "PMT(0.08/12,10,10000)", "A2", ws );
ok( oParser.parse() );
ok( Math.abs( oParser.calculate().getValue() - pmt( 0.08 / 12, 10, 10000 ) ) < dif );
oParser = new parserFormula( "PMT(0.08/12,10,10000,0,1)", "A2", ws );
ok( oParser.parse() );
ok( Math.abs( oParser.calculate().getValue() - pmt( 0.08 / 12, 10, 10000, 0, 1 ) ) < dif );
} )
test( "Test: \"NPER\"", function () {
function nper(rate,pmt,pv,fv,type){
if ( rate === undefined || rate === null )
rate = 0;
if ( pmt === undefined || pmt === null )
pmt = 0;
if ( pv === undefined || pv === null )
pv = 0;
if ( type === undefined || type === null )
type = 0;
if ( fv === undefined || fv === null )
fv = 0;
var res;
if ( rate != 0 ) {
res = (-fv * rate + pmt * (1 + rate * type)) / (rate * pv + pmt * (1 + rate * type))
res = Math.log( res ) / Math.log( 1+rate )
}
else {
res = (- pv - fv )/ pmt ;
}
return res;
}
oParser = new parserFormula( "NPER(0.12/12,-100,-1000,10000,1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), nper(0.12/12,-100,-1000,10000,1) );
oParser = new parserFormula( "NPER(0.12/12,-100,-1000)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), nper(0.12/12,-100,-1000) );
} )
test( "Test: \"PV\"", function () {
function pv( rate, nper, pmt, fv, type ) {
if ( rate != 0 ) {
return -1 * ( fv + pmt * (1 + rate * type) * ( (Math.pow( (1 + rate), nper ) - 1) / rate ) ) / Math.pow( 1 + rate, nper )
}
else {
return -1 * ( fv + pmt * nper );
}
}
oParser = new parserFormula( "PV(0.08/12,12*20,500,,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), pv( 0.08 / 12, 12 * 20, 500, 0, 0 ) );
oParser = new parserFormula( "PV(0,12*20,500,,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), pv( 0, 12 * 20, 500, 0, 0 ) );
} )
test( "Test: \"NPV\"", function () {
oParser = new parserFormula( "NPV(0.1,-10000,3000,4200,6800)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 1188.4434123352216 );
} )
test( "Test: \"EFFECT\"", function () {
function effect(nr,np){
if( nr <= 0 || np < 1 ) return "#NUM!"
return Math.pow( ( 1 + nr/np ), np ) - 1;
}
oParser = new parserFormula( "EFFECT(0.0525,4)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), effect(0.0525,4) );
oParser = new parserFormula( "EFFECT(0.0525,-4)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), effect(0.0525,-4) );
oParser = new parserFormula( "EFFECT(0.0525,1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), effect(0.0525,1) );
oParser = new parserFormula( "EFFECT(-1,54)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), effect(-1,54) );
} )
test( "Test: \"ISPMT\"", function () {
function ISPMT( rate, per, nper, pv ){
return pv * rate * (per / nper - 1.0)
}
oParser = new parserFormula( "ISPMT(0.1/12,1,3*12,8000000)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), ISPMT(0.1/12,1,3*12,8000000) );
oParser = new parserFormula( "ISPMT(0.1,1,3,8000000)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), ISPMT(0.1,1,3,8000000) );
} )
test( "Test: \"XNPV\"", function () {
function xnpv( rate, valueArray, dateArray ){
var res = 0, r = rate;
d1 = dateArray[0];
for( var i = 0; i < dateArray.length; i++ ){
res += valueArray[i] / ( Math.pow( ( 1 + r ), ( dateArray[i] - d1 ) / 365 ) )
}
return res;
}
ws.getRange2( "A701" ).setValue( "39448" );
ws.getRange2( "A702" ).setValue( "39508" );
ws.getRange2( "A703" ).setValue( "39751" );
ws.getRange2( "A704" ).setValue( "39859" );
ws.getRange2( "A705" ).setValue( "39904" );
oParser = new parserFormula( "XNPV(0.09,{-10000,2750,4250,3250,2750},A701:A705)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), xnpv( 0.09, [-10000,2750,4250,3250,2750], [39448,39508,39751,39859,39904] ) );
ws.getRange2( "A705" ).setValue( "43191" );
oParser = new parserFormula( "XNPV(0.09,{-10000,2750,4250,3250,2750},A701:A705)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), xnpv( 0.09, [-10000,2750,4250,3250,2750], [39448,39508,39751,39859,43191] ) );
} )
test( "Test: \"IRR\"", function () {
function irr( costArr, x ){
if (!x) x = 0.1
var nC = 0, g_Eps = 1e-7, fEps = 1.0, fZ = 0, fN = 0, xN = 0, nIM = 100, nMC = 0,arr0 = costArr[0], arrI, wasNegative = false, wasPositive = false;
if( arr0 < 0 )
wasNegative = true;
else if( arr0 > 0 )
wasPositive = true;
while(fEps > g_Eps && nMC < nIM ){
nC = 0; fZ = 0; fN = 0;
fZ += costArr[0]/Math.pow( 1.0 + x, nC );
fN += -nC * costArr[0]/Math.pow( 1 + x, nC + 1 );
nC++;
for(var i = 1; i < costArr.length; i++){
arrI = costArr[i];
fZ += arrI/Math.pow( 1.0 + x, nC );
fN += -nC * arrI/Math.pow( 1 + x, nC + 1 );
if( arrI < 0 )
wasNegative = true;
else if( arrI > 0 )
wasPositive = true
nC++
}
xN = x - fZ / fN;
nMC ++;
fEps = Math.abs( xN - x );
x = xN;
}
if( !(wasNegative && wasPositive) )
return "#NUM!";
if (fEps < g_Eps)
return x;
else
return "#NUM!";
}
oParser = new parserFormula( "IRR({-70000,12000,15000,18000,21000})", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), irr( [-70000,12000,15000,18000,21000] ) );
ws.getRange2( "A705" ).setValue( "43191" );
oParser = new parserFormula( "IRR({-70000,12000,15000,18000,21000,26000})", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), irr([-70000,12000,15000,18000,21000,26000] ) );
oParser = new parserFormula( "IRR({-70000,12000,15000},-0.1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), irr([-70000,12000,15000],-0.1 ) );
oParser = new parserFormula( "IRR({-70000},-0.1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), irr([-70000],-0.1 ) );
} )
test( "Test: \"ACCRINT\"", function () {
oParser = new parserFormula( "ACCRINT(DATE(2006,3,1),DATE(2006,9,1),DATE(2006,5,1),0.1,1100,2,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 18.333333333333332 );
oParser = new parserFormula( "ACCRINT(DATE(2006,3,1),DATE(2006,9,1),DATE(2006,5,1),0.1,,2,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 16.666666666666668 );
} )
test( "Test: \"ACCRINTM\"", function () {
oParser = new parserFormula( "ACCRINTM(DATE(2006,3,1),DATE(2006,5,1),0.1,1100,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 18.333333333333332 );
oParser = new parserFormula( "ACCRINTM(DATE(2006,3,1),DATE(2006,5,1),0.1,,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 16.666666666666668 )
oParser = new parserFormula( "ACCRINTM(DATE(2006,3,1),DATE(2006,5,1),0.1,)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 16.666666666666668 );
} )
test( "Test: \"AMORDEGRC\"", function () {
oParser = new parserFormula( "AMORDEGRC(2400,DATE(2008,8,19),DATE(2008,12,31),300,1,0.15,1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 776 );
} )
test( "Test: \"AMORLINC\"", function () {
oParser = new parserFormula( "AMORLINC(2400,DATE(2008,8,19),DATE(2008,12,31),300,1,0.15,1)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), 360 );
} )
test( "Test: \"CUMIPMT\"", function () {
function cumipmt(fRate, nNumPeriods, fVal, nStartPer, nEndPer, nPayType){
var fRmz, fZinsZ;
if( nStartPer < 1 || nEndPer < nStartPer || fRate <= 0.0 || nEndPer > nNumPeriods || nNumPeriods <= 0 ||
fVal <= 0.0 || ( nPayType != 0 && nPayType != 1 ) )
return "#NUM!"
function GetRmz( fZins, fZzr, fBw, fZw, nF ){
var fRmz;
if( fZins == 0.0 )
fRmz = ( fBw + fZw ) / fZzr;
else{
var fTerm = Math.pow( 1.0 + fZins, fZzr );
if( nF > 0 )
fRmz = ( fZw * fZins / ( fTerm - 1.0 ) + fBw * fZins / ( 1.0 - 1.0 / fTerm ) ) / ( 1.0 + fZins );
else
fRmz = fZw * fZins / ( fTerm - 1.0 ) + fBw * fZins / ( 1.0 - 1.0 / fTerm );
}
return -fRmz;
}
function GetZw( fZins, fZzr, fRmz, fBw, nF ){
var fZw;
if( fZins == 0.0 )
fZw = fBw + fRmz * fZzr;
else{
var fTerm = Math.pow( 1.0 + fZins, fZzr );
if( nF > 0 )
fZw = fBw * fTerm + fRmz * ( 1.0 + fZins ) * ( fTerm - 1.0 ) / fZins;
else
fZw = fBw * fTerm + fRmz * ( fTerm - 1.0 ) / fZins;
}
return -fZw;
}
fRmz = GetRmz( fRate, nNumPeriods, fVal, 0.0, nPayType );
fZinsZ = 0.0;
if( nStartPer == 1 )
{
if( nPayType <= 0 )
fZinsZ = -fVal;
nStartPer++;
}
for( var i = nStartPer ; i <= nEndPer ; i++ )
{
if( nPayType > 0 )
fZinsZ += GetZw( fRate, i - 2, fRmz, fVal, 1 ) - fRmz;
else
fZinsZ += GetZw( fRate, i - 1, fRmz, fVal, 0 );
}
fZinsZ *= fRate;
return fZinsZ;
}
oParser = new parserFormula( "CUMIPMT(0.09/12,30*12,125000,1,1,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), cumipmt(0.09/12,30*12,125000,1,1,0) );
oParser = new parserFormula( "CUMIPMT(0.09/12,30*12,125000,13,24,0)", "A2", ws );
ok( oParser.parse() );
strictEqual( oParser.calculate().getValue(), cumipmt(0.09/12,30*12,125000,13,24,0) );
} )
} );
......@@ -7,28 +7,44 @@
* Time: 13:24
* To change this template use File | Settings | File Templates.
*/
function GetDiffDate360( nDay1, nMonth1, nYear1, bLeapYear1, nDay2, nMonth2, nYear2, bUSAMethod ) {
if ( nDay1 == 31 )
nDay1--;
else if ( bUSAMethod && ( nMonth1 == 2 && ( nDay1 == 29 || ( nDay1 == 28 && !bLeapYear1 ) ) ) )
nDay1 = 30;
if ( nDay2 == 31 ) {
if ( bUSAMethod && nDay1 != 30 ) {
nDay2 = 1;
if ( nMonth2 == 12 ) {
nYear2++;
nMonth2 = 1;
}
else
nMonth2++;
function yearFrac(d1, d2, mode) {
var date1 = d1.getDate(),
month1 = d1.getMonth(),
year1 = d1.getFullYear(),
date2 = d2.getDate(),
month2 = d2.getMonth(),
year2 = d2.getFullYear();
switch ( mode ) {
case 0:
return new cNumber( Math.abs( GetDiffDate360( date1, month1, year1, d2.isLeapYear(), date2, month2, year2, false ) ) / 360 );
case 1:
var yc = Math.abs( year2 - year1 ),
sd = year1 > year2 ? d2 : d1,
yearAverage = sd.isLeapYear() ? 366 : 365, dayDiff = Math.abs( d2 - d1 );
for ( var i = 0; i < yc; i++ ) {
sd.addYears( 1 );
yearAverage += sd.isLeapYear() ? 366 : 365;
}
else
nDay2 = 30;
yearAverage /= (yc + 1);
dayDiff /= (yearAverage * c_msPerDay);
return new cNumber( dayDiff );
case 2:
var dayDiff = Math.abs( d2 - d1 );
dayDiff /= (360 * c_msPerDay);
return new cNumber( dayDiff );
case 3:
var dayDiff = Math.abs( d2 - d1 );
dayDiff /= (365 * c_msPerDay);
return new cNumber( dayDiff );
case 4:
return new cNumber( Math.abs( GetDiffDate360( date1, month1, year1, d2.isLeapYear(), date2, month2, year2, true ) ) / 360 );
default:
return new cError( cErrorType.not_numeric );
}
return nDay2 + nMonth2 * 30 + nYear2 * 360 - nDay1 - nMonth1 * 30 - nYear1 * 360;
}
cFormulaFunction.DateAndTime = {
'groupName':"DateAndTime",
'DATE':cDATE,
......@@ -1681,39 +1697,8 @@ cYEARFRAC.prototype.Calculate = function ( arg ) {
val0 = Date.prototype.getDateFromExcel( val0 );
val1 = Date.prototype.getDateFromExcel( val1 );
var date1 = val0.getDate(), date2 = val1.getDate(), month1 = val0.getMonth(), month2 = val1.getMonth(), year1 = val0.getFullYear(), year2 = val1.getFullYear();
switch ( arg2.getValue() ) {
case 0:
return this.value = new cNumber( Math.abs( GetDiffDate360( date1, month1, year1, val1.isLeapYear(), date2, month2, year2, false ) ) / 360 );
break;
case 1:
var yc = Math.abs( year2 - year1 ),
sd = year1 > year2 ? val1 : val0,
yearAverage = sd.isLeapYear() ? 366 : 365, dayDiff = Math.abs( val1 - val0 );
for ( var i = 0; i < yc; i++ ) {
sd.addYears( 1 );
yearAverage += sd.isLeapYear() ? 366 : 365;
}
yearAverage /= (yc + 1);
dayDiff /= (yearAverage * c_msPerDay);
return this.value = new cNumber( dayDiff );
break;
case 2:
var dayDiff = Math.abs( val1 - val0 );
dayDiff /= (360 * c_msPerDay);
return this.value = new cNumber( dayDiff );
break;
case 3:
var dayDiff = Math.abs( val1 - val0 );
dayDiff /= (365 * c_msPerDay);
return this.value = new cNumber( dayDiff );
break;
case 4:
return this.value = new cNumber( Math.abs( GetDiffDate360( date1, month1, year1, val1.isLeapYear(), date2, month2, year2, true ) ) / 360 );
break;
default:
return this.value = new cError( cErrorType.not_numeric )
}
return this.value = yearFrac( val0, val1, arg2.getValue() );
}
cYEARFRAC.prototype.getInfo = function () {
......
......@@ -65,24 +65,518 @@ cFormulaFunction.Financial = {
}
function cACCRINT() {
cBaseFunction.call( this, "ACCRINT" );
// cBaseFunction.call( this, "ACCRINT" );
this.name = "ACCRINT";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 6;
this.argumentsCurrent = 0;
this.argumentsMax = 7;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cACCRINT.prototype = Object.create( cBaseFunction.prototype )
cACCRINT.prototype.Calculate = function ( arg ) {
var arg0 = arg[0], arg1 = arg[1],
arg2 = arg[2], arg3 = arg[3],
arg4 = arg[4] && !(arg[4] instanceof cEmpty) ? arg[4] : new cNumber(1000 ),
arg5 = arg[5],
arg6 = arg[6] && !(arg[6] instanceof cEmpty) ? arg[6] : new cNumber( 0 );
function GetYearDiff( nNullDate, nStartDate, nEndDate, nMode ){
var nDays1stYear ;
switch(nMode){
case 0:
case 4:
nDays1stYear = 360;
break;
case 1:
nDays1stYear = nStartDate.isLeapYear() ? 366 : 365;
break;
case 2:
nDays1stYear = 360;
break;
case 3:
nDays1stYear = 365;
break;
}
var nTotalDays = yearFrac( nStartDate, nEndDate, nMode );
return nTotalDays//nDays1stYear;
}
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 );
}
if ( arg2 instanceof cArea || arg2 instanceof cArea3D ) {
arg2 = arg2.cross( arguments[1].first );
}
else if ( arg2 instanceof cArray ) {
arg2 = arg2.getElementRowCol( 0, 0 );
}
if ( arg3 instanceof cArea || arg3 instanceof cArea3D ) {
arg3 = arg3.cross( arguments[1].first );
}
else if ( arg3 instanceof cArray ) {
arg3 = arg3.getElementRowCol( 0, 0 );
}
if ( arg4 instanceof cArea || arg4 instanceof cArea3D ) {
arg4 = arg4.cross( arguments[1].first );
}
else if ( arg4 instanceof cArray ) {
arg4 = arg4.getElementRowCol( 0, 0 );
}
if ( arg5 instanceof cArea || arg5 instanceof cArea3D ) {
arg5 = arg5.cross( arguments[1].first );
}
else if ( arg5 instanceof cArray ) {
arg5 = arg5.getElementRowCol( 0, 0 );
}
if ( arg6 instanceof cArea || arg6 instanceof cArea3D ) {
arg6 = arg6.cross( arguments[1].first );
}
else if ( arg6 instanceof cArray ) {
arg6 = arg6.getElementRowCol( 0, 0 );
}
arg0 = arg0.tocNumber();
arg1 = arg1.tocNumber();
arg2 = arg2.tocNumber();
arg3 = arg3.tocNumber();
arg4 = arg4.tocNumber();
arg5 = arg5.tocNumber();
arg6 = arg6.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 ( arg4 instanceof cError ) return this.value = arg4;
if ( arg5 instanceof cError ) return this.value = arg5;
if ( arg6 instanceof cError ) return this.value = arg6;
var _arg5 = arg5.getValue()
if( arg0.getValue() >= arg2.getValue() || arg3.getValue() <= 0 || arg4.getValue() <= 0 || arg6.getValue() < 0 || arg6.getValue() > 4 || (_arg5 != 1 && _arg5 != 2 && _arg5 != 4) ){
return this.value = new cError( cErrorType.not_numeric );
}
var res = GetYearDiff( Date.prototype.getDateFromExcel(arg0.getValue()), Date.prototype.getDateFromExcel(arg0.getValue()),
Date.prototype.getDateFromExcel(arg2.getValue()), arg6.getValue() )
res = res * arg4.getValue() * arg3.getValue();
return this.value = new cNumber(res)
}
cACCRINT.prototype.getInfo = function () {
return {
name:this.name,
args:"( issue , first-interest , settlement , rate , [ par ] , frequency [ , [ basis ] ] )"
};
}
function cACCRINTM() {
cBaseFunction.call( this, "ACCRINTM" );
// cBaseFunction.call( this, "ACCRINTM" );
this.name = "ACCRINTM";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 3;
this.argumentsCurrent = 0;
this.argumentsMax = 5;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cACCRINTM.prototype = Object.create( cBaseFunction.prototype )
cACCRINTM.prototype.Calculate = function ( arg ) {
var arg0 = arg[0],
arg1 = arg[1],
arg2 = arg[2],
arg3 = arg[3] && !(arg[3] instanceof cEmpty) ? arg[3] : new cNumber(1000),
arg4 = arg[4] && !(arg[4] instanceof cEmpty) ? arg[4] : new cNumber(0);
function GetYearDiff( nStartDate, nEndDate, nMode ){
var nDays1stYear ;
switch(nMode){
case 0:
case 4:
nDays1stYear = 360;
break;
case 1:
nDays1stYear = nStartDate.isLeapYear() ? 366 : 365;
break;
case 2:
nDays1stYear = 360;
break;
case 3:
nDays1stYear = 365;
break;
}
var nTotalDays = yearFrac( nStartDate, nEndDate, nMode );
return nTotalDays
}
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 );
}
if ( arg2 instanceof cArea || arg2 instanceof cArea3D ) {
arg2 = arg2.cross( arguments[1].first );
}
else if ( arg2 instanceof cArray ) {
arg2 = arg2.getElementRowCol( 0, 0 );
}
if ( arg3 instanceof cArea || arg3 instanceof cArea3D ) {
arg3 = arg3.cross( arguments[1].first );
}
else if ( arg3 instanceof cArray ) {
arg3 = arg3.getElementRowCol( 0, 0 );
}
if ( arg4 instanceof cArea || arg4 instanceof cArea3D ) {
arg4 = arg4.cross( arguments[1].first );
}
else if ( arg4 instanceof cArray ) {
arg4 = arg4.getElementRowCol( 0, 0 );
}
arg0 = arg0.tocNumber();
arg1 = arg1.tocNumber();
arg2 = arg2.tocNumber();
arg3 = arg3.tocNumber();
arg4 = arg4.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 ( arg4 instanceof cError ) return this.value = arg4;
if( arg0.getValue() >= arg1.getValue() || arg3.getValue() <= 0 || arg4.getValue() < 0 || arg4.getValue() > 4 ){
return this.value = new cError( cErrorType.not_numeric );
}
var res = GetYearDiff( Date.prototype.getDateFromExcel(arg0.getValue()),
Date.prototype.getDateFromExcel(arg1.getValue()), arg4.getValue() )
res = res * arg2.getValue() * arg3.getValue();
return this.value = new cNumber(res)
}
cACCRINTM.prototype.getInfo = function () {
return {
name:this.name,
args:"( issue , settlement , rate , [ [ par ] [ , [ basis ] ] ] )"
};
}
function cAMORDEGRC() {
cBaseFunction.call( this, "AMORDEGRC" );
// cBaseFunction.call( this, "AMORDEGRC" );
this.name = "AMORDEGRC";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 6;
this.argumentsCurrent = 0;
this.argumentsMax = 7;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cAMORDEGRC.prototype = Object.create( cBaseFunction.prototype )
cAMORDEGRC.prototype.Calculate = function ( arg ) {
var cost = arg[0],
detePurch = arg[1],
firstPer = arg[2],
salvage = arg[3],
period = arg[4],
rate = arg[5],
basis = arg[6] && !(arg[6] instanceof cEmpty) ? arg[6] : new cNumber(0);
if ( cost instanceof cArea || cost instanceof cArea3D ) {
cost = cost.cross( arguments[1].first );
}
else if ( cost instanceof cArray ) {
cost = cost.getElementRowCol( 0, 0 );
}
if ( detePurch instanceof cArea || detePurch instanceof cArea3D ) {
detePurch = detePurch.cross( arguments[1].first );
}
else if ( detePurch instanceof cArray ) {
detePurch = detePurch.getElementRowCol( 0, 0 );
}
if ( firstPer instanceof cArea || firstPer instanceof cArea3D ) {
firstPer = firstPer.cross( arguments[1].first );
}
else if ( firstPer instanceof cArray ) {
firstPer = firstPer.getElementRowCol( 0, 0 );
}
if ( salvage instanceof cArea || salvage instanceof cArea3D ) {
salvage = salvage.cross( arguments[1].first );
}
else if ( salvage instanceof cArray ) {
salvage = salvage.getElementRowCol( 0, 0 );
}
if ( period instanceof cArea || period instanceof cArea3D ) {
period = period.cross( arguments[1].first );
}
else if ( period instanceof cArray ) {
period = period.getElementRowCol( 0, 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 ( basis instanceof cArea || basis instanceof cArea3D ) {
basis = basis.cross( arguments[1].first );
}
else if ( basis instanceof cArray ) {
basis = basis.getElementRowCol( 0, 0 );
}
cost = cost.tocNumber();
detePurch = detePurch.tocNumber();
firstPer = firstPer.tocNumber();
salvage = salvage.tocNumber();
period = period.tocNumber();
rate = rate.tocNumber();
basis = basis.tocNumber();
if ( cost instanceof cError ) return this.value = cost;
if ( detePurch instanceof cError ) return this.value = detePurch;
if ( firstPer instanceof cError ) return this.value = firstPer;
if ( salvage instanceof cError ) return this.value = salvage;
if ( period instanceof cError ) return this.value = period;
if ( rate instanceof cError ) return this.value = rate;
if ( basis instanceof cError ) return this.value = basis;
var fRate = rate.getValue(),
fCost = cost.getValue(),
fRestVal = salvage.getValue(),
nPer = period.getValue()
var fUsePer = 1.0 / fRate,
fAmorCoeff;
if( fUsePer < 3.0 )
fAmorCoeff = 1.0;
else if( fUsePer < 5.0 )
fAmorCoeff = 1.5;
else if( fUsePer <= 6.0 )
fAmorCoeff = 2.0;
else
fAmorCoeff = 2.5;
fRate *= fAmorCoeff;
var val0 = Date.prototype.getDateFromExcel( detePurch.getValue() );
var val1 = Date.prototype.getDateFromExcel( firstPer.getValue() );
var fNRate = Math.round( yearFrac( val0, val1, basis.getValue() ) * fRate * fCost );
fCost -= fNRate;
var fRest = fCost - fRestVal; // Anschaffungskosten - Restwert - Summe aller Abschreibungen
for ( var n = 0; n < nPer; n++ ) {
fNRate = Math.round( fRate * fCost );
fRest -= fNRate;
if ( fRest < 0.0 ) {
switch ( nPer - n ) {
case 0:
case 1:
return this.value = new cNumber( Math.round( fCost * 0.5 ) );
default:
return this.value = new cNumber( 0 );
}
}
fCost -= fNRate;
}
return this.value = new cNumber( fNRate )
}
cAMORDEGRC.prototype.getInfo = function () {
return {
name:this.name,
args:"( cost , date-purchased , first-period , salvage , period , rate [ , [ basis ] ] )"
};
}
function cAMORLINC() {
cBaseFunction.call( this, "AMORLINC" );
// cBaseFunction.call( this, "AMORLINC" );
this.name = "AMORLINC";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 6;
this.argumentsCurrent = 0;
this.argumentsMax = 7;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cAMORLINC.prototype = Object.create( cBaseFunction.prototype )
cAMORLINC.prototype.Calculate = function ( arg ) {
var cost = arg[0],
detePurch = arg[1],
firstPer = arg[2],
salvage = arg[3],
period = arg[4],
rate = arg[5],
basis = arg[6] && !(arg[6] instanceof cEmpty) ? arg[6] : new cNumber(0);
if ( cost instanceof cArea || cost instanceof cArea3D ) {
cost = cost.cross( arguments[1].first );
}
else if ( cost instanceof cArray ) {
cost = cost.getElementRowCol( 0, 0 );
}
if ( detePurch instanceof cArea || detePurch instanceof cArea3D ) {
detePurch = detePurch.cross( arguments[1].first );
}
else if ( detePurch instanceof cArray ) {
detePurch = detePurch.getElementRowCol( 0, 0 );
}
if ( firstPer instanceof cArea || firstPer instanceof cArea3D ) {
firstPer = firstPer.cross( arguments[1].first );
}
else if ( firstPer instanceof cArray ) {
firstPer = firstPer.getElementRowCol( 0, 0 );
}
if ( salvage instanceof cArea || salvage instanceof cArea3D ) {
salvage = salvage.cross( arguments[1].first );
}
else if ( salvage instanceof cArray ) {
salvage = salvage.getElementRowCol( 0, 0 );
}
if ( period instanceof cArea || period instanceof cArea3D ) {
period = period.cross( arguments[1].first );
}
else if ( period instanceof cArray ) {
period = period.getElementRowCol( 0, 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 ( basis instanceof cArea || basis instanceof cArea3D ) {
basis = basis.cross( arguments[1].first );
}
else if ( basis instanceof cArray ) {
basis = basis.getElementRowCol( 0, 0 );
}
cost = cost.tocNumber();
detePurch = detePurch.tocNumber();
firstPer = firstPer.tocNumber();
salvage = salvage.tocNumber();
period = period.tocNumber();
rate = rate.tocNumber();
basis = basis.tocNumber();
if ( cost instanceof cError ) return this.value = cost;
if ( detePurch instanceof cError ) return this.value = detePurch;
if ( firstPer instanceof cError ) return this.value = firstPer;
if ( salvage instanceof cError ) return this.value = salvage;
if ( period instanceof cError ) return this.value = period;
if ( rate instanceof cError ) return this.value = rate;
if ( basis instanceof cError ) return this.value = basis;
var fRate = rate.getValue(),
fCost = cost.getValue(),
fRestVal = salvage.getValue(),
nPer = period.getValue()
var val0 = Date.prototype.getDateFromExcel( detePurch.getValue() );
var val1 = Date.prototype.getDateFromExcel( firstPer.getValue() );
var fOneRate = fCost * fRate,
fCostDelta = fCost - fRestVal;
var f0Rate = yearFrac( val0, val1, basis.getValue() ) * fRate * fCost;
var nNumOfFullPeriods = ( fCost - fRestVal - f0Rate) / fOneRate;
if( nPer == 0 )
return this.value = new cNumber( f0Rate );
else if( nPer <= nNumOfFullPeriods )
return this.value = new cNumber( fOneRate );
else if( nPer == nNumOfFullPeriods + 1 )
return this.value = new cNumber( fCostDelta - fOneRate * nNumOfFullPeriods - f0Rate );
else
return this.value = new cNumber( 0.0 );
}
cAMORLINC.prototype.getInfo = function () {
return {
name:this.name,
args:"( cost , date-purchased , first-period , salvage , period , rate [ , [ basis ] ] )"
};
}
function cCOUPDAYBS() {
cBaseFunction.call( this, "COUPDAYBS" );
......@@ -115,9 +609,159 @@ function cCOUPPCD() {
cCOUPPCD.prototype = Object.create( cBaseFunction.prototype )
function cCUMIPMT() {
cBaseFunction.call( this, "CUMIPMT" );
// cBaseFunction.call( this, "CUMIPMT" );
this.name = "CUMIPMT";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 6;
this.argumentsCurrent = 0;
this.argumentsMax = 6;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cCUMIPMT.prototype = Object.create( cBaseFunction.prototype )
cCUMIPMT.prototype.Calculate = function ( arg ) {
var rate = arg[0],
nper = arg[1],
pv = arg[2],
startPeriod = arg[3],
endPeriod = arg[4],
type = arg[5];
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 ( startPeriod instanceof cArea || startPeriod instanceof cArea3D ) {
startPeriod = startPeriod.cross( arguments[1].first );
}
else if ( startPeriod instanceof cArray ) {
startPeriod = startPeriod.getElementRowCol( 0, 0 );
}
if ( endPeriod instanceof cArea || endPeriod instanceof cArea3D ) {
endPeriod = endPeriod.cross( arguments[1].first );
}
else if ( endPeriod instanceof cArray ) {
endPeriod = endPeriod.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();
startPeriod = startPeriod.tocNumber();
endPeriod = endPeriod.tocNumber();
type = type.tocNumber();
if ( rate instanceof cError ) return this.value = rate;
if ( nper instanceof cError ) return this.value = nper;
if ( pv instanceof cError ) return this.value = pv;
if ( startPeriod instanceof cError ) return this.value = startPeriod;
if ( endPeriod instanceof cError ) return this.value = endPeriod;
if ( type instanceof cError ) return this.value = type;
var fRate = rate.getValue(),
nNumPeriods = nper.getValue(),
fVal = pv.getValue(),
nStartPer = startPeriod.getValue(),
nEndPer = endPeriod.getValue(),
nPayType = type.getValue(),
fRmz, fZinsZ;
if( nStartPer < 1 || nEndPer < nStartPer || fRate <= 0.0 || nEndPer > nNumPeriods || nNumPeriods <= 0 ||
fVal <= 0.0 || ( nPayType != 0 && nPayType != 1 ) )
return this.value = new cError( cErrorType.not_numeric );
function GetRmz( fZins, fZzr, fBw, fZw, nF ){
var fRmz;
if( fZins == 0.0 )
fRmz = ( fBw + fZw ) / fZzr;
else{
var fTerm = Math.pow( 1.0 + fZins, fZzr );
if( nF > 0 )
fRmz = ( fZw * fZins / ( fTerm - 1.0 ) + fBw * fZins / ( 1.0 - 1.0 / fTerm ) ) / ( 1.0 + fZins );
else
fRmz = fZw * fZins / ( fTerm - 1.0 ) + fBw * fZins / ( 1.0 - 1.0 / fTerm );
}
return -fRmz;
}
function GetZw( fZins, fZzr, fRmz, fBw, nF ){
var fZw;
if( fZins == 0.0 )
fZw = fBw + fRmz * fZzr;
else{
var fTerm = Math.pow( 1.0 + fZins, fZzr );
if( nF > 0 )
fZw = fBw * fTerm + fRmz * ( 1.0 + fZins ) * ( fTerm - 1.0 ) / fZins;
else
fZw = fBw * fTerm + fRmz * ( fTerm - 1.0 ) / fZins;
}
return -fZw;
}
fRmz = GetRmz( fRate, nNumPeriods, fVal, 0.0, nPayType );
fZinsZ = 0.0;
if( nStartPer == 1 )
{
if( nPayType <= 0 )
fZinsZ = -fVal;
nStartPer++;
}
for( var i = nStartPer ; i <= nEndPer ; i++ )
{
if( nPayType > 0 )
fZinsZ += GetZw( fRate, i - 2, fRmz, fVal, 1 ) - fRmz;
else
fZinsZ += GetZw( fRate, i - 1, fRmz, fVal, 0 );
}
fZinsZ *= fRate;
return this.value = new cNumber( fZinsZ );
}
cCUMIPMT.prototype.getInfo = function () {
return {
name:this.name,
args:"( rate , nper , pv , start-period , end-period , type )"
};
}
function cCUMPRINC() {
cBaseFunction.call( this, "CUMPRINC" );
......@@ -155,9 +799,58 @@ function cDURATION() {
cDURATION.prototype = Object.create( cBaseFunction.prototype )
function cEFFECT() {
cBaseFunction.call( this, "EFFECT" );
// cBaseFunction.call( this, "EFFECT" );
this.name = "EFFECT";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 2;
this.argumentsCurrent = 0;
this.argumentsMax = 2;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cEFFECT.prototype = Object.create( cBaseFunction.prototype )
cEFFECT.prototype.Calculate = function ( arg ) {
var nominalRate = arg[0], npery = arg[1];
if ( nominalRate instanceof cArea || nominalRate instanceof cArea3D ) {
nominalRate = nominalRate.cross( arguments[1].first );
}
else if ( nominalRate instanceof cArray ) {
nominalRate = nominalRate.getElementRowCol( 0, 0 );
}
if ( npery instanceof cArea || npery instanceof cArea3D ) {
npery = npery.cross( arguments[1].first );
}
else if ( npery instanceof cArray ) {
npery = npery.getElementRowCol( 0, 0 );
}
nominalRate = nominalRate.tocNumber();
npery = npery.tocNumber();
if ( nominalRate instanceof cError ) return this.value = nominalRate;
if ( npery instanceof cError ) return this.value = npery;
var nr = nominalRate.getValue(), np = npery.getValue();
if( nominalRate.getValue() <= 0 || npery.getValue() < 1 ){
return this.value = new cError( cErrorType.not_numeric );
}
return this.value = new cNumber( Math.pow( (1 + nr/np), np ) - 1 );
}
cEFFECT.prototype.getInfo = function () {
return {
name:this.name,
args:"( nominal-rate , npery )"
};
}
function cFV() {
// cBaseFunction.call( this, "FV" );
......@@ -264,14 +957,170 @@ function cIPMT() {
cIPMT.prototype = Object.create( cBaseFunction.prototype )
function cIRR() {
cBaseFunction.call( this, "IRR" );
// cBaseFunction.call( this, "IRR" );
this.name = "IRR";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 1;
this.argumentsCurrent = 0;
this.argumentsMax = 2;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cIRR.prototype = Object.create( cBaseFunction.prototype )
cIRR.prototype.Calculate = function ( arg ) {
var arg0 = arg[0], arg1 = arg[1] ? arg[1] : new cNumber(0.1);
function irr(arr,x) {
x = x.getValue();
var nC = 0, g_Eps = 1e-7, fEps = 1.0, fZ = 0, fN = 0, xN = 0, nIM = 100, nMC = 0, arr0 = arr[0], arrI, wasNegative = false, wasPositive = false;
if( arr0 instanceof cError ){
return new cError( cErrorType.not_available );
}
if( arr0.getValue() < 0 )
wasNegative = true;
else if( arr0.getValue() > 0 )
wasPositive = true;
if( arr.length < 2 )
return new cError( cErrorType.not_numeric );
while ( fEps > g_Eps && nMC < nIM ) {
nC = 0;
fZ = 0;
fN = 0;
fZ += arr0.getValue() / Math.pow( 1.0 + x, nC );
fN += -nC * arr0.getValue() / Math.pow( 1 + x, nC + 1 );
nC++;
for ( var i = 1; i < arr.length; i++ ) {
if(arr[i] instanceof cError){
return new cError( cErrorType.not_available );
}
arrI = arr[i].getValue()
fZ += arrI / Math.pow( 1.0 + x, nC );
fN += -nC * arrI / Math.pow( 1 + x, nC + 1 );
if( arrI < 0 )
wasNegative = true;
else if( arrI > 0 )
wasPositive = true ;
nC++
}
xN = x - fZ / fN;
nMC++;
fEps = Math.abs( xN - x );
x = xN;
}
if( !(wasNegative && wasPositive) )
return new cError( cErrorType.not_numeric );
if (fEps < g_Eps)
return new cNumber( x );
else
return new cError( cErrorType.not_numeric );
}
var arr = []
if( arg0 instanceof cArray ){
arg0.foreach(function(v){
arr.push(v.tocNumber())
})
}
else if( arg0 instanceof cArea ){
arg0.foreach2(function(v){
arr.push(v.tocNumber())
})
}
arg1 = arg1.tocNumber();
if( arg1 instanceof cError ){
return this.value = new cError( cErrorType.not_numeric );
}
this.value = irr(arr,arg1);
this.value.numFormat = 9;
return this.value;
}
cIRR.prototype.getInfo = function () {
return {
name:this.name,
args:"( values [ , [ guess ] ] )"
};
}
function cISPMT() {
cBaseFunction.call( this, "ISPMT" );
// cBaseFunction.call( this, "ISPMT" );
this.name = "ISPMT";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 4;
this.argumentsCurrent = 0;
this.argumentsMax = 4;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cISPMT.prototype = Object.create( cBaseFunction.prototype )
cISPMT.prototype.Calculate = function ( arg ) {
var rate = arg[0], per = arg[1], nper = arg[2], pv = arg[3];
if ( rate instanceof cArea || rate instanceof cArea3D ) {
rate = rate.cross( arguments[1].first );
}
else if ( rate instanceof cArray ) {
rate = rate.getElementRowCol( 0, 0 );
}
if ( per instanceof cArea || per instanceof cArea3D ) {
per = per.cross( arguments[1].first );
}
else if ( per instanceof cArray ) {
per = per.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 );
}
rate = rate.tocNumber();
per = per.tocNumber();
nper = nper.tocNumber();
pv = pv.tocNumber();
if ( rate instanceof cError ) return this.value = rate;
if ( per instanceof cError ) return this.value = per;
if ( nper instanceof cError ) return this.value = nper;
if ( pv instanceof cError ) return this.value = pv;
return this.value = new cNumber( pv.getValue() * rate.getValue() * (per.getValue() / nper.getValue() - 1.0) );
}
cISPMT.prototype.getInfo = function () {
return {
name:this.name,
args:"( rate , per , nper , pv )"
};
}
function cMDURATION() {
cBaseFunction.call( this, "MDURATION" );
......@@ -730,14 +1579,314 @@ function cVDB() {
cVDB.prototype = Object.create( cBaseFunction.prototype )
function cXIRR() {
cBaseFunction.call( this, "XIRR" );
// cBaseFunction.call( this, "XIRR" );
this.name = "XIRR";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 2;
this.argumentsCurrent = 0;
this.argumentsMax = 3;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cXIRR.prototype = Object.create( cBaseFunction.prototype )
cXIRR.prototype.Calculate = function ( arg ) {
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2] ? arg[2] : new cNumber( 0.1 );
function lcl_sca_XirrResult( rValues, rDates, fRate ){
/* V_0 ... V_n = input values.
D_0 ... D_n = input dates.
R = input interest rate.
r := R+1
E_i := (D_i-D_0) / 365
n V_i n V_i
f(R) = SUM ------- = V_0 + SUM ------- .
i=0 r^E_i i=1 r^E_i
*/
var D_0 = rDates[0];
var r = fRate + 1.0;
var fResult = rValues[0];
for( var i = 1, nCount = rValues.length; i < nCount; ++i )
fResult += rValues[i] / Math.pow( r, (rDates[i] - D_0) / 365.0 );
return fResult;
}
function lcl_sca_XirrResult_Deriv1( rValues, rDates, fRate ){
/* V_0 ... V_n = input values.
D_0 ... D_n = input dates.
R = input interest rate.
r := R+1
E_i := (D_i-D_0) / 365
n V_i
f'(R) = [ V_0 + SUM ------- ]'
i=1 r^E_i
n V_i n E_i V_i
= 0 + SUM -E_i ----------- r' = - SUM ----------- .
i=1 r^(E_i+1) i=1 r^(E_i+1)
*/
var D_0 = rDates[0];
var r = fRate + 1.0;
var fResult = 0.0;
for( var i = 1, nCount = rValues.length; i < nCount; ++i )
{
var E_i = (rDates[i] - D_0) / 365.0;
fResult -= E_i * rValues[i] / Math.pow( r, E_i + 1.0 );
}
return fResult;
}
function xirr( valueArray, dateArray, rate ){
var res = 0, vaTmp, daTmp;
var nC = 0, g_Eps = 1e-7, fEps = 1.0, fZ = 0, fN = 0, xN = 0, nIM = 100, nMC = 0, arr0 = valueArray[0], arr1 = dateArray[0], arrI, wasNegative = false, wasPositive = false;
if( arr0 instanceof cError ){
return new cError( cErrorType.not_available );
}
if( arr1 instanceof cError ){
return new cError( cErrorType.not_available );
}
if( valueArray.length < 2 || (dateArray.length != valueArray.length) )
return new cError( cErrorType.not_numeric );
var fResultRate = rate.getValue()
if( fResultRate <= -1 )
return new cError( cErrorType.not_numeric );
var fMaxEps = 1e-10, nMaxIter = 50;
for( var i = 0; i < dateArray.length; i++ ){
dateArray[i] = dateArray[i].tocNumber();
valueArray[i] = valueArray[i].tocNumber();
if( dateArray[i] instanceof cError || valueArray[i] instanceof cError )
return new cError( cErrorType.not_numeric );
dateArray[i] = dateArray[i].getValue();
valueArray[i] = valueArray[i].getValue();
}
// Newton's method - try to find a fResultRate, so that lcl_sca_XirrResult() returns 0.
var fNewRate, fRateEps, fResultValue, nIter = 0, bContLoop;
do
{
fResultValue = lcl_sca_XirrResult( valueArray, dateArray, fResultRate );
fNewRate = fResultRate - fResultValue / lcl_sca_XirrResult_Deriv1( valueArray, dateArray, fResultRate );
fRateEps = Math.abs( fNewRate - fResultRate );
fResultRate = fNewRate;
bContLoop = (fRateEps > fMaxEps) && (Math.abs( fResultValue ) > fMaxEps);
}
while( bContLoop && (++nIter < nMaxIter) );
if( bContLoop )
return new cError( cErrorType.not_numeric );
return new cNumber( fResultRate );
}
var dateArray = [], valueArray = [];
if( arg0 instanceof cArea ){
arg0.foreach2(function(c){
valueArray.push( c.tocNumber() );
})
}
else if( arg0 instanceof cArray ){
arg0.foreach(function(c){
valueArray.push( c.tocNumber() );
})
}
else if( arg0 instanceof cArea3D ){
if( arg0.wsFrom == arg0.wsTo ){
valueArray = arg0.getMatrix()[0];
}
else
return this.value = new cError( cErrorType.wrong_value_type );
}
else{
arg0 = arg0.tocNumber();
if( arg1 instanceof cError ){
return this.value = new cError( cErrorType.not_numeric )
}
else
valueArray[0] = arg0;
}
if( arg1 instanceof cArea ){
arg1.foreach2(function(c){
dateArray.push( c.tocNumber() );
})
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(c){
dateArray.push( c.tocNumber() );
})
}
else if( arg1 instanceof cArea3D ){
if( arg1.wsFrom == arg1.wsTo ){
dateArray = arg1.getMatrix()[0];
}
else
return this.value = new cError( cErrorType.wrong_value_type );
}
else{
arg1 = arg1.tocNumber();
if( arg1 instanceof cError ){
return this.value = new cError( cErrorType.not_numeric )
}
else
dateArray[0] = arg1;
}
if ( arg2 instanceof cArea || arg2 instanceof cArea3D ) {
arg2 = arg2.cross( arguments[1].first );
}
arg2 = arg2.tocNumber();
if ( arg2 instanceof cArray ) {
arg2 = arg2.getElement(0);
}
if ( arg2 instanceof cError ){
return this.value = arg2;
}
this.value = xirr( valueArray, dateArray, arg2 );
this.value.numFormat = 9;
return this.value;
}
cXIRR.prototype.getInfo = function () {
return {
name:this.name,
args:"( values , dates [ , [ guess ] ] )"
};
}
function cXNPV() {
cBaseFunction.call( this, "XNPV" );
// cBaseFunction.call( this, "XNPV" );
this.name = "XNPV";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 3;
this.argumentsCurrent = 0;
this.argumentsMax = 3;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cXNPV.prototype = Object.create( cBaseFunction.prototype )
cXNPV.prototype.Calculate = function ( arg ) {
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2];
function xnpv( rate, valueArray, dateArray ){
var res = 0, vaTmp, daTmp, r = rate.getValue()
if( dateArray.length != valueArray.length )
return new cError( cErrorType.not_numeric );
d1 = dateArray[0].tocNumber();
for( var i = 0; i < dateArray.length; i++ ){
vaTmp = valueArray[i].tocNumber();
daTmp = dateArray[i].tocNumber();
if( vaTmp instanceof cError || daTmp instanceof cError )
return new cError( cErrorType.not_numeric );
res += valueArray[i].getValue() / ( Math.pow( ( 1 + r ), ( dateArray[i].getValue()-d1.getValue() ) / 365 ) )
}
return new cNumber(res);
}
if ( arg0 instanceof cArea || arg0 instanceof cArea3D ) {
arg0 = arg0.cross( arguments[1].first );
}
arg0 = arg0.tocNumber();
if ( arg0 instanceof cArray ) {
arg0 = arg0.getElement(0);
}
if ( arg0 instanceof cError ){
return this.value = arg0;
}
var dateArray = [], valueArray = [];
if( arg1 instanceof cArea ){
arg1.foreach2(function(c){valueArray.push(c)})
// valueArray = arg1.getMatrix();
}
else if( arg1 instanceof cArray ){
arg1.foreach(function(c){valueArray.push(c)})
}
else if( arg1 instanceof cArea3D ){
if( arg1.wsFrom == arg1.wsTo ){
valueArray = arg1.getMatrix()[0];
}
else
return this.value = new cError( cErrorType.wrong_value_type );
}
else{
arg1 = arg1.tocNumber();
if( arg1 instanceof cError ){
return this.value = new cError( cErrorType.not_numeric )
}
else
valueArray[0] = arg1;
}
if( arg2 instanceof cArea ){
arg2.foreach2(function(c){dateArray.push(c)})
// dateArray = arg2.getMatrix();
}
else if( arg2 instanceof cArray ){
// dateArray = arg2.getMatrix();
arg2.foreach(function(c){dateArray.push(c)})
}
else if( arg2 instanceof cArea3D ){
if( arg2.wsFrom == arg2.wsTo ){
dateArray = arg2.getMatrix()[0];
}
else
return this.value = new cError( cErrorType.wrong_value_type );
}
else{
arg2 = arg2.tocNumber();
if( arg2 instanceof cError ){
return this.value = new cError( cErrorType.not_numeric )
}
else
dateArray[0] = arg2;
}
return this.value = xnpv(arg0, valueArray, dateArray);
}
cXNPV.prototype.getInfo = function () {
return {
name:this.name,
args:"( rate , values , dates )"
};
}
function cYIELD() {
cBaseFunction.call( this, "YIELD" );
......
......@@ -446,9 +446,71 @@ function cHYPERLINK() {
cHYPERLINK.prototype = Object.create( cBaseFunction.prototype )
function cINDEX() {
cBaseFunction.call( this, "INDEX" );
// cBaseFunction.call( this, "INDEX" );
this.name = "INDEX";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 2;
this.argumentsCurrent = 0;
this.argumentsMax = 4;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.def;
}
cINDEX.prototype = Object.create( cBaseFunction.prototype )
cINDEX.prototype.Calculate = function ( arg ) {
var arg0 = arg[0],
arg1 = arg[1] && !(arg[1] instanceof cEmpty) ? arg[1] : new cNumber(1),
arg2 = arg[2] && !(arg[2] instanceof cEmpty) ? arg[2] : new cNumber(1),
arg3 = arg[3] && !(arg[3] instanceof cEmpty) ? arg[3] : new cNumber(1),
isArrayForm = false, res;
if( arg0 instanceof cArea3D ){
return this.value = new cError( cErrorType.not_available );
}
else if( arg0 instanceof cError ){
return this.value = arg0;
}
arg1 = arg1.tocNumber();
arg2 = arg2.tocNumber();
arg3 = arg3.tocNumber();
if( arg1 instanceof cError || arg2 instanceof cError || arg3 instanceof cError ){
return this.value = new cError( cErrorType.wrong_value_type );
}
if( arg1.getValue() < 0 || arg2.getValue() < 0 ){
return this.value = new cError( cErrorType.wrong_value_type );
}
if( arg0 instanceof cArray ){
arg0 = arg0.getMatrix();
}
else if( arg0 instanceof cArea ){
arg0 = arg0.getMatrix();
}
else{
arg0 = [[arg0.tryConvert()]]
}
res = arg0[arg1.getValue()-1];
if( res )
res = res[arg2.getValue()-1];
return this.value = res ? res : new cError( cErrorType.bad_reference );
}
cINDEX.prototype.getInfo = function () {
return {
name:this.name,
args:"( array , [ row-number ] [ , [ column-number ] ] ) "+this.name+"( reference , [ row-number ] [ , [ column-number ] [ , [ area-number ] ] ] )"
};
}
function cINDIRECT() {
// cBaseFunction.call( this, "INDIRECT" );
......@@ -549,7 +611,6 @@ cINDIRECT.prototype.Calculate = function ( arg ) {
return this.value = found_operand;
}
return this.value = new cError( cErrorType.bad_reference );
}
......@@ -710,14 +771,160 @@ cLOOKUP.prototype.getInfo = function () {
}
function cMATCH() {
cBaseFunction.call( this, "MATCH" );
// cBaseFunction.call( this, "MATCH" );
this.name = "MATCH";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 2;
this.argumentsCurrent = 0;
this.argumentsMax = 3;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.def;
}
cMATCH.prototype = Object.create( cBaseFunction.prototype )
cMATCH.prototype.Calculate = function ( arg ) {
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2] ? arg[2] : new cNumber(1);
function findMatch(a0,a1,a2){
var a1RowCount = a1.length, a1ColumnCount = a1[0].length,
a0Value = a0.getValue(), a2Value = a2.getValue(),
arr = [], res = new cError( cErrorType.not_available ),
index = -1;
if( a1RowCount > 1 && a1ColumnCount > 1 ){
return new cError( cErrorType.not_available );
}
else if( a1RowCount == 1 && a1ColumnCount > 1 ){
for(var i = 0; i < a1ColumnCount; i++){
arr[i] = a1[0][i].getValue();
}
}
else if( a1RowCount > 1 && a1ColumnCount == 1 ){
for(var i = 0; i < a1RowCount; i++){
arr[i] = a1[i][0].getValue();
}
}
else {
arr[0]=a1[0][0];
}
if( !(a2Value == 1 || a2Value == 0 || a2Value == -1) ){
return new cError( cErrorType.not_numeric );
}
if( a2Value == -1 ){
for(var i = 0; i<arr.length; i++){
if( arr[i] >= a0Value ){
index = i;
}
else
break;
}
}
else if( a2Value == 0 ){
if( a0 instanceof cString ){
for(var i = 0; i<arr.length; i++){
if( searchRegExp2(arr[i].toString(),a0Value) ){
index = i;
}
}
}
else{
for(var i = 0; i<arr.length; i++){
if( arr[i] == a0Value ){
index = i;
}
}
}
}
else if( a2Value == 1 ){
for(var i = 0; i<arr.length; i++){
if( arr[i] <= a0Value ){
index = i;
}
else
break;
}
}
if( index > -1 )
res = new cNumber(index+1);
return res;
}
if( arg0 instanceof cArea3D || arg0 instanceof cArray || arg0 instanceof cArea ){
return this.value = new cError( cErrorType.not_available );
}
else if( arg0 instanceof cError ){
return this.value = arg0;
}
/* else{
}*/
if( !(arg1 instanceof cArray || arg1 instanceof cArea) ){
return this.value = new cError( cErrorType.not_available );
}
else {
arg1 = arg1.getMatrix();
}
if( arg2 instanceof cNumber || arg2 instanceof cBool ){
}
else if( arg2 instanceof cError ){
return this.value = arg2;
}
else{
return this.value = new cError( cErrorType.not_available );
}
return this.value = findMatch(arg0,arg1,arg2)
}
cMATCH.prototype.getInfo = function () {
return {
name:this.name,
args:"( lookup-value , lookup-array [ , [ match-type ]] )"
};
}
function cOFFSET() {
cBaseFunction.call( this, "OFFSET" );
// cBaseFunction.call( this, "OFFSET" );
this.name = "OFFSET";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 3;
this.argumentsCurrent = 0;
this.argumentsMax = 5;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.def;
}
cOFFSET.prototype = Object.create( cBaseFunction.prototype )
cOFFSET.prototype.Calculate = function ( arg ) {
var arg0 = arg[0], arg1 = arg[1], arg2 = arg[2], arg3 = arg[3] ? arg[3] : new cNumber(0 ), arg4 = arg[4] ? arg[4] : new cNumber(0);
if(1){}
}
cOFFSET.prototype.getInfo = function () {
return {
name:this.name,
args:"( reference , rows , cols [ , [ height ] [ , [ width ] ] ] )"
};
}
function cROW() {
// cBaseFunction.call( this, "ROW" );
......@@ -822,9 +1029,62 @@ function cRTD() {
cRTD.prototype = Object.create( cBaseFunction.prototype )
function cTRANSPOSE() {
cBaseFunction.call( this, "TRANSPOSE" );
// cBaseFunction.call( this, "TRANSPOSE" );
this.name = "TRANSPOSE";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 1;
this.argumentsCurrent = 0;
this.argumentsMax = 1;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.noneFormat;
}
cTRANSPOSE.prototype = Object.create( cBaseFunction.prototype )
cTRANSPOSE.prototype.Calculate = function ( arg ) {
function TransposeMatrix( A ) {
var tMatrix = [], res = new cArray();
for ( var i = 0; i < A.length; i++ ) {
for ( var j = 0; j < A[i].length; j++ ) {
if(!tMatrix[j]) tMatrix[j] = [];
tMatrix[j][i] = A[i][j];
}
}
res.fillFromArray( tMatrix );
return res;
}
var arg0 = arg[0];
if ( arg0 instanceof cArea || arg0 instanceof cArray ) {
arg0 = arg0.getMatrix();
}
else if( arg0 instanceof cNumber || arg0 instanceof cString || arg0 instanceof cBool || arg0 instanceof cRef || arg0 instanceof cRef3D ){
return this.value = arg0.getValue();
}
else if( arg0 instanceof cError ){
return this.value = arg0;
}
else
return this.value = new cError( cErrorType.not_available );
return this.value = TransposeMatrix( arg0 );
}
cTRANSPOSE.prototype.getInfo = function () {
return {
name:this.name,
args:"( array )"
};
}
function VHLOOKUPCache(bHor){
this.cacheId = {};
......
......@@ -4041,6 +4041,69 @@ function parseNum( str ) {
return !isNaN( str );
}
function matching( x, y, oper ) {
var res = false, rS;
if ( y instanceof cString ) {
rS = searchRegExp2( x.value, y.toString() )
switch ( oper ) {
case "<>":
res = !rS;
break;
case "=":
default:
res = rS;
break;
}
}
else 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;
}
function GetDiffDate360( nDay1, nMonth1, nYear1, bLeapYear1, nDay2, nMonth2, nYear2, bUSAMethod ) {
if ( nDay1 == 31 )
nDay1--;
else if ( bUSAMethod && ( nMonth1 == 2 && ( nDay1 == 29 || ( nDay1 == 28 && !bLeapYear1 ) ) ) )
nDay1 = 30;
if ( nDay2 == 31 ) {
if ( bUSAMethod && nDay1 != 30 ) {
nDay2 = 1;
if ( nMonth2 == 12 ) {
nYear2++;
nMonth2 = 1;
}
else
nMonth2++;
}
else
nDay2 = 30;
}
return ( nDay2 - nDay1 ) + ( nMonth2 - nMonth1 ) * 30 + ( nYear2 - nYear1 ) * 360;
}
function searchRegExp( str, flags ) {
var vFS = str
.replace( /(\\)/g, "\\" )
......
......@@ -7,45 +7,6 @@
* Time: 15:18
* To change this template use File | Settings | File Templates.
*/
function matching( x, y, oper ) {
var res = false, rS;
if ( y instanceof cString ) {
rS = searchRegExp2( x.value, y.toString() )
switch ( oper ) {
case "<>":
res = !rS;
break;
case "=":
default:
res = rS;
break;
}
}
else 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;
}
cFormulaFunction.Statistical = {
'groupName':"Statistical",
'AVEDEV':cAVEDEV,
......@@ -1564,6 +1525,10 @@ cFREQUENCY.prototype.Calculate = function ( arg ) {
function frequency( A, B ) {
function sort(a,b){
return a - b;
}
var tA = [], tB = [Number.NEGATIVE_INFINITY];
for ( var i = 0; i < A.length; i++ ) {
......@@ -1593,17 +1558,11 @@ cFREQUENCY.prototype.Calculate = function ( arg ) {
}
}
tA.sort( function ( a, b ) {
return a - b;
} )
tA.sort( sort );
tB.push( Number.POSITIVE_INFINITY );
tB.sort( function ( a, b ) {
return a - b;
} )
tB.sort( sort );
var C = [
[]
], k = 0;
var C = [[]], k = 0;
for ( var i = 1; i < tB.length; i++, k++ ) {
C[0][k] = new cNumber( 0 );
for ( var j = 0; j < tA.length; j++ ) {
......@@ -4604,19 +4563,296 @@ cSTDEVA.prototype.getInfo = function () {
}
function cSTDEVP() {
cBaseFunction.call( this, "STDEVP" );
// cBaseFunction.call( this, "STDEVP" );
this.name = "STDEVP";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 1;
this.argumentsCurrent = 0;
this.argumentsMax = 255;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.def;
}
cSTDEVP.prototype = Object.create( cBaseFunction.prototype )
cSTDEVP.prototype.Calculate = function ( arg ) {
function _var( x ) {
var tA = [], sumSQRDeltaX = 0, _x = 0, xLength = 0;
for ( var i = 0; i < x.length; i++ ) {
if ( x[i] instanceof cNumber ) {
_x += x[i].getValue();
tA.push( x[i].getValue() )
xLength++;
}
else if( x[i] instanceof cError ){
return x[i];
}
}
_x /= xLength;
for ( var i = 0; i < x.length; i++ ) {
sumSQRDeltaX += (tA[i] - _x) * (tA[i] - _x)
}
return new cNumber( Math.sqrt( sumSQRDeltaX / xLength ) );
}
var arr0 = [];
for ( var j = 0; j < this.getArguments(); j++ ) {
if ( arg[j] instanceof cArea || arg[j] instanceof cArea3D ) {
arg[j].foreach2( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
} );
}
else if ( arg[j] instanceof cRef || arg[j] instanceof cRef3D ) {
var a = arg[j].getValue();
if ( a instanceof cNumber || a instanceof cError ){
arr0.push( a );
}
}
else if ( arg[j] instanceof cArray ) {
arg[j].foreach( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
} );
}
else if ( arg[j] instanceof cNumber || arg[j] instanceof cBool ) {
arr0.push( arg[j].tocNumber() );
}
else if ( arg[j] instanceof cString || arg[j] instanceof cEmpty ) {
arr0.push( new cNumber(0) );
}
else
return this.value = cError( cErrorType.wrong_value_type )
}
return this.value = _var( arr0 );
}
cSTDEVP.prototype.getInfo = function () {
return {
name:this.name,
args:"( argument-list )"
};
}
function cSTDEVPA() {
cBaseFunction.call( this, "STDEVPA" );
// cBaseFunction.call( this, "STDEVPA" );
this.name = "STDEVPA";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 1;
this.argumentsCurrent = 0;
this.argumentsMax = 255;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.def;
}
cSTDEVPA.prototype = Object.create( cBaseFunction.prototype )
cSTDEVPA.prototype.Calculate = function ( arg ) {
function _var( x ) {
var tA = [], sumSQRDeltaX = 0, _x = 0, xLength = 0;
for ( var i = 0; i < x.length; i++ ) {
if ( x[i] instanceof cNumber ) {
_x += x[i].getValue();
tA.push( x[i].getValue() )
xLength++;
}
else if( x[i] instanceof cError ){
return x[i];
}
}
_x /= xLength;
for ( var i = 0; i < x.length; i++ ) {
sumSQRDeltaX += (tA[i] - _x) * (tA[i] - _x)
}
return new cNumber( Math.sqrt( sumSQRDeltaX / xLength ) );
}
var arr0 = [];
for ( var j = 0; j < this.getArguments(); j++ ) {
if ( arg[j] instanceof cArea || arg[j] instanceof cArea3D ) {
arg[j].foreach2( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
else if ( elem instanceof cBool ) {
arr0.push( elem.tocNumber() );
}
else{
arr0.push( new cNumber(0) );
}
} );
}
else if ( arg[j] instanceof cRef || arg[j] instanceof cRef3D ) {
var a = arg[j].getValue();
if ( a instanceof cNumber || a instanceof cError ){
arr0.push( a );
}
else if ( a instanceof cBool ) {
arr0.push( a.tocNumber() );
}
else{
arr0.push( new cNumber(0) );
}
}
else if ( arg[j] instanceof cArray ) {
arg[j].foreach( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
else if ( elem instanceof cBool ) {
arr0.push( elem.tocNumber() );
}
else{
arr0.push( new cNumber(0) );
}
} );
}
else if ( arg[j] instanceof cNumber || arg[j] instanceof cBool ) {
arr0.push( arg[j].tocNumber() );
}
else if ( arg[j] instanceof cString || arg[j] instanceof cEmpty ) {
arr0.push( new cNumber(0) );
}
else
return this.value = cError( cErrorType.wrong_value_type )
}
return this.value = _var( arr0 );
}
cSTDEVPA.prototype.getInfo = function () {
return {
name:this.name,
args:"( argument-list )"
};
}
function cSTEYX() {
cBaseFunction.call( this, "STEYX" );
// cBaseFunction.call( this, "STEYX" );
this.name = "STEYX";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 2;
this.argumentsCurrent = 0;
this.argumentsMax = 2;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.def;
}
cSTEYX.prototype = Object.create( cBaseFunction.prototype )
cSTEYX.prototype.Calculate = function ( arg ) {
function steyx( y, x ) {
var sumXDeltaYDelta = 0, sqrXDelta = 0, sqrYDelta = 0, _x = 0, _y = 0, xLength = 0;
if ( x.length != y.length )
return new cError( cErrorType.not_available );
for ( var i = 0; i < x.length; i++ ) {
if ( !( x[i] instanceof cNumber && y[i] instanceof cNumber ) ) {
continue;
}
_x += x[i].getValue();
_y += y[i].getValue();
xLength++;
}
_x /= xLength;
_y /= xLength;
for ( var i = 0; i < x.length; i++ ) {
if ( !( x[i] instanceof cNumber && y[i] instanceof cNumber ) ) {
continue;
}
sumXDeltaYDelta += (x[i].getValue() - _x) * (y[i].getValue() - _y);
sqrXDelta += (x[i].getValue() - _x) * (x[i].getValue() - _x);
sqrYDelta += (y[i].getValue() - _y) * (y[i].getValue() - _y);
}
if ( sqrXDelta == 0 || sqrYDelta == 0 || xLength < 3 )
return new cError( cErrorType.division_by_zero );
else
return new cNumber( Math.sqrt( (1/(xLength-2)) * (sqrYDelta - sumXDeltaYDelta * sumXDeltaYDelta / sqrXDelta) ) );
}
var arg0 = arg[0], arg1 = arg[1], arr0 = [], arr1 = [];
if ( arg0 instanceof cArea ) {
arr0 = arg0.getValue();
}
else if ( arg0 instanceof cArray ) {
arg0.foreach( function ( elem ) {
arr0.push( elem );
} );
}
else
return this.value = cError( cErrorType.wrong_value_type )
if ( arg1 instanceof cArea ) {
arr1 = arg1.getValue();
}
else if ( arg1 instanceof cArray ) {
arg1.foreach( function ( elem ) {
arr1.push( elem );
} );
}
else
return this.value = cError( cErrorType.wrong_value_type )
return this.value = steyx( arr0, arr1 );
}
cSTEYX.prototype.getInfo = function () {
return {
name:this.name,
args:"( known-ys , known-xs )"
};
}
function cTDIST() {
cBaseFunction.call( this, "TDIST" );
......@@ -4674,6 +4910,9 @@ cVAR.prototype.Calculate = function ( arg ) {
tA.push( x[i].getValue() )
xLength++;
}
else if( x[i] instanceof cError ){
return x[i];
}
}
......@@ -4713,7 +4952,7 @@ cVAR.prototype.Calculate = function ( arg ) {
else if ( arg[j] instanceof cNumber || arg[j] instanceof cBool ) {
arr0.push( arg[j].tocNumber() );
}
else if ( arg[j] instanceof cString ) {
else if ( arg[j] instanceof cString || arg[j] instanceof cEmpty ) {
continue;
}
else
......@@ -4731,19 +4970,310 @@ cVAR.prototype.getInfo = function () {
}
function cVARA() {
cBaseFunction.call( this, "VARA" );
// cBaseFunction.call( this, "VARA" );
this.name = "VARA";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 1;
this.argumentsCurrent = 0;
this.argumentsMax = 255;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.def;
}
cVARA.prototype = Object.create( cBaseFunction.prototype )
cVARA.prototype.Calculate = function ( arg ) {
function _var( x ) {
var tA = [], sumSQRDeltaX = 0, _x = 0, xLength = 0;
for ( var i = 0; i < x.length; i++ ) {
if ( x[i] instanceof cNumber ) {
_x += x[i].getValue();
tA.push( x[i].getValue() )
xLength++;
}
else if( x[i] instanceof cError ){
return x[i];
}
}
_x /= xLength;
for ( var i = 0; i < x.length; i++ ) {
sumSQRDeltaX += (tA[i] - _x) * (tA[i] - _x)
}
return new cNumber( sumSQRDeltaX / (xLength - 1) )
}
var arr0 = [];
for ( var j = 0; j < this.getArguments(); j++ ) {
if ( arg[j] instanceof cArea || arg[j] instanceof cArea3D ) {
arg[j].foreach2( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
else if ( elem instanceof cBool ) {
arr0.push( elem.tocNumber() );
}
else{
arr0.push( new cNumber(0) );
}
} );
}
else if ( arg[j] instanceof cRef || arg[j] instanceof cRef3D ) {
var a = arg[j].getValue();
if ( a instanceof cNumber || a instanceof cError ){
arr0.push( a );
}
else if ( a instanceof cBool ) {
arr0.push( a.tocNumber() );
}
else{
arr0.push( new cNumber(0) );
}
}
else if ( arg[j] instanceof cArray ) {
arg[j].foreach( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
else if ( elem instanceof cBool ) {
arr0.push( elem.tocNumber() );
}
else{
arr0.push( new cNumber(0) );
}
} );
}
else if ( arg[j] instanceof cNumber || arg[j] instanceof cBool ) {
arr0.push( arg[j].tocNumber() );
}
else if ( arg[j] instanceof cString || arg[j] instanceof cEmpty ) {
arr0.push( new cNumber(0) );
}
else
return this.value = cError( cErrorType.wrong_value_type )
}
return this.value = _var( arr0 );
}
cVARA.prototype.getInfo = function () {
return {
name:this.name,
args:"( argument-list )"
};
}
function cVARP() {
cBaseFunction.call( this, "VARP" );
// cBaseFunction.call( this, "VARP" );
this.name = "VARP";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 1;
this.argumentsCurrent = 0;
this.argumentsMax = 255;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.def;
}
cVARP.prototype = Object.create( cBaseFunction.prototype )
cVARP.prototype.Calculate = function ( arg ) {
function _var( x ) {
var tA = [], sumSQRDeltaX = 0, _x = 0, xLength = 0;
for ( var i = 0; i < x.length; i++ ) {
if ( x[i] instanceof cNumber ) {
_x += x[i].getValue();
tA.push( x[i].getValue() )
xLength++;
}
else if( x[i] instanceof cError ){
return x[i];
}
}
_x /= xLength;
for ( var i = 0; i < x.length; i++ ) {
sumSQRDeltaX += (tA[i] - _x) * (tA[i] - _x)
}
return new cNumber( sumSQRDeltaX / xLength )
}
var arr0 = [];
for ( var j = 0; j < this.getArguments(); j++ ) {
if ( arg[j] instanceof cArea || arg[j] instanceof cArea3D ) {
arg[j].foreach2( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
} );
}
else if ( arg[j] instanceof cRef || arg[j] instanceof cRef3D ) {
var a = arg[j].getValue();
if ( a instanceof cNumber || a instanceof cError ){
arr0.push( a );
}
}
else if ( arg[j] instanceof cArray ) {
arg[j].foreach( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
} );
}
else if ( arg[j] instanceof cNumber || arg[j] instanceof cBool ) {
arr0.push( arg[j].tocNumber() );
}
else if ( arg[j] instanceof cString || arg[j] instanceof cEmpty ) {
arr0.push( new cNumber(0) );
}
else
return this.value = cError( cErrorType.wrong_value_type )
}
return this.value = _var( arr0 );
}
cVARP.prototype.getInfo = function () {
return {
name:this.name,
args:"( argument-list )"
};
}
function cVARPA() {
cBaseFunction.call( this, "VARPA" );
// cBaseFunction.call( this, "VARPA" );
this.name = "VARPA";
this.type = cElementType.func;
this.value = null;
this.argumentsMin = 1;
this.argumentsCurrent = 0;
this.argumentsMax = 255;
this.formatType = {
def:-1, //подразумевается формат первой ячейки входящей в формулу.
noneFormat:-2
};
this.numFormat = this.formatType.def;
}
cVARPA.prototype = Object.create( cBaseFunction.prototype )
cVARPA.prototype.Calculate = function ( arg ) {
function _var( x ) {
var tA = [], sumSQRDeltaX = 0, _x = 0, xLength = 0;
for ( var i = 0; i < x.length; i++ ) {
if ( x[i] instanceof cNumber ) {
_x += x[i].getValue();
tA.push( x[i].getValue() )
xLength++;
}
else if( x[i] instanceof cError ){
return x[i];
}
}
_x /= xLength;
for ( var i = 0; i < x.length; i++ ) {
sumSQRDeltaX += (tA[i] - _x) * (tA[i] - _x)
}
return new cNumber( sumSQRDeltaX / xLength );
}
var arr0 = [];
for ( var j = 0; j < this.getArguments(); j++ ) {
if ( arg[j] instanceof cArea || arg[j] instanceof cArea3D ) {
arg[j].foreach2( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
else if ( elem instanceof cBool ) {
arr0.push( elem.tocNumber() );
}
else{
arr0.push( new cNumber(0) );
}
} );
}
else if ( arg[j] instanceof cRef || arg[j] instanceof cRef3D ) {
var a = arg[j].getValue();
if ( a instanceof cNumber || a instanceof cError ){
arr0.push( a );
}
else if ( a instanceof cBool ) {
arr0.push( a.tocNumber() );
}
else{
arr0.push( new cNumber(0) );
}
}
else if ( arg[j] instanceof cArray ) {
arg[j].foreach( function ( elem ) {
if ( elem instanceof cNumber || elem instanceof cError ){
arr0.push( elem );
}
else if ( elem instanceof cBool ) {
arr0.push( elem.tocNumber() );
}
else{
arr0.push( new cNumber(0) );
}
} );
}
else if ( arg[j] instanceof cNumber || arg[j] instanceof cBool ) {
arr0.push( arg[j].tocNumber() );
}
else if ( arg[j] instanceof cString || arg[j] instanceof cEmpty ) {
arr0.push( new cNumber(0) );
}
else
return this.value = cError( cErrorType.wrong_value_type )
}
return this.value = _var( arr0 );
}
cVARPA.prototype.getInfo = function () {
return {
name:this.name,
args:"( argument-list )"
};
}
function cWEIBULL() {
cBaseFunction.call( this, "WEIBULL" );
......
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