Live UDF formulas in template, remove gApiEnabled guard
- GL_Accounts_Client_Template.xlsx: C6:C13 now have real formulas =GetAccountAmount(Setup!$B$6,...) instead of text placeholders - GLAccountsExcelApi.bas: removed gApiEnabled flag entirely, UDFs call RunQuery directly; add SetManualCalculation helper macro - Works in Excel safe mode + manual calc: F9 triggers DB queries Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,17 +6,14 @@ Option Explicit
|
||||
' Setup:
|
||||
' 1. Install PostgreSQL ODBC driver.
|
||||
' 2. Apply notes/accounting/excel_api/gl_accounts_sql_poc.sql.
|
||||
' 3. In Excel: Developer > Visual Basic > File > Import File...
|
||||
' 4. Import this .bas module.
|
||||
' 5. Edit GL_API_CONNECTION_STRING below (or use a named DSN).
|
||||
' 3. Open Excel in safe mode: double-clic sur Ouvrir_Excel_SafeMode.bat
|
||||
' 4. Developer > Visual Basic > File > Import File > importe ce .bas.
|
||||
' 5. Run SetManualCalculation once (sets xlCalculationManual).
|
||||
'
|
||||
' Usage:
|
||||
' - On the Setup sheet, fill in B6 (company), B7 (account), B8 (currency), B9 (date).
|
||||
' - Run RefreshGLApi to query the database and populate the Functions sheet.
|
||||
' - Excel stays in manual calculation mode after refresh; edit Setup freely.
|
||||
'
|
||||
' DSN example:
|
||||
' "DSN=TradonGLApi;Uid=tradon_readonly;Pwd=change_me;"
|
||||
' Usage (2 modes au choix):
|
||||
' Mode formule : tape =GetAccountAmount("ICT TRADING","20011","USD","2026-05-13")
|
||||
' dans n'importe quelle cellule, puis F9 pour recalculer.
|
||||
' Mode macro : remplis Setup!B6:B9, lance RefreshGLApi.
|
||||
|
||||
Private Const GL_API_CONNECTION_STRING As String = _
|
||||
"Driver={PostgreSQL Unicode(x64)};" & _
|
||||
@@ -28,7 +25,6 @@ Private Const GL_API_CONNECTION_STRING As String = _
|
||||
|
||||
Private gConnection As Object
|
||||
Private gCache As Object
|
||||
Private gApiEnabled As Boolean
|
||||
|
||||
' ---- Connection helpers ----
|
||||
|
||||
@@ -72,11 +68,18 @@ Public Sub CloseGLApiConnection()
|
||||
Set gConnection = Nothing
|
||||
End Sub
|
||||
|
||||
' ---- Main macro ----
|
||||
' ---- Setup ----
|
||||
|
||||
' Reads Setup!B6:B9, queries the database directly, writes plain values to Functions!C6:C13.
|
||||
' Excel stays in xlCalculationManual after this runs -- edit Setup cells freely,
|
||||
' then run RefreshGLApi again when you want fresh results.
|
||||
' A lancer une seule fois apres import du module.
|
||||
' Passe Excel en calcul manuel : les formules ne se recalculent que sur F9.
|
||||
Public Sub SetManualCalculation()
|
||||
Application.Calculation = xlCalculationManual
|
||||
MsgBox "Calcul manuel actif. F9 = recalculer toutes les formules GL.", vbInformation
|
||||
End Sub
|
||||
|
||||
' ---- Mode macro : RefreshGLApi ----
|
||||
|
||||
' Lit Setup!B6:B9, interroge la DB, ecrit les valeurs dans Functions!C6:C13.
|
||||
Public Sub RefreshGLApi()
|
||||
Dim wsSetup As Worksheet
|
||||
Dim wsFunctions As Worksheet
|
||||
@@ -87,11 +90,11 @@ Public Sub RefreshGLApi()
|
||||
On Error GoTo 0
|
||||
|
||||
If wsSetup Is Nothing Then
|
||||
MsgBox "Sheet 'Setup' not found.", vbExclamation
|
||||
MsgBox "Feuille 'Setup' introuvable.", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
If wsFunctions Is Nothing Then
|
||||
MsgBox "Sheet 'Functions' not found.", vbExclamation
|
||||
MsgBox "Feuille 'Functions' introuvable.", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
@@ -101,26 +104,26 @@ Public Sub RefreshGLApi()
|
||||
|
||||
Dim company As String
|
||||
Dim account As String
|
||||
Dim currency As String
|
||||
Dim currencyIso As String
|
||||
Dim valueDate As Variant
|
||||
|
||||
company = CStr(wsSetup.Range("B6").Value)
|
||||
account = CStr(wsSetup.Range("B7").Value)
|
||||
currency = CStr(wsSetup.Range("B8").Value)
|
||||
currencyIso = CStr(wsSetup.Range("B8").Value)
|
||||
valueDate = wsSetup.Range("B9").Value
|
||||
|
||||
ClearGLApiCache
|
||||
|
||||
On Error GoTo RefreshError
|
||||
|
||||
wsFunctions.Range("C6").Value = RunQuery("get_account_base_amount", company, account, currency, valueDate)
|
||||
wsFunctions.Range("C7").Value = RunQuery("get_account_real_base_amount", company, account, currency, valueDate)
|
||||
wsFunctions.Range("C8").Value = RunQuery("get_account_amount", company, account, currency, valueDate)
|
||||
wsFunctions.Range("C9").Value = RunQuery("get_account_abbr", company, account, currency, valueDate)
|
||||
wsFunctions.Range("C10").Value = RunQuery("get_account_name", company, account, currency, valueDate)
|
||||
wsFunctions.Range("C11").Value = RunQuery("get_account_contact", company, account, currency, valueDate)
|
||||
wsFunctions.Range("C12").Value = RunQuery("get_account_contact_code", company, account, currency, valueDate)
|
||||
wsFunctions.Range("C13").Value = RunQuery("get_account_contact_name", company, account, currency, valueDate)
|
||||
wsFunctions.Range("C6").Value = RunQuery("get_account_base_amount", company, account, currencyIso, valueDate)
|
||||
wsFunctions.Range("C7").Value = RunQuery("get_account_real_base_amount", company, account, currencyIso, valueDate)
|
||||
wsFunctions.Range("C8").Value = RunQuery("get_account_amount", company, account, currencyIso, valueDate)
|
||||
wsFunctions.Range("C9").Value = RunQuery("get_account_abbr", company, account, currencyIso, valueDate)
|
||||
wsFunctions.Range("C10").Value = RunQuery("get_account_name", company, account, currencyIso, valueDate)
|
||||
wsFunctions.Range("C11").Value = RunQuery("get_account_contact", company, account, currencyIso, valueDate)
|
||||
wsFunctions.Range("C12").Value = RunQuery("get_account_contact_code", company, account, currencyIso, valueDate)
|
||||
wsFunctions.Range("C13").Value = RunQuery("get_account_contact_name", company, account, currencyIso, valueDate)
|
||||
|
||||
CloseGLApiConnection
|
||||
GoTo RefreshDone
|
||||
@@ -131,23 +134,6 @@ RefreshError:
|
||||
RefreshDone:
|
||||
Application.ScreenUpdating = True
|
||||
Application.EnableEvents = True
|
||||
' Intentionally keep xlCalculationManual -- prevents any live recalculation.
|
||||
End Sub
|
||||
|
||||
' Resets Functions!C6:C13 to placeholder text.
|
||||
Public Sub ClearGLApiResults()
|
||||
Dim wsFunctions As Worksheet
|
||||
On Error Resume Next
|
||||
Set wsFunctions = ThisWorkbook.Worksheets("Functions")
|
||||
On Error GoTo 0
|
||||
If wsFunctions Is Nothing Then Exit Sub
|
||||
|
||||
Application.EnableEvents = False
|
||||
Dim r As Long
|
||||
For r = 6 To 13
|
||||
wsFunctions.Cells(r, 3).Value = "-"
|
||||
Next r
|
||||
Application.EnableEvents = True
|
||||
End Sub
|
||||
|
||||
' ---- Core SQL runner ----
|
||||
@@ -213,29 +199,15 @@ Private Function SqlText(ByVal value As Variant) As String
|
||||
SqlText = Replace(CStr(value), "'", "''")
|
||||
End Function
|
||||
|
||||
' ---- UDF functions (optional, for manual formula use) ----
|
||||
'
|
||||
' These functions are NOT called by RefreshGLApi.
|
||||
' They are available if you want to type a live formula directly in a cell,
|
||||
' but you must first run EnableGLApi and keep Excel in manual calculation mode.
|
||||
' Warning: live UDF formulas in cells can freeze Excel on recalculation.
|
||||
|
||||
Public Sub EnableGLApi()
|
||||
gApiEnabled = True
|
||||
Application.Calculation = xlCalculationManual
|
||||
End Sub
|
||||
|
||||
Public Sub DisableGLApi()
|
||||
gApiEnabled = False
|
||||
CloseGLApiConnection
|
||||
End Sub
|
||||
' ---- UDF formulas (mode formule) ----
|
||||
' Utilisation : =GetAccountAmount("ICT TRADING", B7, B8, B9)
|
||||
' Puis F9 pour recalculer. Le cache evite les appels DB redondants.
|
||||
|
||||
Public Function GetAccountBaseAmount( _
|
||||
ByVal companyKey As Variant, _
|
||||
ByVal accountCode As Variant, _
|
||||
ByVal currencyIso As Variant, _
|
||||
ByVal valueDate As Variant) As Variant
|
||||
If Not gApiEnabled Then GetAccountBaseAmount = "#GLAPI_OFF": Exit Function
|
||||
GetAccountBaseAmount = RunQuery("get_account_base_amount", companyKey, accountCode, currencyIso, valueDate)
|
||||
End Function
|
||||
|
||||
@@ -244,7 +216,6 @@ Public Function GetAccountRealBaseAmount( _
|
||||
ByVal accountCode As Variant, _
|
||||
ByVal currencyIso As Variant, _
|
||||
ByVal valueDate As Variant) As Variant
|
||||
If Not gApiEnabled Then GetAccountRealBaseAmount = "#GLAPI_OFF": Exit Function
|
||||
GetAccountRealBaseAmount = RunQuery("get_account_real_base_amount", companyKey, accountCode, currencyIso, valueDate)
|
||||
End Function
|
||||
|
||||
@@ -253,7 +224,6 @@ Public Function GetAccountAmount( _
|
||||
ByVal accountCode As Variant, _
|
||||
ByVal currencyIso As Variant, _
|
||||
ByVal valueDate As Variant) As Variant
|
||||
If Not gApiEnabled Then GetAccountAmount = "#GLAPI_OFF": Exit Function
|
||||
GetAccountAmount = RunQuery("get_account_amount", companyKey, accountCode, currencyIso, valueDate)
|
||||
End Function
|
||||
|
||||
@@ -262,7 +232,6 @@ Public Function GetAccountAbbr( _
|
||||
ByVal accountCode As Variant, _
|
||||
ByVal currencyIso As Variant, _
|
||||
ByVal valueDate As Variant) As Variant
|
||||
If Not gApiEnabled Then GetAccountAbbr = "#GLAPI_OFF": Exit Function
|
||||
GetAccountAbbr = RunQuery("get_account_abbr", companyKey, accountCode, currencyIso, valueDate)
|
||||
End Function
|
||||
|
||||
@@ -271,7 +240,6 @@ Public Function GetAccountName( _
|
||||
ByVal accountCode As Variant, _
|
||||
ByVal currencyIso As Variant, _
|
||||
ByVal valueDate As Variant) As Variant
|
||||
If Not gApiEnabled Then GetAccountName = "#GLAPI_OFF": Exit Function
|
||||
GetAccountName = RunQuery("get_account_name", companyKey, accountCode, currencyIso, valueDate)
|
||||
End Function
|
||||
|
||||
@@ -280,7 +248,6 @@ Public Function GetAccountContact( _
|
||||
ByVal accountCode As Variant, _
|
||||
ByVal currencyIso As Variant, _
|
||||
ByVal valueDate As Variant) As Variant
|
||||
If Not gApiEnabled Then GetAccountContact = "#GLAPI_OFF": Exit Function
|
||||
GetAccountContact = RunQuery("get_account_contact", companyKey, accountCode, currencyIso, valueDate)
|
||||
End Function
|
||||
|
||||
@@ -289,7 +256,6 @@ Public Function GetAccountContactCode( _
|
||||
ByVal accountCode As Variant, _
|
||||
ByVal currencyIso As Variant, _
|
||||
ByVal valueDate As Variant) As Variant
|
||||
If Not gApiEnabled Then GetAccountContactCode = "#GLAPI_OFF": Exit Function
|
||||
GetAccountContactCode = RunQuery("get_account_contact_code", companyKey, accountCode, currencyIso, valueDate)
|
||||
End Function
|
||||
|
||||
@@ -298,6 +264,5 @@ Public Function GetAccountContactName( _
|
||||
ByVal accountCode As Variant, _
|
||||
ByVal currencyIso As Variant, _
|
||||
ByVal valueDate As Variant) As Variant
|
||||
If Not gApiEnabled Then GetAccountContactName = "#GLAPI_OFF": Exit Function
|
||||
GetAccountContactName = RunQuery("get_account_contact_name", companyKey, accountCode, currencyIso, valueDate)
|
||||
End Function
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user