Skip to main content
Participant
November 20, 2022
Answered

Bedingung bei Formular einfügen

  • November 20, 2022
  • 2 replies
  • 1588 views

Liebe Community,

da ich bei meinem letzten Problem innerhalb kurzer Zeit fantastische Hilfe erhalten habe, probiere ich es heute noch einmal und kreuze die Finger, dass es wieder funktioniert:

Ich bin Lehrer und möchte über selbsterstelle Formulare Auswertungsbögen erstellen, in denen ich für gewöhnlich ankreuze, was in einer abgegebenen Arbeit enthalten war und das Formular zählt die Punkte zusammen.

Dieses Mal habe ich einen Fall, den ich mit meinen nicht vorhandenen Code-Kenntnissen nicht lösen kann:

Es wurden 4 Fachvokabeltests geschrieben, von denen mind. drei abgegeben werden mussten. Wenn ein Testmitgeschrieben wurde und ich das mit einem Häkchen markiere, trage ich die erreichte Punktezahl im Test ein. Das tue ich dann für mind. 3 Tests. Die Gesamtzahl der zu Pkt. lässt sich leicht addieren. Aber wie kann das Formular nun im Feld PktSummeMAX die mögliche zu erreichende Punktzahl eintragen, in abhängigkeit von den mitgeschriebenen Tests? Denn ich möchte automatisiert die erreichten Prozente berechnen lassen, um die Note festlegenlegen zu können. Mein Versuch, dem Checkfeld die entsprechende Punktezahl des Tests als Ausgabewert zuzuordnen hat für Test 1 funktioniert, aber ich bekomme das Addieren der Gesamtpunktzahlen aller aktivierter Tests nicht hin.

 

Hätte noch jemand einen Tipp, wie ich die Syntax lernen könnte? Das ist doch kein reines JavaScript, sondern eine Modifikation davon, oder? Nicht, dass ich Java könnte ... aber wenn ich es schon lernen will, dann schon das Richtige für die PDF-Formulare 🙂

This topic has been closed for replies.
Correct answer Robert Kohlmann

After a long time of trial and error and with the help of a friend I found the solution for the problem. If someone needs it, here is the Code:

//calculate the sum of the maximal points of only the activated tests
var summax = 0;
for (var i = 1; i < 5; ++i) {
  if(this.getField("Teilnahme"+i).valueAsString != "Off"){
    summax = summax + this.getField("pktmax" + i).value;
  }
}
event.value = summax;

 

 

2 replies

Robert KohlmannAuthorCorrect answer
Participant
December 6, 2022

After a long time of trial and error and with the help of a friend I found the solution for the problem. If someone needs it, here is the Code:

//calculate the sum of the maximal points of only the activated tests
var summax = 0;
for (var i = 1; i < 5; ++i) {
  if(this.getField("Teilnahme"+i).valueAsString != "Off"){
    summax = summax + this.getField("pktmax" + i).value;
  }
}
event.value = summax;

 

 

Nesa Nurani
Community Expert
Community Expert
December 6, 2022

You could use one script for both "PktSUMME" and "PktSummeMAX" fields it will also prevent to add maxsum if checkbox is checked and pkt is empty, you can use like this as custom calculation script of "PktSUMME" field:

var summax = 0;
var sumpkt = 0;
for (var i = 1; i <=4; i++) {
if(this.getField("Teilnahme"+i).valueAsString != "Off" && this.getField("Pkt" + i).valueAsString != ""){
summax += Number(this.getField("pktmax" + i).valueAsString);
sumpkt += Number(this.getField("Pkt" + i).valueAsString);}}
event.value = sumpkt;
this.getField("PktSummeMAX").value = summax;

 

Also, to not get 'NaN' and 'Infinity' errors in percentage field use this:

var pktsum = Number(this.getField("PktSUMME").valueAsString);
var pktmax = Number(this.getField("PktSummeMAX").valueAsString);
var percentage = 0;
if(pktsum&&pktmax)
percentage = Math.round((pktsum / pktmax) * 10000) / 100;
event.value = percentage;

 

Here is file with new scripts:

https://drive.google.com/file/d/1DRG4gJMuBQafqEEeLUoTgLKlB-nWXdIM/view?usp=share_link 

 

Nesa Nurani
Community Expert
Community Expert
November 20, 2022

There are no fields in that file.

If I understand you right, you need to check that field is not empty before adding that field value and total score when calculating percentage.

Participant
November 21, 2022

sorry, I uploaded the wrong file.

To explain it in english:

I wrote 4 vocab-tests and the students had to write at least three of them. Now I want to activate, which test the specific student made e.g. "Teilnahme1" and enter the points of the students, he reached in field "Pkt1".

In the field "PktSUMME" I already make the sum of "Pkt1+Pkt2+Pkt3+Pkt4".

My first problem is, that I need the Points of the three or four actived Tests, which automatically should be in the field "PktSummeMAX" (Points Sum Maximum). It's also possible, that a student wrote Test 2,3,4 or 1,2,4 or 1, 2, 3, 4 and so on.

My second problem is, how to calculate the percentage with Javascript &colon;/