Copy link to clipboard
Copied
I have three field types I need to do a calculation on. I have 18 possible entries on a form that is basically a timesheet. User enters hours worked and chooses the service they performed from a drop-down. At the bottom of the form, there is a text box to display the total hours worked for that activity. There are 9 of these boxes, one for each service category. What I want to do is have the form calculate these totals. For example, John Smith did 4 hours on day 1 of the month doing Community Service. On Day 10 he also did Community Service. That would be two of the 18 services performed rows. There arw 18 rows that contain the Hours served and the type of service. This total would need to be added to the Community Service total box. There remain 16 rows that could be any of the other 8 service types.
To sum it up, I want to be able to have the one service type add up the total hours from the total hours fields in the two rows that are Community Service in this example. Keeping in mind this will need to be done for each service type that may appear in the form. We also have no way of knowing ahead of time what order entries will be made, as they take place on different dates.
Copy link to clipboard
Copied
Here's an example of how to calculate the total time for Meetings, for example:
var total = 0;
for (var i=1; i<=18; i++) {
if (this.getField("ServiceHeading"+i).valueAsString=="Mettings")
total+=Number(this.getField("HoursServed"+i).valueAsString);
}
event.value = total;
Copy link to clipboard
Copied
This is possible, using a custom-made script. There are two steps here:
- Calculate the total time worked for each row.
- Add up those times depending on the Type of Service for all the rows.
There have been many discussions here on the former, including full code samples. I suggest trying to search the forum for "worksheet calculation" or something similar.
If the results are in numeric form (ie. 1.25, instead of 1:15) then the second step is pretty straight forward. You would just need a loop that checks the Type of Service value in each row and then adds them up to get a total. If they are in the latter form then it's a bit more complex, as you'll first need to convert those values to numbers, then add them up, then convert the result back to an HH:MM format.
Copy link to clipboard
Copied
I have searched for hours and cannot find a solution. I do not know where to begin on either step
First question is how to get the Items in the list to be recognized individually so they can be tallied by row with the Time entry column on that row. I assume this is the first step you mentioned. I am using number fields and time spent per activity is entered 1.00 for example, in 15 minute increments (.25, .50, .75). on the day 1 of the month (a single row, row 1) we have 1 hour of community service. on day 3, we have 2 hours of community service (row 3). I want to add hours on row 1 to hours on row 3 to calculate a total community service hours result to populate the community service box at the bottom. This same logic would be used on the other list items like meetings, for example. The key is, it needs to know the difference between meeting hours and community service hours, so I obviously can't just sum the hours column like I did for the total hours for the month.
I have attached a snip of a sample form, leaving out the date and details, just the fields I need to use to do the calculation have sample info.
Copy link to clipboard
Copied
I found this code you posted some time ago, but it only shows the total number of occurrences of that list item and displays that as the hours for that service type. I am not a coder by any means, so please bear with me.
var total = 0; for (var i=1; i<=83; i++) { var fname = "Dropdown"+i; var f = this.getField(fname); if (f==null) { console.println("Error: " + fname); continue; } if (f.valueAsString=="S") { total++; } } event.value = total;
So I still haven't figured out how to use the hours associated with each service type by row to get the corrct total.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Not sure what this image is supposed to say to me...
Copy link to clipboard
Copied
to demonstrate the cells and info I want to calculate that I can't find an existing script that works that will accomplish this, after three days of searching. This should be simple, but I'm not a programmer. The
The category list is ServiceHeading1-18. The hours worked is HoursServed1-18. The boxes for the total by service/task are labeled/named as shown.
Thanks for any help you can offer on this auto calculation scri[ting.
Copy link to clipboard
Copied
Here's an example of how to calculate the total time for Meetings, for example:
var total = 0;
for (var i=1; i<=18; i++) {
if (this.getField("ServiceHeading"+i).valueAsString=="Mettings")
total+=Number(this.getField("HoursServed"+i).valueAsString);
}
event.value = total;
Copy link to clipboard
Copied
Thanks. Worked like a charm.

