将VBA转换为JavaScript自定义功能,得到不同的答案

问题描述 投票:0回答:2

我有以下VBA UDF

qty = 1
numPierces = 5
material = 1
thicknessInches = 0.18
easy1OrHard2 = 1


Function pierceMins(qty As Double, numPierces As Double, material As Double, thicknessInches As Double, easy1OrHard2 As Double)
'Stainless = 1 Steel = 2 Aluminum = 3
Dim procEff As Double
Dim pierceSecs As Double
Dim holesInchPerMin As Double
Dim perimInchPerMin As Double
'***********************************************************************
' EFFICIENCY
'***********************************************************************
    Select Case qty
        Case 0 To 2
            procEff = 0.35 * 0.8
        Case 3 To 4
            procEff = 0.55 * 0.8
        Case 5 To 9
            procEff = 0.7 * 0.8
        Case 10 To 24
            procEff = 0.8 * 0.8
        Case 25 To 49
            procEff = 0.9 * 0.8
        Case Is >= 50
            procEff = 1 * 0.8
        Case Else
    End Select
'***********************************************************************
' STAINLESS
'***********************************************************************
    Select Case material & thicknessInches
        Case 1 & 0
            pierceSecs = 0.1
            holesInchPerMin = 280
            perimInchPerMin = 325
        Case 1 & 0.035, 1 & 0.048
            pierceSecs = 0.2
            holesInchPerMin = 175
            perimInchPerMin = 200
        Case 1 & 0.06
            pierceSecs = 0.3
            holesInchPerMin = 120
            perimInchPerMin = 140
        Case 1 & 0.075, 1 & 0.105
            pierceSecs = 0.75
            holesInchPerMin = 100
            perimInchPerMin = 100
        Case 1 & 0.135
            pierceSecs = 0.3
            holesInchPerMin = 70
            perimInchPerMin = 90
        Case 1 & 0.18, 1 & 0.25
            pierceSecs = 0.3
            holesInchPerMin = 57
            perimInchPerMin = 65
        Case 1 & 0.312
            pierceSecs = 1
            holesInchPerMin = 30
            perimInchPerMin = 35
        Case 1 & 0.375
            pierceSecs = 5
            holesInchPerMin = 25
            perimInchPerMin = 29
        Case 1 & 0.437
            pierceSecs = 4
            holesInchPerMin = 25
            perimInchPerMin = 29
        Case 1 & 0.5
            pierceSecs = 9
            holesInchPerMin = 15
            perimInchPerMin = 13
'***********************************************************************
' STEEL
'***********************************************************************
        Case 2 & 0, 2 & 0.02, 2 & 0.03
            pierceSecs = 0.5
            holesInchPerMin = 157
            perimInchPerMin = 170
        Case 2 & 0.035, 2 & 0.048
            pierceSecs = 1
            holesInchPerMin = 155
            perimInchPerMin = 165
        Case 2 & 0.06
            pierceSecs = 1
            holesInchPerMin = 125
            perimInchPerMin = 140
        Case 2 & 0.075
            pierceSecs = 1.5
            holesInchPerMin = 114
            perimInchPerMin = 125
        Case 2 & 0.086, 2 & 0.105
            pierceSecs = 2
            holesInchPerMin = 80
            perimInchPerMin = 110
        Case 2 & 0.135
            pierceSecs = 5
            holesInchPerMin = 70
            perimInchPerMin = 85
        Case 2 & 0.18
            pierceSecs = 11
            holesInchPerMin = 32
            perimInchPerMin = 65
        Case 2 & 0.25
            pierceSecs = 14
            holesInchPerMin = 35
            perimInchPerMin = 55
        Case 2 & 0.312
            pierceSecs = 14
            holesInchPerMin = 14
            perimInchPerMin = 35
        Case 2 & 0.375
            pierceSecs = 20
            holesInchPerMin = 20
            perimInchPerMin = 40
        Case 2 & 0.437
            pierceSecs = 20
            holesInchPerMin = 20
            perimInchPerMin = 47
        Case 2 & 0.5, 2 & 0.562
            pierceSecs = 18
            holesInchPerMin = 22
            perimInchPerMin = 35
        Case 2 & 0.625
            pierceSecs = 18
            holesInchPerMin = 27
            perimInchPerMin = 35
        Case 2 & 0.75
            pierceSecs = 35
            holesInchPerMin = 32
            perimInchPerMin = 22
