Switch the focus from the sidebar to the document

81 views
Skip to first unread message

Sandeep Dhama

unread,
Aug 8, 2023, 1:37:59 PM8/8/23
to Google Apps Script Community
I wanna create sidebar in my google document. I wanna add some buttons to perform different tasks. When I click on any button or even sidebar focus from the document lost. Is this possible after performing the task focus move to the active document? If anyone can help please guide me.

Tanaike

unread,
Aug 8, 2023, 10:25:50 PM8/8/23
to Google Apps Script Community
I believe your goal is as follows.

- You want to move the focus from the sidebar to Google Document.

In this case, how about using "editor.focus()" of Class google.script.host?
https://developers.google.com/apps-script/guides/html/reference/host#editor.focus()

A sample script is as follows. Please copy and paste the following script to the script editor of Google Document and run "openSidebar" function. By this, a sidebar is opened. When you click "ok" button, the focus it moved from the sidebar to Google Document.

function openSidebar() {
  const html = HtmlService.createHtmlOutput('<input type="button" value="ok" onclick="google.script.host.editor.focus()">');
  DocumentApp.getUi().showSidebar(html);
}

If I misunderstood your goal, I apologize.

Sandeep Dhama

unread,
Aug 9, 2023, 4:48:54 AM8/9/23
to Google Apps Script Community
Perfect! thankyou so much @Tanaike. This is what I want. This works perfectly. Thankyou so much again @Tanaike

Tanaike

unread,
Aug 9, 2023, 7:56:38 AM8/9/23
to Google Apps Script Community
Thank you for replying. I'm glad your issue was resolved. Thank you, too.

Reply all
Reply to author
Forward
0 new messages