5

I am writing a simple script for a Google Sheet. I have a button labeled "New Entry" (the button is actually a drawing with my script assigned to it). When the user clicks the button, the script inserts an empty row at a specified location and activates the first column so that the user can begin typing.

Everything works great except that after the script finishes, the keyboard doesn't work until after the user manually clicks in the sheet. The correct cell is visibly activated, but I suspect the focus is still (secretly) on the drawing. This theory is supported by the fact that if the user hits the escape key, he can then begin typing in the active cell as desired.

Is there any way to automate this final step so that the user does not need to hit ESC or click into the sheet before begining to type?

Here is the relevant line of code I am using:

this_sheet.getRange(new_row_num, start_col + 1).activate();   // activate the appropriate cell for convenient data entry

I have tried a few other functions in combination with and/or instead of the line of code shown above, but always with the same result. These other functions include Sheet.activate() and Spreadsheet.setActiveSelection(range).

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Andrew
  • 61
  • 5
  • 1
    Welcome to Stack Overflow! You might want to check out [how to ask a question](http://stackoverflow.com/help/how-to-ask). Including your existing code would be a good start. – Gary Storey Jun 17 '15 at 16:46
  • @GaryStorey thanks for the welcome! I've added some code and some additional details in accordance with the how-to link. Sorry for the somewhat bumbled question--I hope this helps! – Andrew Jun 18 '15 at 17:42
  • I think I Have a similar problem, and can't get to work it right: – maruda Oct 06 '17 at 10:36

1 Answers1

2

If these functions are not working the way as desired, then a workaround to get focus would be to write to the target cell using

this_sheet.getRange(new_row_num, start_col + 1).setValue("");//for blank cell

and if it is not a blank cell but having some existing value then you can use getValue() first and then setValue() so as to keep the same value.

Fi Teach
  • 88
  • 9
  • Thanks for the response and sorry for the delay--I'm just seeing this now! Unfortunately, this workaround doesn't work for me. The focus still seems to be on the button (drawing) that the user clicked. I still have to hit escape to actually type anything into the activated cell. – Andrew Feb 28 '17 at 14:20
  • @Andrew I have just completed a script a couple of days ago where it works pretty fine even with `range.activate()` method. So, it looks that either you need to share the full code or find as if some other statement is executing after that, causing the focus to go back to drawing. – Fi Teach Feb 28 '17 at 18:28
  • Same Problem, no matter what I try to do, if the macro is run by clicking a button, then I have to click on the cell before I can edit it, I cannot even use the F2 or arrow keys to move the actively selected cell. the EXACT same macro run with a keyboard shortcut works absolutely fine! I have tried at least 20 different versions, and have even asked chatgpt (which eventually gave up too!) I will post my full code, not sure if I can do it in the comments or I have to start a new answer... – johnvdenley Apr 04 '23 at 15:43
  • AH-HA I found the answer at https://stackoverflow.com/questions/57859071/cant-edit-field-without-clicking-after-a-script-run Well I say "answer" but really its simply a "no it cant be done" BUT you can just press "ESC" and then start typing and that will have the same effect! -seems like Google need to fix this as it appear to be a bug IMHO – johnvdenley Apr 04 '23 at 15:46