Offset in JavaScript UDF? VBA for Example

╄→гoц情女王★ 提交于 2019-12-11 16:36:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!