I'm trying to get a sense of the viability of replacing some of my Microsoft Excel spreadsheets with Google Doc Spreadsheets. How can I create a custom keyboard shortcut to a google app script function in a google docs spreadsheet? This is something I commonly do with VBA and Excel.
9 Answers
Apps Script only exposes server side events. Unfortunately, you cannot register client side events like keyboard strokes today. Please log an issue in our issue tracker

- 5,547
- 1
- 22
- 19
-
2The closest issue on the issue tracker appears to be [issue 306](https://code.google.com/p/google-apps-script-issues/issues/detail?id=306) which could do with a few more stars/upvotes but there is a deafening silence from the apps team on this :( – Simon Francesco Nov 20 '14 at 21:10
-
1Someone added a working solution at [issue 306 post 33](https://code.google.com/p/google-apps-script-issues/issues/detail?id=306#c33)! – John Targaryen Jun 01 '15 at 22:09
-
you can do this with Macro – Liker777 Feb 01 '20 at 13:55
-
See answer below for dicussion of macros (click click click) – Att Righ Jun 09 '21 at 16:09
This now supported in Sheets (see https://issuetracker.google.com/issues/36752620), but not yet in Docs. See and star https://issuetracker.google.com/issues/36752620 for Docs support

- 14,272
- 6
- 84
- 96
-
2
-
-
1
-
@Imray It could. It could also be a good "solution" to the problem in hand, given the feature is not there out of the box. – nawfal Feb 18 '16 at 06:35
-
@nawfal yes but that was mentioned in a previous answer which this answer actually references – CodyBugstein Feb 18 '16 at 06:37
-
3@Imray no, that was not mentioned in any previous answer, which is the point. Leading to the right ticket is what the answerer does, which is great. It is different (and more helpful) from asking the OP to log a new ticket or search and find one himself. – nawfal Feb 18 '16 at 08:48
-
4Also star https://issuetracker.google.com/issues/79461369 since the originally linked one is marked "Fixed" despite the fix only applying to Sheets. – Kev Dec 11 '18 at 10:51
-
1
Very recently (April 2018) Google launched a macro recorder that includes a way to assign a keyboard shortcut to fire a macro and a way to import existing scripts as macros. See Google Sheets Macros
NOTE: This feature is currently being rolled out so it could take few weeks to be available for all. In my case it was available first on my personal Google account and since yesterday it is available on one of my G Suite accounts.

- 34,714
- 9
- 70
- 166
A solution has been posted over at issue 306! For the lazy, here it is:
The new IFRAME mode in HtmlService does allow for key codes to be passed on to Add-ons...
$(document).keydown(function(e){
//CTRL + V keydown combo
if(e.ctrlKey && e.keyCode == 86){
$( '#output' ).html("I've been pressed!");
}
})
Have to click on / activate the sidebar first for that to happen.

- 1,109
- 1
- 13
- 29
-
2The full code to implement it can be found [here](https://stackoverflow.com/a/44345522/3154274) – MagTun Jun 04 '17 at 11:12
-
Thank you so much! This really works in sheets. Now we can do any keyboard shortcut for any macro instead of only those by default: cumbersome Alt+Ctrl+Shift – Liker777 Feb 01 '20 at 14:03
Great news :) You can achieve custom keyboard shortcuts for google app script functions following next easy steps:
- In the Google Sheets UI, select Tools > Macros > Record Macro.
- Record any action. For instance, change the color background in A1. Press Save.
- Save it with a random title and the preferred shortcut. You can change it later.
- Select Tools > Script editor to open the script bound to the sheet in the Apps Script editor.
- In the new editor tab, select View > Show Manifest File
- TA - DÁ! :D Change the functionName value to the desired one ;)
Remember macro functions should take no arguments and return no values. More info at https://developers.google.com/apps-script/guides/sheets/macros#importing_functions_as_macros

- 117
- 1
- 7
-
This answer was already given months before in another answer to this question: https://stackoverflow.com/a/49946187 – Matthew Jul 18 '18 at 01:06
-
1Indeed, but IMO that answer does not clear if: 1. It is possible to create a custom keyboard shortcut to a google app script function in a google docs spreadsheet 2. How to do it – urpi5 Jul 24 '18 at 12:42
-
Hmm... shortcuts keys have to take the form option+cmd+shift+N where N is a number key - kinda lame. – Att Righ Jun 09 '21 at 16:12
Just updating, it is now possible the workaround that The Guy mentioned, trough IFRAME, you can create a sidebar, a STUB just to enter keyboard commands, treat them with jquery, and run the apropriate function, been using this already.

- 3,731
- 1
- 15
- 23
One possible work around for this would be to dedicate a column for "trigger text", define different text based triggers for each action you're trying to perform, and then create a function that checks the value and performs an action based on the "trigger text". You can then set an onEdit event trigger in the project's triggers under "Resources" in the script editor for your hotkey function.
The biggest downside to this approach is that it takes (at least for me) approximately 7 full seconds for the onEdit trigger to catch the change and perform the update. If you need something to process more quickly you may have to look for an alternate approach.
I've provided an example below for how to change row color based on trigger text in a designated "trigger" column. You can use this to do anything that can be done on the sheet via scripting such as change values, set font weight, copy data, or even run other functions after checking the trigger text input.
/*you will need to add an onEdit trigger to your project
for this to run when you edit the cell*/
//function to update row color using entered text in a specified "trigger" column
function hotKey(){
//get the cell you edited and the associated column and row number
var cell = sheet.getActiveCell();
var thisCol = cell.getColumn();
var thisRow = cell.getRow();
//set a range variable for the entire row
var colorRow = sheet.getRange(thisRow,thisCol,1,Cols);
//get the edited value for the cell as a string
var val = cell.getValue().toString();
//check that the edited cell is in the trigger column
if (thisCol = 1){
//update the row color based on the entered value
if(val == "g"){
colorRow.setBackground("#00ff00"); //sets row color to green
cell.clearContent(); //delete the trigger cell value
}else if(val == "r"){
colorRow.setBackground("#ff0000");
cell.clearContent();
}else if(val == "fd"){
colorRow.setBackground("#fff2cc");
cell.clearContent();
}else if(val == "pr"){
colorRow.setBackground("#ffff00");
cell.clearContent();
}else if(val == "cn"){
colorRow.setBackground("#6fa8dc");
cell.clearContent();
}
}
}

- 856
- 8
- 9
I'm struggling with a similar issue and tho I haven't got much resolved yet I think a way forward can be found thru this keypress event handler under Class Textbox
I don't know if this gets around the problem of server side only that Arun pointed out but I'm sure hoping so. Please feel free to correct my reasoning before I waste too much time trying this! :)

- 129
- 1
- 6
-
This method is part of UiApp service, it does not provide a solution for calling a function from within a document or a spreadsheet Ui. In UiApp or html service you can of course use keypress to trigger events – Serge insas Mar 21 '14 at 20:40
-
Ok. So is there anything then keeping the UiApp or html service (which I assume could be part of say an extension) from calling a macro written in Apps Script on Google Docs which could then perform the required steps that the keyboard shortcut would have done directly? – The Guy Mar 21 '14 at 23:26
-
nothing indeed... One can use such a schema with a sidebar... the ui in the sidebar would have buttons that can call script functions. Some new add-ons use that configuration in a very elegant way. – Serge insas Mar 21 '14 at 23:50
-
Thanks for the reply. I'm forced to wonder why answering the original question had to be like extracting a tooth. – The Guy Mar 24 '14 at 03:22
-
The UI Service and it's methods, including the referred keypress method are deprecated. – Rubén Oct 23 '17 at 18:38
Long story short: write an html service which uses a key press event handler to capture keypress events and compares them to the hotkey shortcuts you want then calls the appropriate sub routine on the server side in apps script to inline the called for keystrokes, format or document changes.
As far as viability goes, not as easy as the vba you are already familiar with but once you get past the different way of doing things not really that terribly difficult.

- 129
- 1
- 6
-
I don't think I get what you're talking about. Do you have a link to an article or some documentation or can you show an example? – User Apr 16 '14 at 14:13
-
1Sorry, no. I just read the related documents while studying a similar problem for a friend. Unfortunately I must admit that my answer now seems to be wrong. I can't get a confirmation of this but the nearest I can tell the ability to get a handle on the Docs text input panel is blocked. I can write a html service sidebar but I can't acquire the necessary object in order to create a keypress event handler for the main docs panel. I speculate that perhaps they are trying to prevent someone writing a keylogger or something. – The Guy Apr 18 '14 at 02:23