190 - Suppression protection VBA

This commit is contained in:
2025-09-11 10:09:46 +02:00
parent 21560f9e7b
commit 49b85a51c6
7 changed files with 149 additions and 145 deletions

Binary file not shown.

View File

@@ -1 +1 @@
1.8.2 1.9.0

92
Scripts/Maj_prod.bat Normal file
View File

@@ -0,0 +1,92 @@
@echo off
chcp 1252 >nul
setlocal EnableExtensions
REM ===================== PARAMS =====================
set "ROOT=C:\Users\miche\PycharmProjects\Ratio_Inventaires"
set "DEV_XLSM=%ROOT%\Excel\dev\Ratio_dev.xlsm"
set "PROD_DIR=%ROOT%\Excel\prod"
set "PROD_XLSM=%PROD_DIR%\Ratio_prod.xlsm"
set "VERSION_TXT=%ROOT%\Excel\dev\VERSION.txt"
set "SHEET_NAME=Tableau de bord"
set "CELL_ADDR=P1"
echo.
echo === [1/4] Prep ===============================================
if not exist "%PROD_DIR%" md "%PROD_DIR%"
if not exist "%DEV_XLSM%" goto ERR_NO_DEV
if not exist "%VERSION_TXT%" (
>"%VERSION_TXT%" echo 1.0.0
echo [INFO] VERSION.txt created with 1.0.0
)
for /f %%i in ('powershell -NoProfile -Command "(Get-Date).ToString('yyyy_MM_dd')"') do set "DATESTAMP=%%i"
set "ARCHIVE_FILE=%PROD_DIR%\Ratio_prod_%DATESTAMP%.xlsm"
echo.
echo === [2/4] Lire VERSION.txt et maj cellule =====================
for /f "usebackq delims=" %%v in ("%VERSION_TXT%") do set "NEW_VER=%%v"
set "NEW_VER=%NEW_VER: =%"
if "%NEW_VER%"=="" goto ERR_EMPTY_VER
echo Version: %NEW_VER%
REM Mise a jour de P1 dans DEV via VBS (macros & evenements OFF)
cscript //nologo "%ROOT%\Scripts\set_cell_silent.vbs" "%DEV_XLSM%" "%SHEET_NAME%" "%CELL_ADDR%" "%NEW_VER%"
if errorlevel 1 goto P1_FAIL
echo [OK] P1 updated on DEV: %NEW_VER%
goto AFTER_P1
:P1_FAIL
echo [WARN] P1 update failed on DEV (continuing).
:AFTER_P1
echo.
echo === [3/4] Backup PROD et purge archives ======================
if not exist "%PROD_XLSM%" goto NO_BACKUP
echo Backup -> "%ARCHIVE_FILE%"
copy /Y "%PROD_XLSM%" "%ARCHIVE_FILE%" >nul
goto AFTER_BACKUP
:NO_BACKUP
echo Pas de PROD a sauvegarder.
:AFTER_BACKUP
REM Garder uniquement les 5 dernieres archives
powershell -NoProfile -Command "Get-ChildItem -Path '%PROD_DIR%' -Filter 'Ratio_prod_20*_??_??.xlsm' | Sort-Object LastWriteTime -Descending | Select-Object -Skip 5 | Remove-Item -Force -ErrorAction SilentlyContinue"
echo.
echo === [4/4] Copier DEV -> PROD =================================
copy /Y "%DEV_XLSM%" "%PROD_XLSM%"
if errorlevel 1 goto ERR_COPY
>"%PROD_DIR%\VERSION.txt" echo %NEW_VER%
echo.
echo [OK] Termine :
echo Version : %NEW_VER%
echo PROD : "%PROD_XLSM%"
echo VERSION : "%PROD_DIR%\VERSION.txt"
echo.
endlocal & exit /b 0
REM ===================== ERRORS =====================
:ERR_NO_DEV
echo [ERROR] DEV file not found: "%DEV_XLSM%"
endlocal & exit /b 1
:ERR_EMPTY_VER
echo [ERROR] VERSION.txt is empty. Put a version like 1.8.3 and rerun.
endlocal & exit /b 1
:ERR_COPY
echo [ERROR] Copy DEV -> PROD failed.
endlocal & exit /b 1

1
Scripts/PROD Normal file
View File

@@ -0,0 +1 @@
=== [4/4] Copier DEV - =================================

View File

