问题
I asked the same question before here (For Loop JavaScript? VBA for Example) but I was not specific enough so I did not get the feedback I was looking for. How can I create a similar code in JavaScript? This might help https://poi.apache.org/apidocs/dev/org/apache/poi/ss/formula/functions/Offset.html
What I have here is a Parent-Child Cost Roll-up Calculation in VBA. Column I has the formula that is in Column H. I want to create a formula similar to this in JavaScript.
Function ParentChildCalc(critCell As Range, valCell As Range) As Double
Dim numYes As Long
Dim i As Long
Dim temp As Double
Do Until i = 5000
If critCell.Offset(i, 0) = "Yes" Then numYes = numYes + 1 'If the cell below says "Yes" then this sums all the 'valCells' until that "yes"
If numYes = 2 Then Exit Do 'We only want to sum the numbers between each one of the "Yes's"
temp = temp + valCell.Offset(i, 0) 'see Console.log(temp) tab to see what temp does
i = i + 1
Loop
ParentChildCalc = temp
End Function
Here is my best shot at JavaScript. I plan on using this with Office.js. I do not know how to offset.
/**
* ParentChildCalc1
* @customfunction ParentChildCalc1
* @param {string} critCell Critical Cell
* @param {number} valCell Value Cell
* @returns {number} ParentChildCalc1
*/
function ParentChildCalc1(critCell, valCell) {
var numYes = 1;
var i = 1;
var temp = 1;
while (i < 5000) {
if (critCell.getOffsetRange(i, 0) == "Yes") {
numYes++;
}
if (numYes == 2) {
break;
}
temp = temp + valCell.getOffsetRange(i, 0);
i++;
}
return temp;
}
来源:https://stackoverflow.com/questions/58730532/offset-in-javascript-udf-vba-for-example