Bonjour a tous
je cherche si quelqu'un saurait et pourrait me convertir une fonction vba en fonction JS
j'avoue que je m'y perd un peu
voici la fonction vba
et voici la fonction js que j'ai tenté mais en vain même avec chatGpt de reproduire mais qui ne donne pas les même résultats
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 Public Function Decompilation(code1) Dim TbL, i&, R, comm, F, coupe As Boolean TbL = Split(code1, vbCrLf) For i = 0 To UBound(TbL) coupe = False comm = "" If InStr(TbL(i), "'") And Left(Trim(TbL(i)), 1) <> "'" Then comm = commentaire(TbL(i)) If Len(comm) > 0 Then TbL(i) = Split(TbL(i), comm)(0) End If 'TbL(i) = ReplaceUnderscore(TbL(i)) 'remplissage provisoire des lignes vides pour les garder If TbL(i) = "" Then TbL(i) = "'/*/" TbL(i) = Replace(TbL(i), Chr(34) & """ : """ & Chr(34), Chr(34) & """ " & Chr(182) & " """ & Chr(34)) If Left(Trim(TbL(i)), 1) <> "'" Then TbL(i) = ReplaceDP(TbL(i)) R = 1 * Abs(i > 0) F = 0 'dézipage des blocs Select Case True Case _ Left(Trim(TbL(i)), 13) = "Private Type " Or Left(Trim(TbL(i)), 11) = "Pblic Type " Or Left(Trim(TbL(i)), 5) = "Type " Or _ Left(Trim(TbL(i)), 13) = "Private Enum " Or Left(Trim(TbL(i)), 11) = "Pblic Enum " Or Left(Trim(TbL(i)), 5) = "Enum " Or _ Left(Trim(TbL(i - R)), 13) = "Private Type " Or Left(Trim(TbL(i - R)), 11) = "Pblic Type " Or Left(Trim(TbL(i - R)), 5) = "Type " Or _ Left(Trim(TbL(i - R)), 13) = "Private Enum " Or Left(Trim(TbL(i - R)), 11) = "Pblic Enum " Or Left(Trim(TbL(i - R)), 5) = "Enum " Or _ Right(Trim(TbL(i)), 10) = ": End Type" Or _ Right(Trim(TbL(i)), 10) = ": End Enum" coupe = True Case _ Left(Trim(TbL(i)), 5) = "With " Or _ Trim(TbL(i)) Like "*: *End With*" Or _ Trim(TbL(i)) Like "*: *Next*" Or _ Trim(TbL(i)) Like "*: *Wend*" Or _ Left(Trim(TbL(i)), 4) = "For " Or _ Left(Trim(TbL(i)), 5) = "Case " Or _ Left(Trim(TbL(i)), 4) = "Sub " Or _ Left(Trim(TbL(i)), 3) = "Do " Or _ Trim(TbL(i)) Like "*As Long: *" Or _ Trim(TbL(i)) Like "*As LongPtr: *" Or _ Trim(TbL(i)) Like "*As String: *" Or _ Trim(TbL(i)) Like "*As Integer: *" Or _ Trim(TbL(i)) Like "*As Double: *" Or _ Trim(TbL(i)) Like "*As Boolean: *" Or _ Trim(TbL(i)) Like "*As Single: *" Or _ Trim(TbL(i)) Like "*As Variant: *" Or _ Trim(TbL(i)) Like "*&: *" Or _ Trim(TbL(i)) Like "*%: *" Or _ Trim(TbL(i)) Like "*[a-z]" & ": *" Or _ Trim(TbL(i)) Like "*[A-Z]" & ": *" Or _ Trim(TbL(i)) Like "*[0-9]" & ": *" Or _ Left(Trim(TbL(i)), 6) = "While " Or _ Left(Trim(TbL(i)), 3) = "If " coupe = True End Select If Right(Trim(TbL(i)), 2) = " _" Then coupe = False: ' pour les cas ou cette condition serait entre guillemet donc a ne pas prendre en compte If Trim(TbL(i)) Like "*: "" *" Then coupe = False ' pour les cas ou cette condition serait entre guillemet donc a ne pas prendre en compte If Trim(TbL(i)) Like "*"": *""" Then coupe = False 'If TbL(i) Like "*Then*If*Then*Else*" Then coupe = False 'toutes les ligne sauf les commentaires If coupe And Left(Trim(TbL(i)), 1) <> "'" Then 'prétraitrement des ligne If sous toutes sesformes If Left(Trim(TbL(i)), 3) = "If " Then 'décompilation de If consecutifs et fermeture de Bloc If TbL(i) Like "* Then*If*Then *" Then TbL(i) = Replace(TbL(i), " Then", " Then" & vbCrLf) & Application.Rept(vbCrLf & "End If", UBound(Split(TbL(i), "Then "))) If TbL(i) Like "*Else*" Then TbL(i) = Replace(TbL(i), "Else", vbCrLf & "Else" & vbCrLf) Else If TbL(i) Like "*: *" And Not TbL(i) Like "* _*" Then TbL(i) = Replace(Replace(TbL(i), " Then", " Then" & vbCrLf), "Else", vbCrLf & "Else" & vbCrLf): F = 1 End If End If 'remplace les separateur d'instructions par un saut de ligne TbL(i) = Replace(TbL(i), " : ", "||") TbL(i) = Replace(TbL(i), ": ", vbCrLf) TbL(i) = Replace(TbL(i), "||", " : ") If F = 1 Then TbL(i) = TbL(i) & vbCrLf & "End If" End If 'remise en place du mommentaire If coupe Then TbL(i) = comm & vbCrLf & TbL(i) Else TbL(i) = TbL(i) & comm TbL(i) = Replace(TbL(i), Chr(34) & " " & Chr(182) & " " & Chr(34), Chr(34) & " : " & Chr(34)) Next 'regéneration du code vba (Unminifié) code1 = Join(TbL, vbCrLf) code1 = Replace(code1, vbCrLf & vbCrLf, vbCrLf) code1 = Replace(code1, "'/*/", "") '--------------------------------- 'pour le debugage Debug.Print Join(TbL, vbCrLf) 'Application.OnTime Now + 0.000015, "'" & ThisWorkbook.FullName & "'!" & ThisWorkbook.CodeName & ".AddIndentEnvironnement" 'Exit Function 'Arrêt provisoire pour tester le unzip dans la concole '--------------------------------- End Function
pour vous donner un exemple voici ce que j'injecte au deux fonctions
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 function decompilation(code1) { const CHR_182 = '\u00B6'; let TbL = code1.split(/\r?\n/); let output = []; for (let i = 0; i < TbL.length; i++) { let line = TbL[i]; let coupe = false; let comm = ""; let F = 0; // commentaire if (line.includes("'") && line.trim()[0] !== "'") { let temp = commentaire(line); let idx = temp.indexOf("'"); if (idx !== -1) { comm = line.slice(idx); line = line.slice(0, idx); } } if (line === "") line = "'/*/"; line = line.replace(/"" : ""/g, '"" ' + CHR_182 + ' ""'); if (line.trim()[0] !== "'") { line = replaceDP(line); } let R = (i > 0) ? 1 : 0; let prev = (i - R >= 0) ? TbL[i - R].trim() : ""; const begins = s => line.trim().startsWith(s); const prevBegins = s => prev.startsWith(s); const ends = s => line.trim().endsWith(s); const like = (str, pattern) => new RegExp(pattern.replace(/\*/g, ".*")).test(str); if ( begins("Private Type ") || begins("Pblic Type ") || begins("Type ") || begins("Private Enum ") || begins("Pblic Enum ") || begins("Enum ") || prevBegins("Private Type ") || prevBegins("Pblic Type ") || prevBegins("Type ") || prevBegins("Private Enum ") || prevBegins("Pblic Enum ") || prevBegins("Enum ") || ends(": End Type") || ends(": End Enum") ) { coupe = true; } else if ( begins("With ") || like(line.trim(), "*: *End With*") || like(line.trim(), "*: *Next*") || like(line.trim(), "*: *Wend*") || begins("For ") || begins("Case ") || begins("Sub ") || begins("Do ") || like(line.trim(), "*As Long: *") || like(line.trim(), "*As LongPtr: *") || like(line.trim(), "*As String: *") || like(line.trim(), "*As Integer: *") || like(line.trim(), "*As Double: *") || like(line.trim(), "*As Boolean: *") || like(line.trim(), "*As Single: *") || like(line.trim(), "*As Variant: *") || like(line.trim(), "*&: *") || like(line.trim(), "*%: *") || like(line.trim(), "*[a-z]: *") || like(line.trim(), "*[A-Z]: *") || like(line.trim(), "*[0-9]: *") || begins("While ") || begins("If ") ) { coupe = true; } if (line.trim().endsWith(" _")) coupe = false; if (like(line.trim(), "*: \" *")) coupe = false; if (like(line.trim(), "*\": *\"")) coupe = false; if (coupe && line.trim()[0] !== "'") { if (begins("If ")) { if (like(line, "* Then.*If.*Then .*")) { const count = (line.match(/Then /g) || []).length; line = line.replace(/ Then/g, " Then\n") + "\n" + "End If\n".repeat(count).trim(); if (line.includes("Else")) { line = line.replace(/Else/g, "\nElse\n"); } } else if (line.includes(": ") && !line.includes("_")) { line = line.replace(/ Then/g, " Then\n").replace(/Else/g, "\nElse\n"); F = 1; } } line = line.replace(/ : /g, "||").replace(/: /g, "\n").replace(/\|\|/g, " : "); if (F === 1) line += "\nEnd If"; } if (coupe) { line = comm + "\n" + line; } else { line += comm; } line = line.replace(/" ± "/g, '" : "'); TbL[i] = line; } let codeFinal = TbL.join("\n").replace(/\n{2,}/g, "\n"); return codeFinal; }
et voici ce que me renvoie ma fonction vbaSub test()
'3 if consécutifs
If truc = 1 Then If chose = 2 Then If machin = 3 Then chose = 1 'commentaire
'3 if consécutifs avec un else
If truc = """ "" ' _ tutu ' "" """ Then If chose = 2 Then If machin = 3 Then chose = 1: bidule = 45 Else chose = 0 '3 if et 3 then impriqué inline
For I = 1 To 10: tb(I) = I * 10: Next I 'une boucle bloc inline
If toto = " 'tutu _''" Then tata = "titi'": _
tata = 78 ' un commentaire
End Sub
et voici ce que me renvoie la fonction JSSub test()
'3 if consécutifs
'commentaire
If truc = 1 Then
If chose = 2 Then
If machin = 3 Then
chose = 1
End If
End If
End If
'3 if consécutifs avec un else
'3 if et 3 then impriqué inline
If truc = """ "" ± _ tutu ± "" """ Then
If chose = 2 Then
If machin = 3 Then
chose = 1
bidule = 45
Else
chose = 0
End If
End If
End If
'une boucle bloc inline
For I = 1 To 10
tb(I) = I * 10
Next I
If toto = " ±tutu _±±" Then tata = "titi±": underbreak tata = 78 ' un commentaire
End Sub
donc la fonction js ne néttoie pas les ligne préservée par "*/*/'/*/
'3 if consécutifs
If truc = 1 Then
If chose = 2 Then
If machin = 3 Then
chose = 1'commentaire
End If
End If
End If
'/*/
'3 if consécutifs avec un else
If truc = """ "" ± _ tutu ± "" """ Then
If chose = 2 Then
If machin = 3 Then
chose = 1
bidule=45
Else
chose = 0'3 if et 3 then impriqué inline
End If
End If
End If
'/*/
for i= 1 to 10
tb(i)=i*10
Next i 'une boucle bloc inline
'/*/
If toto=" ±tutu _±±" then tata="titi±"
underbreak tata=78 ' un commentaire
'/*/
'/*/
elle ne replasse pas le commentaire au dessus du bloc restructuré
merci d'avance pour les retours eventuels
Partager