Quantcast
Channel: AutoHotkey Community
Viewing all articles
Browse latest Browse all 579

Ask for Help (v1) • Re: Need help with Excel data transfer

$
0
0
Thanks for taking a look, I really appreciate it. I've also added a GUI and, I know I'm being annoying but, if it is possible could you please take another look at my "final" final code :D in case I've missed some sneaky bugs.

At the moment I'm happy with the speed, and, well, at least, I hope this will be useful for some others as well.

CODE:

SendMode Input  ; Sets the script to use the input method for sending keystrokes.
SetWorkingDir %A_ScriptDir% ; Sets the script's working directory to where the script is located.

#SingleInstance force ; only run one instance of this script
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SetBatchLines -1 ;max performance
ListLines Off ;max performance
;SetKeyDelay, 0 ;max key hits
#MaxThreadsPerHotkey 1

; Define mappings of countries to their row ranges in sheets 1 and 2.
countryRowMappings := {}
countryRowMappings["Germany"] := {1: [4, 12], 2: [4, 18]}
countryRowMappings["Spain"] := {1: [16, 24], 2: [22, 35]}
countryRowMappings["Baguette(France)"] := {1: [28, 36], 2: [39, 52]}

F2:: ; Hotkey to start the script.
excel := ComObjActive("Excel.Application") ; Get the active Excel application.
workbookBR := getWorkbookByName(excel, "BusinessReport") ; Get the BusinessReport workbook.
workbookVA := getWorkbookByName(excel, "Verkaufsliste Autohotkey") ; Get the Verkaufsliste Autohotkey workbook.

; Check if the required workbooks are open.
If (workbookBR = "" or workbookVA = "") {
MsgBox, % "One or both of the required workbooks were not found."
return
}

; Check the header in column "O" of the BusinessReport.
worksheetBR := workbookBR.Sheets(1)
header := worksheetBR.Cells(1, 15).Value
If (header != "Bestellte Einheiten") {
MsgBox, % "The header in column 'O' of the 'BusinessReport' workbook does not match the expected 'Bestellte Einheiten'. Please check the file."
return
}

; GUI-Definition mit angepasstem Design
Gui, New
Gui, Font, s10 cBlack, Arial ; Set font and color
Gui, Color, FFFFFF ; Set background color
Gui, Add, Text, x10 y10 w300 h30 +Center, Bitte geben Sie das Land für den 'BusinessReport' ein:
Gui, Add, DropDownList, x10 y50 w300 h300 vCountryChoice, Germany|Spain|Baguette(France)
Gui, Add, Button, x10 y100 w300 h30 Default, OK
Gui, Show, w320 h150, Work Work GUI
return

ButtonOK: ; Action when the OK button is pressed.
Gui, Submit ; Get the user input from the GUI.
global country := CountryChoice ; Store the selected country.
Gui, Destroy ; Close the GUI.

; Data transfer logic based on the selected country.
For index, sheetIndex in [1, 2]
{
rowRange := countryRowMappings[country][sheetIndex]
startRow := rowRange[1]
endRow := rowRange[2]
worksheetVA := workbookVA.Sheets(sheetIndex)
CalculateSumOfOrderedUnits(excel, workbookBR.Sheets(1), worksheetVA, startRow, endRow)
}

MsgBox, % "Data transfer for " country " completed. Press F2 to continue with a new country."
return

GuiClose: ; Action when the GUI is closed.
ExitApp

getWorkbookByName(excel, nameStart) { ; Function to get a workbook by its starting name.
Loop, % excel.Workbooks.Count {
workbook := excel.Workbooks(A_Index)
If (InStr(workbook.Name, nameStart) = 1)
return workbook
}
return "" ; Return empty string if no matching workbook is found.
}

CalculateSumOfOrderedUnits(excel, worksheetBR, worksheetVA, startRow, endRow) { ; Function to calculate and transfer total units.
month := A_MM ; Get the current month as a number.
colVA := Chr(67 + month - 1) ; Determine the target column based on the month.

rowCountBR := worksheetBR.UsedRange.Rows.Count
ASINtoUnits := {}

; Collect data from BusinessReport and calculate total units.
Loop, %rowCountBR% {
rowBR := A_Index + 1
ASIN := worksheetBR.Cells(rowBR, 2).Value
SKU := worksheetBR.Cells(rowBR, 4).Value
bestellteEinheiten := worksheetBR.Cells(rowBR, 15).Value

If (!ASINtoUnits.HasKey(ASIN)) {
ASINtoUnits[ASIN] := {"FBA": 0, "Standard": 0, "HasFBA": false}
}

If (StrLen(SKU) > 0) {
If (InStr(SKU, "-FBA") and substr(SKU,-1) != "B") {
ASINtoUnits[ASIN]["FBA"] += bestellteEinheiten
ASINtoUnits[ASIN]["HasFBA"] := true
} Else If (substr(SKU,-1) != "B") {
ASINtoUnits[ASIN]["Standard"] += bestellteEinheiten
}
}
}

; Write the total units to the Verkaufsliste Autohotkey workbook.
Loop, % endRow - startRow + 1 {
rowVA := A_Index + startRow - 1
ASIN := worksheetVA.Cells(rowVA, 2).Value
If (ASINtoUnits.HasKey(ASIN)) {
totalUnits := ASINtoUnits[ASIN]["HasFBA"] ? (ASINtoUnits[ASIN]["FBA"] + ASINtoUnits[ASIN]["Standard"]) : ASINtoUnits[ASIN]["Standard"]
worksheetVA.Cells(rowVA, colVA).Value := totalUnits
} Else {
worksheetVA.Cells(rowVA, colVA).Value := 0
}
}
}

Statistics: Posted by Mindfork — Today, 17:11



Viewing all articles
Browse latest Browse all 579

Trending Articles