10 Replies Latest reply on Jun 23, 2015 1:22 PM by Bisser

    Sample Timesheet does not want to calculate!

    Bisser

      Hello Everyone!


      From this good thread I found a Sample Timesheet, I attached it.


      https://forums.adobe.com/message/1998662


      It works perfectly and I checked formulas included and data format of each cell.

      On base of the script, I tried to prepare own Sample Timesheet, as attached sample. I put the same script, but unfortunately the form does not calculate at all. Could somebody assist in this?

       

      Here I attach the samples - DropMeFiles – free one-click file sharing service

        • 1. Re: Sample Timesheet does not want to calculate!
          George_Johnson MVP & Adobe Community Professional

          The sample that you're using includes two document-level JavaScripts that contain code that you didn't copy to your new test form. You will need to do that so the same code is available to your form.

          • 2. Re: Sample Timesheet does not want to calculate!
            gkaiseril MVP & Adobe Community Professional

            Did the sample time sheet work?

             

            Do you think there is something that you have not copied from the sample form?

             

             

            Did you open the JavaScript console to see if there are any errors?

             

            I get the following error displaying in the JavaScript console:

             

             

            TimeDiff is not defined

            20:Field:Calculate

            ReferenceError: TimeDiff is not defined

            20:Field:Calculate

            TimeDiff is not defined

            20:AcroForm:total 2:Calculate

            ReferenceError: TimeDiff is not defined

            20:AcroForm:total 2:Calcullate

             

            "TimeDiff" is user defined function that computes the difference of 2 fields that have a time value.

            There is also a "Time2Num" function that converts the time string to minutes.

            Both of these functions are user written.

            • 3. Re: Sample Timesheet does not want to calculate!
              Bisser Level 1

              Dear George_Johnson, thanks a lot your answer and pointing me to correct answer.. I checked every cell in the sample an can see scripts included to subtotal fields. Subtotal for Sun has one – biggest script. Subtotals for Mon and Tue have the following:

              1. event.value = '';

              var fDiff = 0;

              // copute difference for first pair of fields

              fDiff = TimeDiff('Field12', 'Field13');

               

              // compute and add the difference for the second pair of fields

              fDiff += TimeDiff('Field15', 'Field16');

               

              // convert to rounded minutes if not zero

              if (fDiff != 0) {

              fDiff = Math.round(fDiff / 60);

              // report decimal hours

              1. event.value = fDiff / 60;

              }

              Also field total hours has the following script:

               

              var f = this.getField("Field53");

              1. f.hidden = (event.value < .0000001);

               

              I put it logically to my time sample and result is the same… What is wrong?

               

              DropMeFiles – free one-click file sharing service

              • 4. Re: Sample Timesheet does not want to calculate!
                Bisser Level 1

                Dear GKaiseril,

                thank you very much for your comments.. And thanks a lot for the time sheet.


                Actually I tried to summarize the 3 time period, and total hours calculate not correctly..

                Yes, you are right. I did not copy above two scripts… But with them the result is the same…

                I tried to open the  JavaScript console, but could not understand where nothing… Sorry.

                This mistake is appearing because of luck additional script I forgot to copy from your template.. But after I copied it, I have the same result… How to manage it?


                DropMeFiles – free one-click file sharing service

                • 5. Re: Sample Timesheet does not want to calculate!
                  gkaiseril MVP & Adobe Community Professional

                  You need to copy the functions to the Document Level Functions and add the code there. Without these document functions you would be repeating a lot of code all the time and if an error occurred you would need to fix the code every place it was used. A script function is a block of code that uses variables for the data that needs to be changed when the cod is run. This is exactly the same as a function in algebra or calculus.

                   

                  The JavaScript console shows error messages and if one looks for familiar strings and read the text before and after the strings one will get an idea about what the issue is. "TimeDiff not defined" is pretty clear there is something missing.

                   

                  Entering Document Scripts by Thom Parker

                  The Acrobat JavaScript Console (Your best friend for developing Acrobat JavaScript) by Thom Parker

                  • 6. Re: Sample Timesheet does not want to calculate!
                    gkaiseril MVP & Adobe Community Professional

                    You could also insert a working sample into you form and then delete the added page. That would add the document level scripts to your PDF form.

                    • 7. Re: Sample Timesheet does not want to calculate!
                      Bisser Level 1

                      Dear GKaiseril, Good day!

                       

                      Thank you very much for your explanations.

                      Ok, let me start from the beginning.. I set up my Java console and after checked scripts which belongs to your timesheet sample. Your document has 2 scripts. I put it to my document, and tried the calculations. There were no results… Perhaps it is normal…

                       

                      After I copied scripts which are included to fields in your document “Sun subtotal”, “Mon subtotal”, “Total hours” and put them to my equal columns.

                      The calculation starts to work. But actually calculated results are not correct, especially if for cells Time in and Time out used minutes.. Please, see attached.   http://dropmefiles.com/GAVaH

                       

                      Is it possible to fix?

                      • 8. Re: Sample Timesheet does not want to calculate!
                        Bisser Level 1

                        Dear GKaiseril,

                         

                        As you recommended I put the two scripts to whole document.

                        Each script I tried to run thru JavaScript console. In both cases I have the same mistake:

                         

                        Time2Num & TimeDiff:

                         

                        SyntaxError: syntax error

                        1:Console:Exec

                        Undefined

                         

                        As I wrote earlier, this time the document does calculations, but incorrectly…

                        Could you advice what should I di with the mistake?

                        http://dropmefiles.com/1rsot

                        • 9. Re: Sample Timesheet does not want to calculate!
                          gkaiseril MVP & Adobe Community Professional

                          You cannot run the scripts through the JavaScript console unless you include the code for the Time2Num and TimeDiff function code as part of the script.

                          I have downloaded the time sheet provided in the quoted email that I had provided. The scripts still work. The only restriction is that the time sheets will not work properly if the time period being computed include that interval for the change in Summer and Winter times.

                           

                          You must be doing something wrong.

                           

                          Try this file:

                           

                          https://forums.adobe.com/servlet/JiveServlet/download/1998662-8927/SimpleTimeSheet.pdf

                           

                          It is the file link from the referenced email.

                          • 10. Re: Sample Timesheet does not want to calculate!
                            Bisser Level 1

                            Dear GKaiseril,

                             

                            First of all, thank you very much for your message and the great example!

                            I checked the scripts which included to whole document, this time where indicated 4 scripts and checked the total summarizing cells.

                            I put the scripts to my time schedule…

                            As result, I can see that the time is calculating correctly! Thank you.

                             

                            But it calculates perfectly for one pair of Time in and Time out.

                            My time schedule has 2 pairs of Time in and Time out (in line).

                            Could you advice which correction should I do in the following script to run it correctly?:

                             

                            1. event.value = '';

                            // get the start time

                            var sStart = this.getField('TimeIn.0').value;

                            // get the end time

                            var sEnd = this.getField('TimeOut.0').value;

                            // complete script if we have data

                            if(sStart != '' & sEnd != '') {

                            // convert sStart string to seconds

                            var fStart = Time2Num('hh:mm', sStart);

                            // convert sEnd string to seconds

                            var fEnd = Time2Num('hh:mm', sEnd);

                            // convert sEnd string to seconds

                            var fEnd = Time2Num('hh:mm', sEnd);

                            // compute difference in seconds

                            var fDiff = fEnd - fStart;

                            // convert to rounded minutes

                            fDiff = Math.round(fDiff / 60);

                            // get hours from fDiff

                            fHours = Math.floor(fDiff / 60);

                            // get minutes less than hour from fDiff

                            fMins = ((fDiff / 60) - fHours ) * 60;

                            // format fMins with leading zero

                            sMins = util.printf("%,302.0f", fMins);

                            // build display string

                            1. event.value = fHours + ':' + sMins;

                            }

                             

                             

                            Also the same question for my “grand total” field. Currently it has the following script (copied from your one):


                            var sum = 0; // value for sum of seonds worked

                            var fSecWorked = 0; // seconds worked in a day

                            var oDay = this.getField('TimeHHMM'); // field object for HHMM worked in one day

                            var aDay = oDay.getArray(); // convert field object to an array of field objects

                            // convert each day field to seconds worked

                            for(i = 0; i < 5; i++) {

                            // convert to seconds using document level function

                            fSecWorked = sTime2Num(aDay[i].value)

                            // add to sum

                            sum += Number(fSecWorked);

                            }

                            // compute minutes from total seconds

                            var fMins = Math.round(sum / 60);

                            // compute hours from total minutes

                            var fHrs = Math.floor(fMins / 60)

                            // get fractional minutes

                            fMins = fMins - (fHrs * 60);

                            // format results

                            1. event.value = fHrs + ':' + util.printf('%,302.0f', fMins);

                             

                            In such case, as you can see, the formula does not round as per 60 minutes. Could you advice?

                            https://www.dropbox.com/s/k3qob41do3guxb4/test_1_1%2023.06_.pdf?dl=0