How do I get the title I set through HtmlOutput's setTitle from the html side?

94 views
Skip to first unread message

Rez GodarzvandChegini

unread,
Aug 26, 2021, 3:19:33 AM8/26/21
to Google Apps Script Community
Hello!
I want to fetch the title I set using setTitle method of HtmlOutput within the html source file, i.e., client-side, but it seems that calling document.title returns me empty string when I call it within my html file.
  • Main.gs:
function onOpen() {
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Build Form...', functionName: 'buildForm_'},
  ];
  spreadsheet.addMenu('Test', menuItems);
}

function buildForm_(reportTitle){
  const htmlForm = HtmlService.createHtmlOutputFromFile('HTML_Sidebar')
      .setTitle(reportTitle);
  SpreadsheetApp.getUi()
      .showSidebar(htmlForm)
}
  • HTML_Sidebar.html:
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <button onmouseup="seeTitle()" id="seeTitleButton">See title</button>
  <script>
    function seeTitle() {
      document.getElementById("seeTitleButton").textContent = "(" + document.title + ")"
    }
    </script>
  </body>
</html>

Google Apps Script's documentation doesn't seem to specify a client-side method in the HTML Service to query the title, and I was hoping that someone in the community could point me towards the right direction.

Alan Wells

unread,
Aug 26, 2021, 11:07:04 AM8/26/21
to Google Apps Script Community
From the client side you need to use the DOM, (Document Object Model) which is not JavaScript.

Clark Lind

unread,
Aug 26, 2021, 1:30:38 PM8/26/21
to Google Apps Script Community
Since you are effectively creating a Workspace/Editor Add-on, you might be able to do what you need via the supplied triggers. 

On Thursday, August 26, 2021 at 3:19:33 AM UTC-4 Rez GodarzvandChegini wrote:
Reply all
Reply to author
Forward
0 new messages