'***********************************************************************
' ALUMINUM
'***********************************************************************
        Case 3 & 0
            pierceSecs = 0.1
            holesInchPerMin = 255
            perimInchPerMin = 300
        Case 3 & 0.035, 3 & 0.048
            pierceSecs = 0.2
            holesInchPerMin = 196
            perimInchPerMin = 236
        Case 3 & 0.06
            pierceSecs = 0.4
            holesInchPerMin = 137
            perimInchPerMin = 157
        Case 3 & 0.075, 3 & 0.105
            pierceSecs = 0.5
            holesInchPerMin = 80
            perimInchPerMin = 98
        Case 3 & 0.135
            pierceSecs = 1
            holesInchPerMin = 47
            perimInchPerMin = 60
        Case 3 & 0.18
            pierceSecs = 2.5
            holesInchPerMin = 31
            perimInchPerMin = 39
        Case 3 & 0.25, 3 & 0.312
            pierceSecs = 11
            holesInchPerMin = 20
            perimInchPerMin = 40
        Case 3 & 0.375
            pierceSecs = 12
            holesInchPerMin = 15
            perimInchPerMin = 25
        Case Else
    End Select
    If easy1OrHard2 = 1 Then
        pierceMins = ((pierceSecs) / procEff * numPierces) / 60
    Else
        pierceMins = ((pierceSecs) / procEff * numPierces) / 60 * (1.2) '20% is added for Intricate Cutting Cycle Time Added
    End If
End Function

我将此代码翻译为JavaScript,但得到了不同的答案。

/**
 * pierceMins
 * @customfunction pierceMins
 * @param {number} qty
 * @param {number} numPierces
 * @param {number} material 
 * @param {number} thicknessInches
 * @param {number} easy1OrHard2
 * @returns {number} pierceMins
 */
