How to copy a single Libre Office calc/Excel cell value into a variable as a float

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ctzen
Posts: 2
Joined: 21 May 2022, 06:56

How to copy a single Libre Office calc/Excel cell value into a variable as a float

Post by Ctzen » 21 May 2022, 07:15

Hello!

This may have been asked and answered a thousand times, but I am unable to find it on the forum. I have a spreadsheet. In that spreadsheet are some cells with various values. I want to manually mark the cell with the mouse, copy a value to the clipboard (ctrl+c), save the clipboard as a variable, do a calculation with that variable, and paste it in another program along with a series of other strings.

I don't want anything too fancy, if it can be avoided. I can make my command work, when I copy a number in a notepad window for example. Just not within Libre Office Calc. I assume it will also be a problem in excel and I need the command to work in both. Also, I use the European style of designating decimals, so I need the command to be able to copy a float with a comma (10,2 not 10.2) and also paste a float with a comma. This last part I have not been able to yet either. This is what I have:

Code: Select all

!u::
U := Clipboard
sleep 100
if U is number
{
	P := U * 0.25
	Send %P%{Tab}Hello{Tab}There
}
else
MsgBox %U% is not a number
return
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 21 May 2022, 09:13, edited 1 time in total.
Reason: Also removed duplicate topic.


Ctzen
Posts: 2
Joined: 21 May 2022, 06:56

Re: How to copy a single Libre Office calc/Excel cell value into a variable as a float

Post by Ctzen » 21 May 2022, 10:18

Maybe I'm overlooking something, but it seems it only says how to extract data from a predetermined cell? How do you extract it from the selected cell? And how do you convert the number from a European decimal designation (10,2 => 10.2), so AHK will be able to do calculations (10.2 * 0.25 = 2.55), and back again to the output (2.55 => 2,55)?

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: How to copy a single Libre Office calc/Excel cell value into a variable as a float

Post by mikeyww » 21 May 2022, 10:55

Code: Select all

target = ahk_exe notepad.exe
#If WinExist("ahk_exe EXCEL.exe") && WinExist(target)
F3::
XL := ComObjActive("Excel.Application")
WinActivate
SendInput % StrReplace(Round(StrReplace(XL.Selection.Value, ",", ".") + 3, 2), ".", ",")
Return
#If

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: How to copy a single Libre Office calc/Excel cell value into a variable as a float

Post by flyingDman » 21 May 2022, 12:13

I believe the issue is that when you do a ctrl c on the active cell in Excel, it will include a return or a line feed. I believe the same is true in Libre Office. So before doing any math, you'll need to trim these. This works:

Code: Select all

#F3::
send ^c
clipwait, 1
msgbox % trim(clipboard," `n`r") + 1000
You then can do the strreplace to convert the ,'s and .'s to . and , and back.
14.3 & 1.3.7

Post Reply

Return to “Ask for Help (v1)”