Copy link to clipboard
Copied
ok, so I'm trying to make a field be document level script based on whether it's the first/last field in a range of fields with a value. So "Field1" has charge code 8008 and "FieldA-G" has a $40. Then the FieldA and FieldG values should be calculated at 75% and the rest stay the same.
Could possibly work like this in powershell. But i need it in JS
$global:arrFieldsWithValues = @()
function TrackField($field) {
if ($field > 0){
$global:arrFieldsWithValues += $field
}
}
if ($global:arrFieldsWithValues.Count -eq 1) {
$Total = $global:arrFieldsWithValues[0] * .75
}
if ($global:arrFieldsWithValues.Count -eq 2) {
$Total = $global:arrFieldsWithValues[0] * .75
$Total += $global:arrFieldsWithValues[1] * .75
}
if ($global:arrFieldsWithValues.Count -ge 3)
{
$Total = $global:arrFieldsWithValues[0] * .75
$Total += $global:arrFieldsWithValues[$global:arrFieldsWithValues.Count - 1] * .75
for($i = 1; i$ < ($global:arrFieldsWithValues.Count - 1); $i++)
{
$Total += $global:arrFieldsWithValues[$i]
}
}
What I ended up with works but is different from what my original post suggest. Originally the charge code for the field I'm working with were part of a drop down. But I ended up making an area for this script static and removed it from the drop down list. Thanks goes to try67 for his guidance to getting me to my goal. It can likely be cleaned up a little but it works.
function Calc2(ndays, nrates) {
var travelDay = 0.75;
for (var arrDays=ndays.length-1; arrDays>=0; arrDays--) {
var
...Copy link to clipboard
Copied
Without seeing the file and understanding what the values are it's very difficult to help you further with this.
If you're getting NaN it means you're trying to perform a mathematical operation on something that isn't a number, though.
Copy link to clipboard
Copied
Understandable, but there's only numeric values in it. And I'm only calling .value, i also tried valueasstring. I just noticed that even when I have no values in fields I get NaN...
Copy link to clipboard
Copied
Sorry, I can't help further without seeing the file.
Copy link to clipboard
Copied
I'm willing to send it if you'll take a look. I've been trying to get it to work, but still not sure what I'm overlooking...
Copy link to clipboard
Copied
Sure, send it over.
Copy link to clipboard
Copied
You have a lot of errors, right off the bat. For example, this line doesn't make sense:
for (var arrDays=ndays.length-1; ndays>=0; ndays--) {
Is arrDays a number, or an array? Is ndays a number, or an array?
And the next line doesn't make much sense, either:
var f = this.getField(arrDays[arrDays]);
You can't use the same variable both as the array and the index number. You need to sort out the names of your variables.
Copy link to clipboard
Copied
ndays = array
arrDays = ndays minus blank objects
so I changed it to
for (var arrDays=ndays.length-1; arrDays>=0; arrDays--)
var f = this.getField(ndays[arrDays]);
and I fixed the other pieces. I'll look through the rest to see if I can find more like that
Copy link to clipboard
Copied
In that case arrDays is not "ndays minus blank objects", it's a number representing the length of the ndays array.
This code looks better, though.
Copy link to clipboard
Copied
What I ended up with works but is different from what my original post suggest. Originally the charge code for the field I'm working with were part of a drop down. But I ended up making an area for this script static and removed it from the drop down list. Thanks goes to try67 for his guidance to getting me to my goal. It can likely be cleaned up a little but it works.
function Calc2(ndays, nrates) {
var travelDay = 0.75;
for (var arrDays=ndays.length-1; arrDays>=0; arrDays--) {
var f = this.getField(ndays[arrDays]);
if (f.valueAsString=="") ndays.splice(arrDays, 1);
var g = this.getField(nrates[arrDays]);
if (g.valueAsString=="") nrates.splice(arrDays, 1);
}
if (ndays.length <= 0 || nrates.length <= 0)event.value = "";
if (ndays.length == 1 && nrates.length == 1){
var days = this.getField(ndays[0]);
var tDays = days.value;
if (tDays == 1){
cdays = tDays * travelDay;
event.value = cdays;
}
else if (tDays == 2){
cDays = (tDays * travelDay) * 2;
event.value = cDays;
}
else if (tDays >= 3){
var oDays = tDays-2;
var rates = this.getField(nrates[0]).value;
var calcRate = oDays * rates;
var calcFLDay = (2 * rates) * travelDay;
event.value = calcRate+calcFLDay;
}
}
if (ndays.length >= 2 && nrates.length >= 2){
var FirstDay = this.getField(nrates[0]).value;
var LastDay = this.getField(nrates[nrates.length-1]).value;
var travelRate = (FirstDay+LastDay)*travelDay;
var iFirst = this.getField(ndays[0]).value -1;
var iLast = this.getField(ndays[ndays.length-1]).value -1;
travelRate += iFirst * this.getField(nrates[0]).value;
if (ndays.length > 2) travelRate += this.getField(ndays[1]).value * this.getField(nrates[1]).value;
travelRate += iLast * this.getField(nrates[ndays.length-1]).value;
event.value = travelRate;
}
}