Skip to main content
Known Participant
May 13, 2024
Question

Odd Script

  • May 13, 2024
  • 2 replies
  • 358 views

I have the following Script to update a couple of records of mine. The script should fill a new line anytime anytime it finds the first field of a line empty (that means the entire line is empty), but before to do that, the script checks if any of the previous lines containes already the values it should be put at that moment. 

Problem is, apparently, the script jumps the second line. Anyone can help me? 

var shipsName = this.getField("SHIPSNAME").value;
var data      = this.getField("DATAAUTORIZZAZIONE").value;
var port      = this.getField("PORTOFARRIVAL").value;
var flag      = this.getField("FLAGSTATE").value;
var targa     = this.getField("PLATES").value;


if (this.getField("Imposta bollo digitale").display === display.hidden) {
  var bunkerDoc = app.openDoc("G:\\Il mio Drive\\PERSONALE - BRUNO PADULA\\REGISTRO BUNKER.pdf");
  var stop = false;
  for (var i = 1; i <= 294 && !stop; i++) {
    if (bunkerDoc.getField("SHIPSNAME" + i).value === "") {
      if (i === 1) {      
        bunkerDoc.getField("SHIPSNAME1").value = shipsName;
        bunkerDoc.getField("DATA1").value = data;
        bunkerDoc.getField("PORTO1").value = port;
        bunkerDoc.getField("FLAGSTATE1").value = flag;
        bunkerDoc.getField("AUTOBOTTE1").value = targa;
      } else if ( i !== 1) {
        for (var j = i - 1; j >= 1; j--){
          if (bunkerDoc.getField("SHIPSNAME" + j).value !== shipsName && bunkerDoc.getField("DATA" + j).value !== data && bunkerDoc.getField("PORTO" + j).value !== port && bunkerDoc.getField("AUTOBOTTE" + j).value !== targa) {
            bunkerDoc.getField("SHIPSNAME" + i).value = shipsName;
            bunkerDoc.getField("DATA" + i).value = data;
            bunkerDoc.getField("PORTO" + i).value = port;
            bunkerDoc.getField("FLAGSTATE" + i).value = flag;
            bunkerDoc.getField("AUTOBOTTE" + i).value = targa;
            stop = true; // imposta la variabile stop a true
            break; // esce dal ciclo for interno
          } else if (bunkerDoc.getField("SHIPSNAME" + j).value === shipsName && bunkerDoc.getField("DATA" + j).value === data && bunkerDoc.getField("PORTO" + j).value === port && bunkerDoc.getField("AUTOBOTTE" + j).value === targa) {
            stop = true; // imposta la variabile stop a true
            break; // esce dal ciclo for interno
          }
        }
      }
      bunkerDoc.saveAs("G:\\Il mio Drive\\PERSONALE - BRUNO PADULA\\REGISTRO BUNKER.pdf");  
      bunkerDoc.closeDoc();
    }  
  }
} else if (this.getField("Imposta bollo digitale").display === display.visible) {
  var bunker2Doc = app.openDoc("G:\\Il mio Drive\\PERSONALE - BRUNO PADULA\\REGISTRO BUNKER (marche digitali).pdf");
  var stop = false;
  for (var i = 1; i <= 294 && !stop; i++) {
  if (bunker2Doc.getField("SHIPSNAME" + i).value === "") {
      if (i === 1) {      
        bunker2Doc.getField("SHIPSNAME1").value = shipsName;
        bunker2Doc.getField("DATA1").value = data;
        bunker2Doc.getField("PORTO1").value = port;
        bunker2Doc.getField("FLAGSTATE1").value = flag;
        bunker2Doc.getField("AUTOBOTTE1").value = targa;

      } else if ( i !== 1) {
        for (var j = i - 1; j >= 1; j--){
          if (bunker2Doc.getField("SHIPSNAME" + j).value !== shipsName && bunker2Doc.getField("DATA" + j).value !== data && bunker2Doc.getField("PORTO" + j).value !== port && bunker2Doc.getField("AUTOBOTTE" + j).value !== targa) {
            bunker2Doc.getField("SHIPSNAME" + i).value = shipsName;
            bunker2Doc.getField("DATA" + i).value = data;
            bunker2Doc.getField("PORTO" + i).value = port;
            bunker2Doc.getField("FLAGSTATE" + i).value = flag;
            bunker2Doc.getField("AUTOBOTTE" + i).value = targa;
            stop = true; // imposta la variabile stop a true
            break; // esce dal ciclo for interno
          } else if (bunker2Doc.getField("SHIPSNAME" + j).value === shipsName && bunker2Doc.getField("DATA" + j).value === data && bunker2Doc.getField("PORTO" + j).value === port && bunker2Doc.getField("AUTOBOTTE" + j).value === targa) {
            stop = true; // imposta la variabile stop a true
            break; // esce dal ciclo for interno
          }
        }
      }
      bunker2Doc.saveAs("G:\\Il mio Drive\\PERSONALE - BRUNO PADULA\\REGISTRO BUNKER (marche digitali).pdf");  
      bunker2Doc.closeDoc();
    }  
  }
}
This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
May 14, 2024

The script seems fine at a first glance, although it has some redundancies (you don't need the "stop" variable if you use the break command, but that shouldn't be an issue). If you want us to be able to debug it properly we'll need to see the actual file.

Bernd Alheit
Community Expert
Community Expert
May 14, 2024

You must check all previous lines before you save the entries.

Known Participant
May 14, 2024
Hello

Sorry could you be more specific?
My script, at the moment, should be looking for the first empty line, but
it won't fill it in, if any duplicates are present in the previous lines.

If you've found out any syntax or logical errors, would you
pleasehighlight/show them for me?
Bernd Alheit
Community Expert
Community Expert
May 14, 2024
...
for (var j = i - 1; j >= 1; j--){
          if (bunkerDoc.getField("SHIPSNAME" + j).value !== shipsName && bunkerDoc.getField("DATA" + j).value !== data && bunkerDoc.getField("PORTO" + j).value !== port && bunkerDoc.getField("AUTOBOTTE" + j).value !== targa) {
            bunkerDoc.getField("SHIPSNAME" + i).value = shipsName;
...

When the script founds the first non duplicate it fills the empty line.