Hi All

I have found a solution to my problem of calculating the sum of the
value in a data field that is present in many tiddlers. I was not able
to figure out a way to use fET, but instead used an inline script. The
script needs the plugin http://www.tiddlytools.com/#InlineJavascriptPlugin
and the plugin http://tiddlywiki.abego-software.de/#DataTiddlerPlugin.
I made the script by modifying code that I found posted by Måns for
creating a table from data in many tiddlers. If anyone has any
improvements to make, I would be pleased to hear any suggestions.

Here is an example script.
<script>
   var out=[];
   var sum=0;
   var tids=store.getTaggedTiddlers('timesheet');
      for (var i=0; i<tids.length; i++){
      var hr=tids[i].data("hours")*1;
      if (isNaN(hr)) {sum = sum;}
      else {sum=sum+hr;}
   }
   out.push(sum+' hours completed.');
   return out.join('\n');
</script>

Here is a brief description of what is happening with the script.
A variable 'out' is created as a blank array, and a variable 'sum' is
created to be the number 0.
Tiddlers with the tag 'timesheet' are stored in the variable 'tids'.
For each of these tiddlers, the variable 'hr' is created to contain
the value from the "hours" data field from the tiddler. The
DataTiddlerPlugin gives the ability to obtain this data using the
syntax: tiddler.data("fieldname").
The result from the data field is multiplied by 1 so that a text from
the "hours" field is converted to a number.
The function isNaN checks the result to see if it is a number or not.
'NaN' stands for 'Not a Number'. isNaN(hr) will be true if the "hours"
field contained data that wasn't a number, or if the "hours" field was
not present in the tiddler.
If the 'hr' value was not a number, the 'sum' variable stays the same,
otherwise, the 'hr' value is added to the 'sum' value to give a new
sum.
The 'sum' variable and the string ' hours completed.' is put into the
'out' array.
The contents of the 'out' array are written to the tiddler and a new
line '\n' added to the output.

Here is the actual script that I use. I need to complete 200 hours for
a study project. This script tells me how many hours I have completed,
how many hours I have remaining to do, or how many hours I am over the
required 200 hours. I have formatted the result as italics. You may
find this script useful.
//<script>
   var out=[];
   var sum=0;
   var rem=200;
   var remword="";
   var tids=store.getTaggedTiddlers('timesheet');
      for (var i=0; i<tids.length; i++){
      var hr=tids[i].data("hours")*1;
      if (isNaN(hr)) {sum = sum;}
      else {sum=sum+hr;}
   }
   if (sum<200) {rem=rem-sum; remword="remaining.";} else {rem=sum-
rem; remword="over.";}
   out.push(sum+' hours completed, '+rem+' hours '+remword);
   return out.join('\n');
</script>//

Cheers
Andrew

On Jun 1, 3:00 pm, AndrewMc <newsp...@post.com> wrote:
> Hi All
>
> I have set up a form for entering timesheet information of the time
> spent working on a study project. Each timesheet record is a separate
> tiddler. I have used forEachTiddler (fET) to produce a table that
> lists all of the records. What I would like to do next is to use fET
> to calculate the total of the hours. That is, I want to sum or add up
> the values from each tiddler into a single total value that I can
> display. Does anyone have any advice on how can I do this?
>
> I can get a plain list of the hours using the following fET:
> <<forEachTiddler
>  where
>  'tiddler.title.startsWith("Time_test_")'
>  sortBy
>  'tiddler.created'
>  descending
>  write
>   'tiddler.data("hours")+"\n"'
>   begin '"Plain list of hours\n"'
>   none '"No timesheets found\n"'
>
>
>
> Ideally, I would like to have the information appear at the top of my
> summary tiddler and look something like:
> "45 hours completed, 55 hours remaining"
>
> Cheers
> Andrew Mc
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To post to this group, send email to TiddlyWiki@googlegroups.com
To unsubscribe from this group, send email to 
tiddlywiki+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/TiddlyWiki?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to