How to make script work on MULTIPLE Cells in Excel?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

How to make script work on MULTIPLE Cells in Excel?

21 Mar 2019, 19:46

I have the following script, which allows me to do an image search in Google for any highlighted cell.

What I want to achieve is, IF I ever highlight 5 cells at a time, it will extract the words for each cell, open new tab in Chrome to search for that term in Google images, instead of having to hit Ctrl+Win+Q for every cell that I want to search in Google. Any idea how I can do that please?

Code: Select all

	
	; Ctrl Win Q to run Google Images 
$^#Q::
    Send, ^c
    Sleep 50
    parameter = C:\Program Files (x86)\Google\Chrome\Application\chrome.exe https://www.google.com.sg/search?q="%clipboard%"&hl=en&gbv=2&tbs=isz:lt,islt:vga&tbm=isch&source=lnt&sa=X&ei=9UXAT-HlDofxrQfcg_XiCQ&ved=0CCwQpwUoBA&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1366&bih=677&cad=h
    Run %parameter%
    Return
User avatar
Scr1pter
Posts: 1277
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to make script work on MULTIPLE Cells in Excel?

22 Mar 2019, 08:48

Why exactly 5 cells?
If you mark just 2 cells, you could also do the same functionality.

Normally, when you mark and copy several cells (eg A1 till C1) and paste them in Notepad (or wherever),
there is a tab character between them.

