convert VBA ---> GAS
Код:
Function SumFIFO(Products As Range, Product As String, amt As Range, price As Range)
Dim prod(), kol(), cost(), out(), i&, j&, sum#, sum2#, pr#
If Products.Count < 2 Or amt.Count < 2 Or price.Count < 2 Then SumFIFO = amt * price: Exit Function
prod = Products.Columns(1).Value
kol = amt.Columns(1).Value
cost = price.Columns(1).Value
If UBound(prod, 1) <> UBound(kol, 1) Or UBound(kol, 1) <> UBound(cost, 1) Then SumFIFO = CVErr(xlErrValue): Exit Function
ReDim out(1 To UBound(kol, 1), 1 To 1)
For i = 1 To UBound(out, 1)
If prod(i, 1) = Product Then
sum = 0
If kol(i, 1) < 0 Then
pr = -kol(i, 1)
For j = 1 To i
If kol(j, 1) > 0 Then
If kol(j, 1) >= pr Then
kol(j, 1) = kol(j, 1) - pr
cost(i, 1) = (sum + pr * cost(j, 1)) / -kol(i, 1)
pr = 0: Exit For
Else
pr = pr - kol(j, 1)
sum = sum + kol(j, 1) * cost(j, 1)
kol(j, 1) = 0
End If
End If
Next j
If pr <> 0 Then out(i, 1) = 0: Exit For
End If
sum2 = sum2 + kol(i, 1) * cost(i, 1)
Else
kol(i, 1) = 0
End If
out(i, 1) = sum2
Next i
SumFIFO = out
End Function
Dim prod(), kol(), cost(), out(), i&, j&, sum#, sum2#, pr#
If Products.Count < 2 Or amt.Count < 2 Or price.Count < 2 Then SumFIFO = amt * price: Exit Function
prod = Products.Columns(1).Value
kol = amt.Columns(1).Value
cost = price.Columns(1).Value
If UBound(prod, 1) <> UBound(kol, 1) Or UBound(kol, 1) <> UBound(cost, 1) Then SumFIFO = CVErr(xlErrValue): Exit Function
ReDim out(1 To UBound(kol, 1), 1 To 1)
For i = 1 To UBound(out, 1)
If prod(i, 1) = Product Then
sum = 0
If kol(i, 1) < 0 Then
pr = -kol(i, 1)
For j = 1 To i
If kol(j, 1) > 0 Then
If kol(j, 1) >= pr Then
kol(j, 1) = kol(j, 1) - pr
cost(i, 1) = (sum + pr * cost(j, 1)) / -kol(i, 1)
pr = 0: Exit For
Else
pr = pr - kol(j, 1)
sum = sum + kol(j, 1) * cost(j, 1)
kol(j, 1) = 0
End If
End If
Next j
If pr <> 0 Then out(i, 1) = 0: Exit For
End If
sum2 = sum2 + kol(i, 1) * cost(i, 1)
Else
kol(i, 1) = 0
End If
out(i, 1) = sum2
Next i
SumFIFO = out
End Function