Home > Mobile >  How to add text to an open Dialog window?
How to add text to an open Dialog window?

Time:10-27

I am working on a Google Sheets macro that displays some text to the user, then presents some buttons for the user to interact with. The buttons run another function and I am struggling with how to have the button display the text to the user.

I can't find the method or object I am supposed to use to grab the currently open window and edit the html to add more text. Or if that isn't possible, how can I close the open window and then display a new window that also has the old text?

function example(text,title,height=90,width=350) {
  const dialogbutton = '<br><input type="button" value="Do Stuff" onClick="google.script.run.doStuff();" />'
  var html=HtmlService.createHtmlOutput(text dialogbutton).setHeight(height).setWidth(width)
  SpreadsheetApp.getUi().showModelessDialog(html, title);
}
function doStuff() {
  const dialogWindow = ???//I am hoping to retrieve the open window as an object
  const text = getText() //run some other function and get the new text to insert
  dialogWindow.displayedText  = text //modify the displayed window to add the new text
}

CodePudding user response:

Here is a very simple example of how to communicate with the server (i.e. Spreadsheet or Doc). In this case a spreadsheet with Sheet1!A1 = hello

Here is a simple dialog

enter image description here

Server side code Code.gs bound to a spreadsheet

function showTest() {
  var html = HtmlService.createTemplateFromFile("HTML_Simple");
  html = html.evaluate();
  SpreadsheetApp.getUi().showModalDialog(html,"Test");
}

function doStuff() {
  try {
    // let get a value from spreadsheet
    let spread = SpreadsheetApp.getActiveSpreadsheet();
    let sheet = spread.getSheetByName("Sheet1");
    return sheet.getRange("A1").getValue();
  }
  catch(err) {
    Logger.log("Error in doStuff() " err);
  }
}

HTML Page HTML_Simple.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <input type="button" value="Do Stuff" onClick="doStuffOnClick()">
    <input type="text" id="whatStuff">
    <script>
      function doStuffOnClick() {
        try {
          google.script.run.withSuccessHandler(
            function(response) {
              document.getElementById("whatStuff").value = response;
            }
          ).doStuff();
        }
        catch(err) {
          alert(err);
        }
      }
    </script>
  </body>
</html>

Reference

  • Related