For this reason you could work with Loop, Parse and split it on the tab character `t (check the syntax!)
After that, you can create for each copied cell an own variable or perhaps you put the values inside of a dynamic array.

I don't know how Google image search works.
Do you want to create several sites/tabs and paste all those values which you copied from Excel?

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: How to make script work on MULTIPLE Cells in Excel?

22 Mar 2019, 12:23

reverberation wrote:
21 Mar 2019, 19:46
I have the following script, which allows me to do an image search in Google for any highlighted cell.

What I want to achieve is, IF I ever highlight 5 cells at a time, it will extract the words for each cell, open new tab in Chrome to search for that term in Google images, instead of having to hit Ctrl+Win+Q for every cell that I want to search in Google. Any idea how I can do that please?

Code: Select all

	
	; Ctrl Win Q to run Google Images 
$^#Q::
    Send, ^c
    Sleep 50
    parameter = C:\Program Files (x86)\Google\Chrome\Application\chrome.exe https://www.google.com.sg/search?q="%clipboard%"&hl=en&gbv=2&tbs=isz:lt,islt:vga&tbm=isch&source=lnt&sa=X&ei=9UXAT-HlDofxrQfcg_XiCQ&ved=0CCwQpwUoBA&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1366&bih=677&cad=h
    Run %parameter%
    Return
Hello,

This is a much more direct way of taking the contents in your cells and running a Google Image Search by using COM. You can select more than 5 cells with this method if you want- you could select 10,000 or just 1 cell if wanted, any number of cells will work.

Well... actually Google has a limit and AHK won't run the string if it's too long :x
As far as searches, you can conduct the searches for 2048 characters with spaces but if you are searching for any subsequent keyword then google would limit the search till 32 words.
With this method you can also select cells that are not adjacent to one another (like with ctrl+left click). It will create the query in the order that you select the cells.

Code: Select all

$^#Q::
Xl := ComObjActive("Excel.Application")

For SelectedCell in Xl.Selection
	SelectedCells .= SelectedCell.Value "+"

Query := StrReplace(SelectedCells, A_Space, "+")
,	GoogleImageSearch := "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe "
.	"https://www.google.com.sg/search?q=" Query "&hl=en&gbv=2&tbs=isz:lt,islt:vga&"
.	"tbm=isch&source=lnt&sa=X&ei=9UXAT-HlDofxrQfcg_XiCQ&ved=0CCwQpwUoBA&"
.	"bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1366&bih=677&cad=h"

Run % GoogleImageSearch
return
LMK how that works for you! :)


EDIT: changed For SelectedCell, Value in Xl.Selection to For SelectedCell in Xl.Selection as it is unnecessary.
Last edited by Tigerlily on 22 Mar 2019, 16:50, edited 2 times in total.
-TL
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: How to make script work on MULTIPLE Cells in Excel?

22 Mar 2019, 13:14

Code: Select all

For cell, Value in Xl.Selection
No need for the value parameter, it is superfluous in this case.

Code: Select all

For cell in Xl.Selection
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: How to make script work on MULTIPLE Cells in Excel?

22 Mar 2019, 16:36

sinkfaze wrote:
22 Mar 2019, 13:14
Thanks sinkfaze - I am still trying to wrap my mind around for-loops, they really confuse me for some reason, but starting to make more sense :crazy:
-TL
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: How to make script work on MULTIPLE Cells in Excel?

23 Mar 2019, 22:09

Hi TigerLily,

Thanks for sharing the script. The outcome is not what I wanted though.

Taking these 3 cells for example, if I were to highlight all 3 at the same time and trigger it, it should open 3 Separate tabs in chrome, each searching for the terms in Google Images, instead of merging the 3 cells into 1 input.


Image
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: How to make script work on MULTIPLE Cells in Excel?

23 Mar 2019, 22:48

reverberation wrote:
23 Mar 2019, 22:09
Hi TigerLily,

Thanks for sharing the script. The outcome is not what I wanted though.

Taking these 3 cells for example, if I were to highlight all 3 at the same time and trigger it, it should open 3 Separate tabs in chrome, each searching for the terms in Google Images, instead of merging the 3 cells into 1 input.
Hi reverberation,

This will do as you need:

Code: Select all

$^#Q::
Xl := ComObjActive("Excel.Application")

For SelectedCell in Xl.Selection
{
	Query := StrReplace(SelectedCell.Value, A_Space, "+")
,	GoogleImageSearch := "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe "
.	"https://www.google.com.sg/search?q=" Query "&hl=en&gbv=2&tbs=isz:lt,islt:vga&"
.	"tbm=isch&source=lnt&sa=X&ei=9UXAT-HlDofxrQfcg_XiCQ&ved=0CCwQpwUoBA&"
.	"bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&biw=1366&bih=677&cad=h"
	Run % GoogleImageSearch
}
return
-TL
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: How to make script work on MULTIPLE Cells in Excel?

26 Mar 2019, 06:00

Thanks again! Few qns:

1. How do I make the same hotkey work also for other applications (e.g. Chrome/Word/PDF) in the same script instead of assigning a separate hotkey?
2. Using Ctrl+Win+Q in Excel opens "Quick Assist" in Win 10. I tried searching around but there was no way to do so. I would like to retain the hotkey assignment. Any workaround for this?
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: How to make script work on MULTIPLE Cells in Excel?

05 Apr 2019, 09:12

reverberation wrote:
26 Mar 2019, 06:00
Thanks again! Few qns:

1. How do I make the same hotkey work also for other applications (e.g. Chrome/Word/PDF) in the same script instead of assigning a separate hotkey?
2. Using Ctrl+Win+Q in Excel opens "Quick Assist" in Win 10. I tried searching around but there was no way to do so. I would like to retain the hotkey assignment. Any workaround for this?
any idea????
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: How to make script work on MULTIPLE Cells in Excel?

07 Apr 2019, 03:04

reverberation wrote:
26 Mar 2019, 06:00
Thanks again! Few qns:

1. How do I make the same hotkey work also for other applications (e.g. Chrome/Word/PDF) in the same script instead of assigning a separate hotkey?
2. Using Ctrl+Win+Q in Excel opens "Quick Assist" in Win 10. I tried searching around but there was no way to do so. I would like to retain the hotkey assignment. Any workaround for this?

1. Your hotkey should work independently across all applications since you are using COM - it will run in the background regardless of the Excel window having active focus. However, if you want certain hotkeys to work differently for different applications use Context-sensitive Hotkeys - you'll just need to tweak your hotkey of course (I'm gonna leave that up to you).

2. I am not sure why your hotkey is triggering a keyboard shortcut in Excel - I could not recreate your issue - Excel ignores my actual input and AHK takes it instead. Try removing the $, as I don't think you need it in this case (not sure, read the docs on it though and didn't seem like you needed it). Is it opening the quick assist menu AND performing the image search, or just one or the other?
-TL
reverberation
Posts: 314
Joined: 13 Dec 2015, 20:48

Re: How to make script work on MULTIPLE Cells in Excel?

13 Apr 2019, 22:35

noted. have another question though:

when replacing the chrome path with a username variable, it refuses to launch. is there any workaround for it?

Code: Select all

 GoogleImageSearch := "C:\Users\%A_UserName%\AppData\Local\Google\Chrome\Application\chrome.exe" 
gregster
Posts: 9068
Joined: 30 Sep 2013, 06:48

Re: How to make script work on MULTIPLE Cells in Excel?

13 Apr 2019, 22:43

Try this syntax (it's an expression - strings as quotes, variables without %'s):

Code: Select all

GoogleImageSearch := "C:\Users\" A_UserName "\AppData\Local\Google\Chrome\Application\chrome.exe"

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Luke10 and 129 guests