@@ -1,124 +0,0 @@
@echo off
setlocal EnableExtensions DisableDelayedExpansion
REM === PARAMETRES =============================================================
set "ROOT=C:\Users\miche\PycharmProjects\Ratio_Inventaires"
set "DEV_XLSM=%ROOT%\Excel\dev\Ratio_dev.xlsm"
set "PROD_DIR=%ROOT%\Excel\prod"
set "PROD_XLSM=%PROD_DIR%\Ratio_prod.xlsm"
set "INSTALL_CLIENT=%ROOT%\Fichiers_Install_Clients"
set "VERSION_TXT=%ROOT%\Excel\dev\VERSION.txt"
set "SHEET_NAME=Tableau de bord"
set "CELL_ADDR=P1"
set "SETCELL_VBS=C:\Users\miche\PycharmProjects\Ratio_Inventaires\Scripts\set_cell_silent.vbs"
REM ============================================================================
REM Date ISO (yyyy_MM_dd)
REM ============================================================================
for /f %%i in ('powershell -NoProfile -Command "(Get-Date).ToString(\"yyyy_MM_dd\")"') do set "DATESTAMP=%%i"
set "ARCHIVE_FILE=%PROD_DIR%\Ratio_prod_%DATESTAMP%.xlsm"
echo(
echo( === [1/4] Préparation des dossiers =======================================
if not exist "%PROD_DIR%" mkdir "%PROD_DIR%"
if not exist "%INSTALL_CLIENT%" mkdir "%INSTALL_CLIENT%"
if not exist "%DEV_XLSM%" (
echo( [ERREUR] Fichier DEV introuvable: %DEV_XLSM%
endlocal & exit /b 1
)
if not exist "%VERSION_TXT%" (
echo 1.0.0 > "%VERSION_TXT%"
echo( [INFO] VERSION.txt manquant -> initialisé à 1.0.0
)
REM ---------------------------------------------------------------------------
REM [2/4] Synchroniser P1 (Tableau de bord) = VERSION.txt, sans macros/événements
REM ---------------------------------------------------------------------------
echo(
echo( === [2/4] Mise a jour P1 depuis VERSION.txt [macros/evenements off] =====
set "NEW_VER="
set /p NEW_VER=<"%VERSION_TXT%"
set "NEW_VER=%NEW_VER: =%"
if "%NEW_VER%"=="" (
echo( [ERREUR] VERSION.txt est vide. Mets une valeur ex: 2.1.3 et relance.
endlocal & exit /b 1
)
echo( Version a ecrire en P1 : %NEW_VER%
if not exist "%SETCELL_VBS%" (
echo( [WARN] VBS absent: "%SETCELL_VBS%". P1 ne sera pas mis a jour, on continue.
) else (
cscript //nologo "%SETCELL_VBS%" "%DEV_XLSM%" "%SHEET_NAME%" "%CELL_ADDR%" "%NEW_VER%"
if errorlevel 1 (
echo( [WARN] Echec mise a jour P1. On continue.
) else (
echo( [OK] P1 mis a jour dans DEV : %NEW_VER%
)
)
REM ---------------------------------------------------------------------------
REM [3/4] Sauvegarde de la PROD courante
REM ---------------------------------------------------------------------------
echo(
echo( === [3/4] Sauvegarde de la PROD courante =================================
if exist "%PROD_XLSM%" (
echo( Sauvegarde -> "%ARCHIVE_FILE%"
copy /Y "%PROD_XLSM%" "%ARCHIVE_FILE%" >nul
) else (
echo( Pas de PROD courante à sauvegarder.
)
REM ---------------------------------------------------------------------------
REM [4/4] Copie DEV -> PROD et Dossier client + nettoyage archives
REM ---------------------------------------------------------------------------
echo(
echo( === [4/4] Copie DEV -^> PROD ^& Dossier client =============================
copy /Y "%DEV_XLSM%" "%PROD_XLSM%" >nul || (
echo( [ERREUR] Copie DEV -> PROD echouee.
endlocal & exit /b 1
)
REM --- Livraison client depuis la PROD (sans ajout de protection) ---
copy /Y "%PROD_XLSM%" "%INSTALL_CLIENT%\Ratio_prod.xlsm" >nul || (
echo( [ERREUR] Copie vers INSTALL_CLIENT echouee.
endlocal & exit /b 1
)
REM ecrire la version cote PROD pour verification rapide
echo %NEW_VER% > "%PROD_DIR%\VERSION.txt"
pushd "%PROD_DIR%" >nul
setlocal EnableDelayedExpansion
set count=0
for /f "delims=" %%f in ('dir /b /o-d "Ratio_prod_20??_??_??.xlsm"') do (
set /a count+=1
if !count! gtr 5 del "%%f"
)
endlocal
popd >nul
REM ---------------------------------------------------------------------------
REM Nettoyage: garder 5 archives
REM ---------------------------------------------------------------------------
pushd "%PROD_DIR%" >nul
setlocal EnableDelayedExpansion
set count=0
for /f "delims=" %%f in ('dir /b /o-d "Ratio_prod_20??_??_??.xlsm"') do (
set /a count+=1
if !count! gtr 5 del "%%f"
)
endlocal
popd >nul
echo(
echo( [OK] Mise à jour terminée :
echo( - Version : %NEW_VER%
echo( - PROD : "%PROD_XLSM%"
echo( - CLIENT : "%INSTALL_CLIENT%\Ratio_prod.xlsm"
echo( - PROD\VERSION.txt: %NEW_VER%
echo(
endlocal & exit /b 0

View File

@@ -1,31 +1,66 @@
' set_cell_silent.vbs ' set_cell_silent.vbs
' Usage: cscript //nologo set_cell_silent.vbs "C:\x\file.xlsm" "Feuille" "A1" "Valeur" ' Usage: cscript //nologo set_cell_silent.vbs "C:\chemin\fichier.xlsm" "NomFeuille" "A1" "Valeur"
Option Explicit
Dim f, sheetName, addr, val
Dim xl, wb, ws
If WScript.Arguments.Count < 4 Then
WScript.Echo "[ERR] Args: set_cell_silent.vbs <fichier.xlsm> <feuille> <cellule> <valeur>"
WScript.Quit 1
End If
f = WScript.Arguments(0)
sheetName = WScript.Arguments(1)
addr = WScript.Arguments(2)
val = WScript.Arguments(3)
On Error Resume Next On Error Resume Next
If WScript.Arguments.Count < 4 Then WScript.Quit 1
Dim fpath, sname, addr, val
fpath = WScript.Arguments(0)
sname = WScript.Arguments(1)
addr = WScript.Arguments(2)
val = WScript.Arguments(3)
Dim xl, wb, ws
Set xl = CreateObject("Excel.Application") Set xl = CreateObject("Excel.Application")
xl.DisplayAlerts = False If Err.Number <> 0 Then
WScript.Echo "[ERR] Excel non disponible (" & Err.Description & ")"
WScript.Quit 1
End If
On Error GoTo 0
xl.Visible = False xl.Visible = False
xl.AutomationSecurity = 3 ' macros OFF xl.DisplayAlerts = False
xl.EnableEvents = False ' événements OFF
Set wb = xl.Workbooks.Open(fpath, 0, False) ' Désactiver macros et événements AVANT douvrir
If Err.Number <> 0 Then xl.Quit: WScript.Quit 2 On Error Resume Next
xl.AutomationSecurity = 3 ' msoAutomationSecurityForceDisable
xl.EnableEvents = False
xl.ScreenUpdating = False
On Error GoTo 0
Set ws = wb.Worksheets(sname) On Error Resume Next
If Err.Number <> 0 Then wb.Close False: xl.Quit: WScript.Quit 3 Set wb = xl.Workbooks.Open(f, False, False)
If Err.Number <> 0 Then
xl.Quit
WScript.Echo "[ERR] Ouverture classeur: " & Err.Description
WScript.Quit 1
End If
On Error GoTo 0
ws.Range(addr).Value = val On Error Resume Next
If Err.Number <> 0 Then wb.Close False: xl.Quit: WScript.Quit 4 Set ws = wb.Worksheets.Item(sheetName)
If Err.Number <> 0 Then
wb.Close False
xl.Quit
WScript.Echo "[ERR] Feuille introuvable: " & sheetName
WScript.Quit 1
End If
On Error GoTo 0
On Error Resume Next
ws.Range(addr).Value2 = val
If Err.Number <> 0 Then
wb.Close False
xl.Quit
WScript.Echo "[ERR] Ecriture cellule " & addr & " : " & Err.Description
WScript.Quit 1
End If
On Error GoTo 0
wb.Save wb.Save
wb.Close False wb.Close False