I didn’t know it was there. I’ve been using the Script Editor to get those inserted.
Thanks.
I appreciate the way you organized it. That is a lot easier to read.
I’m having trouble reading and understanding the objects, etc. and have to study a bit more to understand it. When done I’ll attempt another use for the code finding out how many days between the date of the last treatment of the previous entry and the date of the current entry and record that in a Days between treatments.
I have other work to get done first though. Even though I’m 79, I can’t get enough done with my insect collection and prepare for another season of collecting. Being retired is a lot of work.
Great that it works for you too!
Yeah, to make it easier I consolidated all the field IDs at the top. To get the total_id, which is the ID of the script field, you need to close the script editor and look at the form editor. See attachment. You can get all the other IDs from the left side panel in the script editor.
Attachments:
You must be
logged in to view attached files.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
While working with it I noticed the IDs were wrong also as you pointed out. Don’t know how that happened.
I’ll have to compare this script with the previous one to see what you did, but with the changes you mentioned in the program and your last script, it worked.
Thanks for the work. Hopefully I can transfer this to other forms and make it work there also.
You need to select ‘script’ and not ‘calculation’ for the field type, return type is ‘number’.
The script had some errors too like incorrect field ids.
function Season_to_date() {
var date_id = 'fld-e2d20da877cb4a6c8fd72153b86f1ab1';
var donation_id = 'fld-05c429d6a4484061b554aa584a45f8fc';
var total_id = 'fld-7c6b9bb288ab47e0b295af6eacc9cd26';
var records = form.getRecords();
var currentRecordIndex = records.indexOf(record);
if (currentRecordIndex > 0) {
var previousRecord = records[currentRecordIndex-1];
} else {
return 0;
}
// Is this the beginning of the year?
var date = record.getFieldValue(date_id);
if (date.getMonth() == 0 && date.getDate() == 1){
return 0;
}
var today = record.getFieldValue(donation_id);
var previous_total = previousRecord.getFieldValue(total_id);
var total = today + previous_total;
console.log("Today: " + today)
console.log("Previous: " + previous_total)
console.log("Total: " + total)
return total;
}
Season_to_date();
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
I did have a record that had no data, date or values. Deleted that. Also made sure my added records for Jan 1 had the value = 0 for donations (that field was empty).
Now the script runs without errors with the original lines, or those lines replaced with the above correction for undefined.
But, all my records have nothing in the Totals value. All blank.
I checked the script window and the value = number. Also the field Total is calculated and that value = decimal with zero decimal places.
Stopped the program, loaded it again and brought up the Form and refreshed it many times and no totals show up.
Is there a way to do this?
Thanks. Code below just so you know what I’m working with.
function Season_to_date() {
var records = form.getRecords();
var currentRecordIndex = records.indexOf(record);
if (currentRecordIndex > 0) {
var previousRecord = records[currentRecordIndex-1];
} else {
return 0;
}
// Is this the beginning of the year?
var date = record.getFieldValue('fld-0720a5baa6fc4980aee22798bd089714');
if (date != undefined && date.getMonth() == 0 && date.getDate() == 1){
}
// if (date.getMonth() == 0 && date.getDate() == 1){
// return 0;
//}
var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3');
var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4');
var total = today + previous_total;
console.log("Today: " + today)
console.log("Previous: " + previous_total)
console.log("Total: " + total)
return total;
}
Season_to_date();
I just ran it again to verify and I get using one statement:
2/16/23, 9:26:49 AM / Donations / Yearly
Yearly: TypeError: undefined is not an object (evaluating ‘date.getMonth’), line:(null)
The other statement gives me:
2/16/23, 9:29:25 AM / Donations / Yearly
Today: undefined
Previous: undefined
Total: NaN
I’m really confused. Don’t know what is going on.
Here is my file.
I would like to see my donations for any single year that I can see with a search view.
I have a script in the Total Field and a similar one (maybe different now) in the Script designer field.
Attachments:
You must be
logged in to view attached files.
Scripts can’t be run line by line due to how Apple’s JavascriptCore framework works.
The best you can do is to put console.log("got here") or console.log(someVariable) statements into your code.
But when you see undefined, it means exactly that. The value you’re trying to call a method on is empty or hasn’t been initialized with a value.
So you should add date != undefined into your if statement where you call getMonth().
if (date != undefined && date.getMonth() == 0 && date.getDate() == 1){
}
You may have a record that has no date value, so that’s what would cause that error.
The TF crashed. When it opened and I ran refresh again.
I get no errors:
2/15/23, 6:48:31 PM / Donations / Yearly
But, all my values in the Total col are blank.
Would help if the script could be run line by line. I assume it can, but haven’t found it.
Oh that’s weird. I didn’t know JavaScript had different scope rules than most every other programming language I know.
That didn’t help. I get error in line 16 and playing with that I’m not sure where I’m at.
Added a backtick char.
Here is the code I was using for my rain totals, but the rain season was from Oct 1 to Sept 31 of each year which required a loop I think to check for the end of the season. Since for this purpose, the year I’m grouping is for the tax year Jan 1 to Dec 31. So, didn’t think I needed the search for Oct 1 date. I thought if I took out that portion that this script would do the job of letting me see all years worth of donations and set up a view of only one specific year and see donations starting at 0 and adding up the amounts until Dec 31. Seemed simple.
This is the Rain script, so I assume I have to replace the id code like this (‘fld-0720a5baa6fc4980aee22798bd089714) with the id code for the field I want in my donation form.
So, do I have to start over or can I use this code with the correct changes. Sorry, thought this would be easy. Hopeful and thank you.
================
function Season_to_date() {
var records = form.getRecords();
var currentRecordIndex = records.indexOf(record);
if (currentRecordIndex > 0) {
var previousRecord = records[currentRecordIndex-1];
} else {
return 0;
}
// Is this the beginning of the year?
var date = record.getFieldValue('fld-0720a5baa6fc4980aee22798bd089714');
if (date.getMonth() == 9 && date.getDate() == 1){
return 0;
}
var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3');
var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4');
var total = today + previous_total;
console.log("Today: " + today)
console.log("Previous: " + previous_total)
console.log("Total: " + total)
return total;
}
Season_to_date();
There’s no need for scripting just to get totals. You can just set the Calculation Summary option on all the fields you want to get a total for to appear at the bottom of the records list.
I haven’t had time to write the form for you Yvette. Sorry about that. But I would suggest starting with just creating the form as you have it now in the spreadsheet. Just add the fields one by one setting the field types appropriately. Then set the Group Records By option to Category and the First Sort Field to whatever the next level of sorting it is that you want. Probably the Name field.
Then set the Calculation Summary setting for each of your Number fields. Or click the ∑ symbol at the bottom of the Multi-Column List View to turn on the Calculation Summary Row. Now you can click the popup buttons to have Tap Forms generate totals for you.
To see sub-totals for each category, just enable the Show Group Summaries option available from the little menu button that appears at the top-right of the multi-column list view.
Hi Kurtois,
That’s the video channel for Sam Moffatt. You’ll find him fairly active on the forums here. Especially the Script Talk forum.
For the best tutorial videos, you can find links to them on my Support page (https://www.tapforms.com/support) or select Video Tutorials from the Help menu in Tap Forms 5 for Mac.
Or here for Mac:
https://vimeo.com/channels/tapformsmac5
And here for iOS:
https://vimeo.com/channels/tapformsios
Thanks for the script for rain totals. Now I figure I can make that do the same for donations. Am I on the right track? I keep getting syntax errors or other errors trying to get the syntax right. Am I way off track? Attached is my screen shot. I can’t seem to get the {} deletions right.
– I deleted the “beginning of the year” section.
– Replaced the variables “today” & “previous_total” ID values with the ones appropriate for my form. Now it looks like this.
function Season_to_date() {
var records = form.getRecords();
var currentRecordIndex = records.indexOf(record);
if (currentRecordIndex > 0) {
var previousRecord = records[currentRecordIndex-1];
} else {
return 0;
// xx = line deleted
xx }
xx // Is this the beginning of the year?
xx var date = record.getFieldValue('fld-0720a5baa6fc4980aee22798bd089714');
xx if (date.getMonth() == 9 && date.getDate() == 1){
xx return 0;
}
var today = record.getFieldValue('xxxxx'); //replaced val with my value from form
var previous_total = previousRecord.getFieldValue('fld-xxxxx'); //replaced val with my value from form
var total = today + previous_total;
console.log("Today: " + today)
console.log("Previous: " + previous_total)
console.log("Total: " + total)
return total;
}
Season_to_date();
-
This topic was modified 3 years, 2 months ago by
Brendan. Reason: Added back ticks around code -- Brendan
Attachments:
You must be
logged in to view attached files.
The experts seem to be busy, so I’ll chime in here.
Your solution will require adding the results in one record with results from the following record to get totals at the bottom of your spreadsheet. I know that can’t be done without lots of javascript programming which is much harder that tracking with a spreadsheet. If I’m wrong, I’m sure somebody will correct me. Look at my postings for examples. They have helped me, but that was a simple solution compared to yours I think. If not you can look at the script they provided for me to add up daily rain totals to get a yearly summation.
A while ago I believe Daniel wrote a script for me to grab a number from a previous record and add it to a number in the current record to provide a running total. I have several forms that use this technique (in another DB program now defunct) for various purposes including medication days between treatments and rain totals. In that program no computer language programming was necessary.
I could not get the script to work and finally 2.5 weeks ago I submitted my Form with data and the script included (“Previous Field calc”)for rain totals in the Script subdivision of the forum, but apparently nobody is interested in looking at it.
I would really like to get this working – I’m used to depending on those forms.
Hope somebody will look at my problem.
Thanks.
Try to define the variables outside of your script:
var value_1;
var value_2;
function Script() {
...
}
Script();
-
This reply was modified 3 years, 2 months ago by
Daniel Leu.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks