Search code examples
javascriptexcelacrobatvba

How to rewrite a excel-vba to acrobat javascript?


this is a code from a excel macro (VB), how can i rewrite it, so it works on adobe acrobat?

for e.g. nummer is "161628686041430"

Function upsp(nummer)
' Übergeben wird KdNr+Serviceart+Paketnummer (ohne 1Z)
'
qsm = 0
For i = 1 To 15
p = Mid(nummer, i, 1)
If Asc(p) > 57 Then p = (Asc(p) - 63) Mod 10
qsm = qsm + (p * (2 - i Mod 2))
Next
upsp = 10 - (qsm Mod 10)
If upsp = 10 Then upsp = 0
End Function

the result of this function should be "2".

Thanks for help.


Solution

  • I think this is what you're looking for, I tried to keep as close as possible to the Excel code, hoping it would be easier to follow. Let me know if you have any questions...

    alert(upsp('161628686041430'));
    
    function upsp(nummer) {
      var qsm;
      var p;
      var returnValue;
    
      qsm = 0;
      for (var i = 0; i < nummer.length; i++) {
        p = nummer.substr(i, 1);
        if (p.charCodeAt(0) > 57) {
          p = (p.charCodeAt(0) - 63) % 10;
        }
        qsm = qsm + (p * (2 - ((i + 1) % 2)));
      }
    
      returnValue = 10 - (qsm % 10);
      if (returnValue === 10) {
        returnValue = 0;
      }
    
      return returnValue;
    }