function pierceMins(qty, numPierces, material, thicknessInches, easy1OrHard2) {
  var pierceSecs;
  var holesInchPerMin;
  var perimInchPerMin;
  var procEff;
  switch(qty) {
      case 0: case 1: case 2:
          procEff = 0.35 * 0.8;
          break;
      case 3: case 4:
          procEff = 0.55 * 0.8;
          break;
      case 5: case 6: case 7: case 8: case 9:
          procEff = 0.7 * 0.8;
          break;
      case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24:
          procEff = 0.8 * 0.8;
          break;
      case 25: case 26: case 27: case 28: case 29: case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39:
      case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49:
          procEff = 0.9 * 0.8;
          break;
      default:
          procEff = 1 * 0.8;
  }
  switch("" + material + thicknessInches) {
  //Stainless
      case "" + 1 + 0:
          pierceSecs = 0.1;
          holesInchPerMin = 280;
          perimInchPerMin = 325;
          break;
      case "" + 1 + 0.035: case "" + 1 + 0.048:
          pierceSecs = 0.2;
          holesInchPerMin = 175;
          perimInchPerMin = 200;
          break;
      case "" + 1 + 0.06:
          pierceSecs = 0.3;
          holesInchPerMin = 120;
          perimInchPerMin = 140;
          break;
      case "" + 1 + 0.075: case "" + 1 + 0.105:
          pierceSecs = 0.75;
          holesInchPerMin = 100;
          perimInchPerMin = 100;
          break;
      case "" + 1 + 0.135:
          pierceSecs = 0.3;
          holesInchPerMin = 70;
          perimInchPerMin = 90;
          break;
      case "" + 1 + 0.18: case "" + 1 + 0.25:
          pierceSecs = 0.3;
          holesInchPerMin = 57;
          perimInchPerMin = 65;
          break;
      case "" + 1 + 0.312:
          pierceSecs = 1;
          holesInchPerMin = 30;
          perimInchPerMin = 35;
          break;
      case "" + 1 + 0.375:
          pierceSecs = 5;
          holesInchPerMin = 25;
          perimInchPerMin = 29;
          break;
      case "" + 1 + 0.437:
          pierceSecs = 4;
          holesInchPerMin = 25;
          perimInchPerMin = 29;
          break;
      case "" + 1 + 0.5:
          pierceSecs = 9;
          holesInchPerMin = 15;
          perimInchPerMin = 13;
          break;
 // Steel
      case "" + 2 + 0: case "" + 2 + 0.02: case "" + 2 + 0.03:
          pierceSecs = 0.5;
          holesInchPerMin = 157;
          perimInchPerMin = 170;
          break;
      case "" + 2 + 0.035: case "" + 2 + 0.048:
          pierceSecs = 1;
          holesInchPerMin = 155;
          perimInchPerMin = 165;
          break;
      case "" + 2 + 0.06:
          pierceSecs = 1;
          holesInchPerMin = 125;
          perimInchPerMin = 140;
          break;
      case "" + 2 + 0.075:
          pierceSecs = 1.5;
          holesInchPerMin = 114;
          perimInchPerMin = 125;
          break;
      case "" + 2 + 0.086: case "" + 2 + 0.105:
          pierceSecs = 2;
          holesInchPerMin = 80;
          perimInchPerMin = 110;
          break;
      case "" + 2 + 0.135:
          pierceSecs = 5;
          holesInchPerMin = 70;
          perimInchPerMin = 85;
          break;
      case "" + 2 + 0.18:
          pierceSecs = 11;
          holesInchPerMin = 32;
          perimInchPerMin = 65;
          break;
      case "" + 2 + 0.25:
          pierceSecs = 14;
          holesInchPerMin = 35;
          perimInchPerMin = 55;
          break;
      case "" + 2 + 0.312:
          pierceSecs = 14;
          holesInchPerMin = 14;
          perimInchPerMin = 35;
          break;
      case "" + 2 + 0.375:
          pierceSecs = 20;
          holesInchPerMin = 20;
          perimInchPerMin = 40;
          break;
      case "" + 2 + 0.437:
          pierceSecs = 20;
          holesInchPerMin = 20;
          perimInchPerMin = 47;
          break;
      case "" + 2 + 0.5: case "" + 2 + 0.562:
          pierceSecs = 18;
          holesInchPerMin = 22;
          perimInchPerMin = 35;
          break;
      case "" + 2 + 0.625:
          pierceSecs = 18;
          holesInchPerMin = 27;
          perimInchPerMin = 35;
          break;
      case "" + 2 + 0.75:
          pierceSecs = 35;
          holesInchPerMin = 32;
          perimInchPerMin = 22;
          break;
 //Aluminum
      case "" + 3 + 0:
          pierceSecs = 0.1;
          holesInchPerMin = 255;
          perimInchPerMin = 300;
          break;
      case "" + 3 + 0.035: case "" + 3 + 0.048:
          pierceSecs = 0.2;
          holesInchPerMin = 196;
          perimInchPerMin = 236;
          break;
      case "" + 3 + 0.06:
          pierceSecs = 0.4;
          holesInchPerMin = 137;
          perimInchPerMin = 157;
          break;
      case "" + 3 + 0.075: case "" + 3 + 0.105:
          pierceSecs = 0.5;
          holesInchPerMin = 80;
          perimInchPerMin = 98;
          break;
      case "" + 3 + 0.135:
          pierceSecs = 1;
          holesInchPerMin = 47;
          perimInchPerMin = 60;
          break;
      case "" + 3 + 0.18:
          pierceSecs = 2.5;
          holesInchPerMin = 31;
          perimInchPerMin = 39;
          break;
      case "" + 3 + 0.25: case "" + 3 + 0.312:
          pierceSecs = 11;
          holesInchPerMin = 20;
          perimInchPerMin = 40;
          break;
      case "" + 3 + 0.375:
          pierceSecs = 12;
          holesInchPerMin = 15;
          perimInchPerMin = 25;
          break;
      default:
      //  alert("Does not work");
  }
  if (easy1OrHard2 == 1) {
      return ((pierceSecs) / procEff * numPierces) / 60; 
  } else {
      return ((pierceSecs) / procEff * numPierces) / 60 * (1.2);
  }
}

在VBA中,我得到0.09作为答案,这是正确的。我认为我的交换机外壳无法正常工作。如果更改JavaScript中的thicknessInches,答案将保持不变。另外,如果有人对如何缩短代码有任何建议,请告诉我!

javascript switch-statement user-defined-functions
2个回答
2
投票

[在VBA中,您使用的是&,Microsoft文档告诉我这是字符串连接。在JavaScript中,您使用的是|,即按位或。要连接数字值,首先需要将它们转换为字符串,然后将其与+组合。做到这一点的规范方法是将您的数字添加到字符串中,因此我建议使用case "" + 1 + 0:作为您对Case 1 & 0的翻译。

© www.soinside.com 2019 - 2024. All rights reserved.