I was able to find some solution, well at leats it looks like its working.
(Information: I have noticed that the script does not work with an Excel .csv file. I would like to make the xls file available, but unfortunately the forum does not allow it. Can I make it available otherwise if required?)
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to help identify potential issues.
SetWorkingDir %A_ScriptDir% ; Sets the working directory to the script's directory.
SendMode Input ; Sets the method for sending keystrokes to 'Input'.
#SingleInstance force ; Ensures that only one instance of the script is running.
SetBatchLines -1 ; Maximizes script performance by running it as fast as possible.
ListLines Off ; Disables the listing of executed lines for performance.
try {
xl := ComObjActive("Excel.Application") ; Attempts to get an active instance of Excel.
} catch {
MsgBox, 48, Error, No active Excel instance found. Please open Excel before running this script.
return ; Exits if Excel is not open.
}
xl.Visible := true ; Makes Excel visible to the user and to track changes.
; Function to retrieve a workbook by part of its name
getWorkbookByName(excel, namePart) {
Loop, % excel.Workbooks.Count {
workbook := excel.Workbooks.Item(A_Index) ; Loops through all open workbooks.
If (InStr(workbook.Name, namePart) > 0) ; Checks if workbook name contains the specified part.
return workbook ; Returns the workbook if found.
}
return "" ; Returns an empty string if no matching workbook is found
}
F2:: ; Hotkey to start the script
; Attempts to connect to an Excel instance and checks if specific workbooks are open
try {
excel := ComObjActive("Excel.Application")
} catch {
MsgBox, % "Please open Excel and the Excel files 'BusinessReport' and 'WeeklySales'."
return
}
; Checks if specific workbooks are open.
workbookBR := getWorkbookByName(excel, "BusinessReport")
workbookVA := getWorkbookByName(excel, "WeeklySales")
; Generates an error message if the workbooks are missing.
If (!workbookBR && !workbookVA) {
MsgBox, % "Please open the Excel files 'BusinessReport' and 'WeeklySales'."
return
} else if (!workbookBR) {
MsgBox, % "Please open the Excel file 'BusinessReport'."
return
} else if (!workbookVA) {
MsgBox, % "Please open the Excel file 'WeeklySales'."
return
}
; Validate the header in column "O" of the BusinessReport
worksheetBR := workbookBR.Sheets(1)
header := worksheetBR.Cells(1, 15).Value
If (header != "Units Ordered") {
MsgBox, % "The header in column 'O' of the 'BusinessReport' workbook does not match the expected 'Units Ordered'. Please check the file."
return
}
; GUI definition with customized design
Gui, New
Gui, Font, s10 cBlack, Arial ; Set font and color
Gui, Color, FFFFFF ; Set background color
Gui, Add, Text, x10 y10 w360 h30 +Center, Bitte geben Sie das Land für den 'BusinessReport' ein: ; Show the GUI with the title "Select a Country".
Gui, Add, DropDownList, x10 y50 w360 h300 vCountryChoice, Deutschland|Frankreich|Kanada ; Add a dropdown list with options.
Gui, Add, Button, x10 y100 w360 h30 Default, OK ; Add an OK button to submit the selection.
Gui, Show, w380 h150, Wöchentliche Verkaufsstatistik
return
ButtonOK:
Gui, Submit ; Retrieve the data from the GUI.
global selectedCountry := CountryChoice ; Store the selected country from the dropdown.
Gui, Destroy ; Destroy the GUI after the selection.
; Setup GUI for progress bar
Gui, ProgressGui: New, +AlwaysOnTop +Owner
Gui, ProgressGui: Color, White
Gui, ProgressGui: Font, s12 cBlack, Verdana
Gui, ProgressGui: Add, Text, x10 y10 w300 h20 +Center, Verarbeitung läuft...
Gui, ProgressGui: Add, Progress, x10 y40 w280 h20 vProgress cLime ; gProgressChanged (wrong Control element)
Gui, ProgressGui: Show, w300 h70, Gleich geschafft
; Continue with the existing script.
WeeklySales := getWorkbookByName(xl, "WeeklySales")
; Define workbook and column settings based on country. The ternary operator (?:) is necessary to select between two values based on a condition. I can not omit it, because the code will not work properly. AutoHotkey will not recognize how the conditions should be evaluated and the values assigned.
; Without the ternary operator, I cannot perform the selection directly in a single line. Instead, I would have to use traditional if-else statements to implement the same logic.
; reportName := (selectedCountry = "Frankreich") ? "Hagebutte" :
;(selectedCountry = "NeuesLand1") ? "NeueDatei1" :
;(selectedCountry = "NeuesLand2") ? "NeueDatei2" :
;"BusinessReport"
; asinColumn := (selectedCountry = "Frankreich") ? "A" : "B"
; skuColumn := (selectedCountry = "Frankreich") ? "L" : "D"
; unitsOrderedColumn := (selectedCountry = "Frankreich") ? "G" : "O"
; Traditional if-else statements
; Assume selectedCountry is set somewhere in the GUI
; Start the assignment based on the selected country
if (selectedCountry = "Frankreich") {
reportName := "Hagebutte"
asinColumn := "A"
skuColumn := "L"
unitsOrderedColumn := "G"
} else if (selectedCountry = "Deutschland") {
reportName := "BusinessReport"
asinColumn := "B"
skuColumn := "D"
unitsOrderedColumn := "O"
} else if (selectedCountry = "Kanada") {
reportName := "BusinessReport"
asinColumn := "B"
skuColumn := "D"
unitsOrderedColumn := "O"
} else if (selectedCountry = "NeuesLand2") {
reportName := "NeueDatei2"
asinColumn := "D"
skuColumn := "F"
unitsOrderedColumn := "Q"
} else {
MsgBox , % "No Country matches the Reports, please change the Country or open the associated report." ; Standardwert, wenn keine anderen Bedingungen zutreffen
}
; Attempt to open the selected report workbook
reportWorkbook := getWorkbookByName(xl, reportName)
if (!reportWorkbook) {
MsgBox, % "Could not find the workbook '" reportName "'. Please ensure it is open."
return
}
; BusinessReport := getWorkbookByName(xl, "BusinessReport") ; duplicate or necessary?
; WeeklySales := getWorkbookByName(xl, "WeeklySales") ; duplicate or necessaryne
; Loops through all rows in the WeeklySales sheet.
BRSheet := reportWorkbook.Sheets(1)
;WSSheet := WeeklySales.Sheets(1)
WSSheet := getWorkbookByName(xl, "WeeklySales").Sheets(1)
BRRows := BRSheet.UsedRange.Rows.Count
WSRows := WSSheet.UsedRange.Rows.Count
countryColumn := { "Deutschland": "D", "Frankreich": "H", "Kanada": "L" } ; Defines country columns for data entry based on user selection.
; selectedCountry := "Deutschland" ; This should be selected dynamically via a GUI.
; Data processing loop
Loop, % WSRows-1 {
wsrow := A_Index + 1
ASIN_WS := Trim(WSSheet.Range("B" . wsrow).Value) ; Gets the ASIN from WeeklySales.
SKU_WS := Trim(WSSheet.Range("A" . wsrow).Value) ; Gets the SKU from WeeklySales.
noMatchFound := true ; Flag to check if a match is found.
if (ASIN_WS = "" && RegExMatch(SKU_WS, "i)-FBA$")) {
continue ; Überspringen dieser Zeile, wenn keine ASIN vorhanden ist und die SKU auf "-FBA" endet
}
; Loops through all rows in the Business Report sheet.
Loop, % BRRows-1 {
row := A_Index + 1
ASIN_BR := Trim(BRSheet.Range(asinColumn . row).Value) ; Gets the ASIN based on selected country.
SKU_BR := Trim(BRSheet.Range(skuColumn . row).Value) ; Gets the SKU based on selected country.
UnitsOrdered := BRSheet.Range(unitsOrderedColumn . row).Value ; Gets the Units Ordered based on selected country.
; if (RegExMatch(SKU_BR, ".*-B$")) {; What was the Differenze between i and .* ...?
if (RegExMatch(SKU_BR, "i)-B$")) { ; Skips entries where SKU ends with '-B'.
; MsgBox, % "SKU `" SKU_BR "` ends with '-B' and will be skipped."
continue
}
; if (ASIN_BR = ASIN_WS && ((RegExMatch(SKU_BR, ".*-FBA$") && RegExMatch(SKU_WS, ".*-FBA$")) || (!RegExMatch(SKU_BR, ".*(-FBA|-B)$") && !RegExMatch(SKU_WS, ".*(-FBA|-B)$")))) { ; Old working Code.
; Checks for matching ASINs and SKUs, respecting the '-FBA' notation.
if (ASIN_BR = ASIN_WS && ((RegExMatch(SKU_BR, "i)-FBA$") && RegExMatch(SKU_WS, "i)-FBA$")) || (!RegExMatch(SKU_BR, "i).*(-FBA|-B)$") && !RegExMatch(SKU_WS, "i).*(-FBA|-B)$")))) { ; new one with i instead of .*
WSSheet.Range(countryColumn[selectedCountry] . wsrow).Value := UnitsOrdered
; MsgBox, % "Orders for ASIN `" ASIN_BR "` and SKU `" SKU_BR "` have been entered in row " wsrow " of 'WeeklySales' in column `" countryColumn[selectedCountry] "`."
noMatchFound := false
break ; Stops the loop once a match is found.
}
}
; if (noMatchFound) { ; old code withouth checken for empty spaces of ASIN in WeeklySales
;If no match is found, enters '0' for that ASIN in WeeklySales.
if (noMatchFound && ASIN_WS != "") {
WSSheet.Range(countryColumn[selectedCountry] . wsrow).Value := 0
;MsgBox, % "No match found for ASIN `" ASIN_WS "` in 'BusinessReport'. A '0' has been entered in 'WeeklySales' row " wsrow "."
}
; Update the progress in the GUI
ProgressUpdate((A_Index * 100) / WSRows)
}
; Completion message and clean-up
Gui, ProgressGui: Destroy
MsgBox, % "Data transfer with ASIN match completed."
Gui, Destroy
return
GuiClose:
Gui, ProgressGui: Destroy
return
; Function to update the progress bar in the GUI
ProgressUpdate(progress) {
global
GuiControl, ProgressGui:, Progress, %progress%
if (progress = 100) {
Gui, ProgressGui: Destroy
MsgBox, % "Datenübertragung abgeschlossen."
}
}
F10::ExitApp ; Hotkey to exit the script.
F11::Suspend ; Hotkey to disable/enable all hotkeys and hotstrings.
F12::Reload ; Hotkey to reload the script.
Can somebody please be so nice to take a look at the code if it is written cleanly or "dirty" ?
A problem that I know of is that when I accidentally click in an Excel row, the code crashes. It doesn't exit and once I'm out of the excel row I can run the code normally, but I don't know how to solve this.
I get several error messages, all starting with
Error: 0x80010001 Call was rejected by called party.
- Screenshot 2024-04-13 162534.png (21.46 KiB) Viewed 98 times
Just one thing out of curiosity. I am also having two more version, basically for performance and Stability - at least what the chatbot told me
ScreenUpdating
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to help identify potential issues.
SetWorkingDir %A_ScriptDir% ; Sets the working directory to the script's directory.
SendMode Input ; Sets the method for sending keystrokes to 'Input'.
#SingleInstance force ; Ensures that only one instance of the script is running.
SetBatchLines -1 ; Maximizes script performance by running it as fast as possible.
ListLines Off ; Disables the listing of executed lines for performance.
try {
xl := ComObjActive("Excel.Application")
xl.ScreenUpdating := false ; Deaktiviere die Bildschirmaktualisierung für die Dauer des Skripts
} catch {
MsgBox, 48, Error, No active Excel instance found. Please open Excel before running this script.
return ; Exits if Excel is not open.
}
; Function to retrieve a workbook by part of its name
getWorkbookByName(excel, namePart) {
Loop, % excel.Workbooks.Count {
workbook := excel.Workbooks.Item(A_Index) ; Loops through all open workbooks.
If (InStr(workbook.Name, namePart) > 0) ; Checks if workbook name contains the specified part.
return workbook ; Returns the workbook if found.
}
return "" ; Returns an empty string if no matching workbook is found
}
F2:: ; Hotkey to start the script
; Attempts to connect to an Excel instance and checks if specific workbooks are open
try {
excel := ComObjActive("Excel.Application")
} catch {
MsgBox, % "Please open Excel and the Excel files 'BusinessReport' and 'WeeklySales'."
return
}
; Checks if specific workbooks are open.
workbookBR := getWorkbookByName(excel, "BusinessReport")
workbookVA := getWorkbookByName(excel, "WeeklySales")
; Generates an error message if the workbooks are missing.
If (!workbookBR && !workbookVA) {
MsgBox, % "Please open the Excel files 'BusinessReport' and 'WeeklySales'."
return
} else if (!workbookBR) {
MsgBox, % "Please open the Excel file 'BusinessReport'."
return
} else if (!workbookVA) {
MsgBox, % "Please open the Excel file 'WeeklySales'."
return
}
; Validate the header in column "O" of the BusinessReport
worksheetBR := workbookBR.Sheets(1)
header := worksheetBR.Cells(1, 15).Value
If (header != "Units Ordered") {
MsgBox, % "The header in column 'O' of the 'BusinessReport' workbook does not match the expected 'Units Ordered'. Please check the file."
return
}
; GUI definition with customized design
Gui, New
Gui, Font, s10 cBlack, Arial ; Set font and color
Gui, Color, FFFFFF ; Set background color
Gui, Add, Text, x10 y10 w360 h30 +Center, Bitte geben Sie das Land für den 'BusinessReport' ein: ; Show the GUI with the title "Select a Country".
Gui, Add, DropDownList, x10 y50 w360 h300 vCountryChoice, Deutschland|Frankreich|Kanada ; Add a dropdown list with options.
Gui, Add, Button, x10 y100 w360 h30 Default, OK ; Add an OK button to submit the selection.
Gui, Show, w380 h150, Wöchentliche Verkaufsstatistik
return
ButtonOK:
Gui, Submit ; Retrieve the data from the GUI.
global selectedCountry := CountryChoice ; Store the selected country from the dropdown.
Gui, Destroy ; Destroy the GUI after the selection.
; Setup GUI for progress bar
Gui, ProgressGui: New, +AlwaysOnTop +Owner
Gui, ProgressGui: Color, White
Gui, ProgressGui: Font, s12 cBlack, Verdana
Gui, ProgressGui: Add, Text, x10 y10 w300 h20 +Center, Verarbeitung läuft...
Gui, ProgressGui: Add, Progress, x10 y40 w280 h20 vProgress cLime ; gProgressChanged (wrong Control element)
Gui, ProgressGui: Show, w300 h70, Gleich geschafft
; Continue with the existing script.
WeeklySales := getWorkbookByName(xl, "WeeklySales")
; Define workbook and column settings based on country. The ternary operator (?:) is necessary to select between two values based on a condition. I can not omit it, because the code will not work properly. AutoHotkey will not recognize how the conditions should be evaluated and the values assigned.
; Without the ternary operator, I cannot perform the selection directly in a single line. Instead, I would have to use traditional if-else statements to implement the same logic.
; reportName := (selectedCountry = "Frankreich") ? "Hagebutte" :
;(selectedCountry = "NeuesLand1") ? "NeueDatei1" :
;(selectedCountry = "NeuesLand2") ? "NeueDatei2" :
;"BusinessReport"
; asinColumn := (selectedCountry = "Frankreich") ? "A" : "B"
; skuColumn := (selectedCountry = "Frankreich") ? "L" : "D"
; unitsOrderedColumn := (selectedCountry = "Frankreich") ? "G" : "O"
; Traditional if-else statements
; Assume selectedCountry is set somewhere in the GUI
; Start the assignment based on the selected country
if (selectedCountry = "Frankreich") {
reportName := "Hagebutte"
asinColumn := "A"
skuColumn := "L"
unitsOrderedColumn := "G"
} else if (selectedCountry = "Deutschland") {
reportName := "BusinessReport"
asinColumn := "B"
skuColumn := "D"
unitsOrderedColumn := "O"
} else if (selectedCountry = "Kanada") {
reportName := "BusinessReport"
asinColumn := "B"
skuColumn := "D"
unitsOrderedColumn := "O"
} else if (selectedCountry = "NeuesLand2") {
reportName := "NeueDatei2"
asinColumn := "D"
skuColumn := "F"
unitsOrderedColumn := "Q"
} else {
MsgBox , % "No Country matches the Reports, please change the Country or open the associated report." ; Standardwert, wenn keine anderen Bedingungen zutreffen
}
; Attempt to open the selected report workbook
reportWorkbook := getWorkbookByName(xl, reportName)
if (!reportWorkbook) {
MsgBox, % "Could not find the workbook '" reportName "'. Please ensure it is open."
return
}
; BusinessReport := getWorkbookByName(xl, "BusinessReport") ; duplicate or necessary?
; WeeklySales := getWorkbookByName(xl, "WeeklySales") ; duplicate or necessaryne
; Loops through all rows in the WeeklySales sheet.
BRSheet := reportWorkbook.Sheets(1)
;WSSheet := WeeklySales.Sheets(1)
WSSheet := getWorkbookByName(xl, "WeeklySales").Sheets(1)
BRRows := BRSheet.UsedRange.Rows.Count
WSRows := WSSheet.UsedRange.Rows.Count
countryColumn := { "Deutschland": "D", "Frankreich": "H", "Kanada": "L" } ; Defines country columns for data entry based on user selection.
; selectedCountry := "Deutschland" ; This should be selected dynamically via a GUI.
; Data processing loop
Loop, % WSRows-1 {
wsrow := A_Index + 1
ASIN_WS := Trim(WSSheet.Range("B" . wsrow).Value) ; Gets the ASIN from WeeklySales.
SKU_WS := Trim(WSSheet.Range("A" . wsrow).Value) ; Gets the SKU from WeeklySales.
noMatchFound := true ; Flag to check if a match is found.
if (ASIN_WS = "" && RegExMatch(SKU_WS, "i)-FBA$")) {
continue ; Überspringen dieser Zeile, wenn keine ASIN vorhanden ist und die SKU auf "-FBA" endet
}
; Loops through all rows in the Business Report sheet.
Loop, % BRRows-1 {
row := A_Index + 1
ASIN_BR := Trim(BRSheet.Range(asinColumn . row).Value) ; Gets the ASIN based on selected country.
SKU_BR := Trim(BRSheet.Range(skuColumn . row).Value) ; Gets the SKU based on selected country.
UnitsOrdered := BRSheet.Range(unitsOrderedColumn . row).Value ; Gets the Units Ordered based on selected country.
; if (RegExMatch(SKU_BR, ".*-B$")) {; What was the Differenze between i and .* ...?
if (RegExMatch(SKU_BR, "i)-B$")) { ; Skips entries where SKU ends with '-B'.
; MsgBox, % "SKU `" SKU_BR "` ends with '-B' and will be skipped."
continue
}
; if (ASIN_BR = ASIN_WS && ((RegExMatch(SKU_BR, ".*-FBA$") && RegExMatch(SKU_WS, ".*-FBA$")) || (!RegExMatch(SKU_BR, ".*(-FBA|-B)$") && !RegExMatch(SKU_WS, ".*(-FBA|-B)$")))) { ; Old working Code.
; Checks for matching ASINs and SKUs, respecting the '-FBA' notation.
if (ASIN_BR = ASIN_WS && ((RegExMatch(SKU_BR, "i)-FBA$") && RegExMatch(SKU_WS, "i)-FBA$")) || (!RegExMatch(SKU_BR, "i).*(-FBA|-B)$") && !RegExMatch(SKU_WS, "i).*(-FBA|-B)$")))) { ; new one with i instead of .*
WSSheet.Range(countryColumn[selectedCountry] . wsrow).Value := UnitsOrdered
; MsgBox, % "Orders for ASIN `" ASIN_BR "` and SKU `" SKU_BR "` have been entered in row " wsrow " of 'WeeklySales' in column `" countryColumn[selectedCountry] "`."
noMatchFound := false
break ; Stops the loop once a match is found.
}
}
; if (noMatchFound) { ; old code withouth checken for empty spaces of ASIN in WeeklySales
;If no match is found, enters '0' for that ASIN in WeeklySales.
if (noMatchFound && ASIN_WS != "") {
WSSheet.Range(countryColumn[selectedCountry] . wsrow).Value := 0
;MsgBox, % "No match found for ASIN `" ASIN_WS "` in 'BusinessReport'. A '0' has been entered in 'WeeklySales' row " wsrow "."
}
; Update the progress in the GUI
ProgressUpdate((A_Index * 100) / WSRows)
}
; Completion message and clean-up
Gui, ProgressGui: Destroy
MsgBox, % "Data transfer with ASIN match completed."
Gui, Destroy
return
GuiClose:
Gui, ProgressGui: Destroy
return
; Function to update the progress bar in the GUI
ProgressUpdate(progress) {
global
GuiControl, ProgressGui:, Progress, %progress%
if (progress = 100) {
Gui, ProgressGui: Destroy
MsgBox, % "Datenübertragung abgeschlossen."
}
xl.ScreenUpdating := true ; Stelle die Bildschirmaktualisierung wieder her
}
F10::ExitApp ; Hotkey to exit the script.
F11::Suspend ; Hotkey to disable/enable all hotkeys and hotstrings.
F12::Reload ; Hotkey to reload the script.
Loop
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to help identify potential issues.
SetWorkingDir %A_ScriptDir% ; Sets the working directory to the script's directory.
SendMode Input ; Sets the method for sending keystrokes to 'Input'.
#SingleInstance force ; Ensures that only one instance of the script is running.
SetBatchLines -1 ; Maximizes script performance by running it as fast as possible.
ListLines Off ; Disables the listing of executed lines for performance.
try {
xl := ComObjActive("Excel.Application") ; Attempts to get an active instance of Excel.
} catch {
MsgBox, 48, Error, No active Excel instance found. Please open Excel before running this script.
return ; Exits if Excel is not open.
}
xl.Visible := true ; Makes Excel visible to the user and to track changes.
; Function to retrieve a workbook by part of its name
getWorkbookByName(excel, namePart) {
Loop, % excel.Workbooks.Count {
workbook := excel.Workbooks.Item(A_Index) ; Loops through all open workbooks.
If (InStr(workbook.Name, namePart) > 0) ; Checks if workbook name contains the specified part.
return workbook ; Returns the workbook if found.
}
return "" ; Returns an empty string if no matching workbook is found
}
F2:: ; Hotkey to start the script
; Attempts to connect to an Excel instance and checks if specific workbooks are open
try {
excel := ComObjActive("Excel.Application")
} catch {
MsgBox, % "Please open Excel and the Excel files 'BusinessReport' and 'WeeklySales'."
return
}
; Checks if specific workbooks are open.
workbookBR := getWorkbookByName(excel, "BusinessReport")
workbookVA := getWorkbookByName(excel, "WeeklySales")
; Generates an error message if the workbooks are missing.
If (!workbookBR && !workbookVA) {
MsgBox, % "Please open the Excel files 'BusinessReport' and 'WeeklySales'."
return
} else if (!workbookBR) {
MsgBox, % "Please open the Excel file 'BusinessReport'."
return
} else if (!workbookVA) {
MsgBox, % "Please open the Excel file 'WeeklySales'."
return
}
; Validate the header in column "O" of the BusinessReport
worksheetBR := workbookBR.Sheets(1)
header := worksheetBR.Cells(1, 15).Value
If (header != "Units Ordered") {
MsgBox, % "The header in column 'O' of the 'BusinessReport' workbook does not match the expected 'Units Ordered'. Please check the file."
return
}
; GUI definition with customized design
Gui, New
Gui, Font, s10 cBlack, Arial ; Set font and color
Gui, Color, FFFFFF ; Set background color
Gui, Add, Text, x10 y10 w360 h30 +Center, Bitte geben Sie das Land für den 'BusinessReport' ein: ; Show the GUI with the title "Select a Country".
Gui, Add, DropDownList, x10 y50 w360 h300 vCountryChoice, Deutschland|Frankreich|Kanada ; Add a dropdown list with options.
Gui, Add, Button, x10 y100 w360 h30 Default, OK ; Add an OK button to submit the selection.
Gui, Show, w380 h150, Wöchentliche Verkaufsstatistik
return
ButtonOK:
Gui, Submit ; Retrieve the data from the GUI.
global selectedCountry := CountryChoice ; Store the selected country from the dropdown.
Gui, Destroy ; Destroy the GUI after the selection.
; Setup GUI for progress bar
Gui, ProgressGui: New, +AlwaysOnTop +Owner
Gui, ProgressGui: Color, White
Gui, ProgressGui: Font, s12 cBlack, Verdana
Gui, ProgressGui: Add, Text, x10 y10 w300 h20 +Center, Verarbeitung läuft...
Gui, ProgressGui: Add, Progress, x10 y40 w280 h20 vProgress cLime ; gProgressChanged (wrong Control element)
Gui, ProgressGui: Show, w300 h70, Gleich geschafft
; Continue with the existing script.
WeeklySales := getWorkbookByName(xl, "WeeklySales")
; Define workbook and column settings based on country. The ternary operator (?:) is necessary to select between two values based on a condition. I can not omit it, because the code will not work properly. AutoHotkey will not recognize how the conditions should be evaluated and the values assigned.
; Without the ternary operator, I cannot perform the selection directly in a single line. Instead, I would have to use traditional if-else statements to implement the same logic.
; reportName := (selectedCountry = "Frankreich") ? "Hagebutte" :
;(selectedCountry = "NeuesLand1") ? "NeueDatei1" :
;(selectedCountry = "NeuesLand2") ? "NeueDatei2" :
;"BusinessReport"
; asinColumn := (selectedCountry = "Frankreich") ? "A" : "B"
; skuColumn := (selectedCountry = "Frankreich") ? "L" : "D"
; unitsOrderedColumn := (selectedCountry = "Frankreich") ? "G" : "O"
; Traditional if-else statements
; Assume selectedCountry is set somewhere in the GUI
; Start the assignment based on the selected country
if (selectedCountry = "Frankreich") {
reportName := "Hagebutte"
asinColumn := "A"
skuColumn := "L"
unitsOrderedColumn := "G"
} else if (selectedCountry = "Deutschland") {
reportName := "BusinessReport"
asinColumn := "B"
skuColumn := "D"
unitsOrderedColumn := "O"
} else if (selectedCountry = "Kanada") {
reportName := "BusinessReport"
asinColumn := "B"
skuColumn := "D"
unitsOrderedColumn := "O"
} else if (selectedCountry = "NeuesLand2") {
reportName := "NeueDatei2"
asinColumn := "D"
skuColumn := "F"
unitsOrderedColumn := "Q"
} else {
MsgBox , % "No Country matches the Reports, please change the Country or open the associated report." ; Standardwert, wenn keine anderen Bedingungen zutreffen
}
; Attempt to open the selected report workbook
reportWorkbook := getWorkbookByName(xl, reportName)
if (!reportWorkbook) {
MsgBox, % "Could not find the workbook '" reportName "'. Please ensure it is open."
return
}
; BusinessReport := getWorkbookByName(xl, "BusinessReport") ; duplicate or necessary?
; WeeklySales := getWorkbookByName(xl, "WeeklySales") ; duplicate or necessaryne
; Loops through all rows in the WeeklySales sheet.
BRSheet := reportWorkbook.Sheets(1)
;WSSheet := WeeklySales.Sheets(1)
WSSheet := getWorkbookByName(xl, "WeeklySales").Sheets(1)
BRRows := BRSheet.UsedRange.Rows.Count
WSRows := WSSheet.UsedRange.Rows.Count
countryColumn := { "Deutschland": "D", "Frankreich": "H", "Kanada": "L" } ; Defines country columns for data entry based on user selection.
; selectedCountry := "Deutschland" ; This should be selected dynamically via a GUI.
; Data processing loop
Loop, % WSRows-1 {
wsrow := A_Index + 1
ASIN_WS := Trim(WSSheet.Range("B" . wsrow).Value) ; Gets the ASIN from WeeklySales.
SKU_WS := Trim(WSSheet.Range("A" . wsrow).Value) ; Gets the SKU from WeeklySales.
noMatchFound := true ; Flag to check if a match is found.
if (ASIN_WS = "" && RegExMatch(SKU_WS, "i)-FBA$")) {
continue ; Überspringen dieser Zeile, wenn keine ASIN vorhanden ist und die SKU auf "-FBA" endet
}
; Loops through all rows in the Business Report sheet.
Loop, % BRRows-1 {
row := A_Index + 1
ASIN_BR := Trim(BRSheet.Range(asinColumn . row).Value) ; Gets the ASIN based on selected country.
SKU_BR := Trim(BRSheet.Range(skuColumn . row).Value) ; Gets the SKU based on selected country.
UnitsOrdered := BRSheet.Range(unitsOrderedColumn . row).Value ; Gets the Units Ordered based on selected country.
; if (RegExMatch(SKU_BR, ".*-B$")) {; What was the Differenze between i and .* ...?
if (RegExMatch(SKU_BR, "i)-B$")) { ; Skips entries where SKU ends with '-B'.
; MsgBox, % "SKU `" SKU_BR "` ends with '-B' and will be skipped."
continue
}
; if (ASIN_BR = ASIN_WS && ((RegExMatch(SKU_BR, ".*-FBA$") && RegExMatch(SKU_WS, ".*-FBA$")) || (!RegExMatch(SKU_BR, ".*(-FBA|-B)$") && !RegExMatch(SKU_WS, ".*(-FBA|-B)$")))) { ; Old working Code.
; Checks for matching ASINs and SKUs, respecting the '-FBA' notation.
if (ASIN_BR = ASIN_WS && ((RegExMatch(SKU_BR, "i)-FBA$") && RegExMatch(SKU_WS, "i)-FBA$")) || (!RegExMatch(SKU_BR, "i).*(-FBA|-B)$") && !RegExMatch(SKU_WS, "i).*(-FBA|-B)$")))) { ; new one with i instead of .*
WSSheet.Range(countryColumn[selectedCountry] . wsrow).Value := UnitsOrdered
; MsgBox, % "Orders for ASIN `" ASIN_BR "` and SKU `" SKU_BR "` have been entered in row " wsrow " of 'WeeklySales' in column `" countryColumn[selectedCountry] "`."
noMatchFound := false
Sleep, 10
break ; Stops the loop once a match is found.
}
Sleep, 10
}
; if (noMatchFound) { ; old code withouth checken for empty spaces of ASIN in WeeklySales
;If no match is found, enters '0' for that ASIN in WeeklySales.
if (noMatchFound && ASIN_WS != "") {
WSSheet.Range(countryColumn[selectedCountry] . wsrow).Value := 0
;MsgBox, % "No match found for ASIN `" ASIN_WS "` in 'BusinessReport'. A '0' has been entered in 'WeeklySales' row " wsrow "."
}
Sleep, 10 ; Update the progress in the GUI
ProgressUpdate((A_Index * 100) / WSRows)
}
; Completion message and clean-up
Gui, ProgressGui: Destroy
MsgBox, % "Data transfer with ASIN match completed."
Gui, Destroy
return
GuiClose:
Gui, ProgressGui: Destroy
Sleep, 10
return
; Function to update the progress bar in the GUI
ProgressUpdate(progress) {
global
GuiControl, ProgressGui:, Progress, %progress%
if (progress = 100) {
Gui, ProgressGui: Destroy
MsgBox, % "Datenübertragung abgeschlossen."
}
}
F10::ExitApp ; Hotkey to exit the script.
F11::Suspend ; Hotkey to disable/enable all hotkeys and hotstrings.
F12::Reload ; Hotkey to reload the script.
Any idea which one would be better xl.Visible := true vs ScreenUpdating vs Loop?