2

I'm building a Google Sheet's add-on, for which I require to get the user to select a range using a modeless dialog, which then would be used by some other functions.

I've managed to get this solution to work correctly, but don't like the user experience of needing to click on a button to get the range copied to the input box.

Is there any way I can get users to input the range with their mouse, similar to what happens when want to create a chart using pure Google Sheets? Here's a video of the intended functionality.

Thanks!

albert
  • 31
  • 3
  • [Perhaps onSelectionChange](https://developers.google.com/apps-script/guides/triggers) could work for you – Cooper Sep 02 '20 at 03:15
  • But that would run server side, right? How can I keep pushing that information to the client without requesting it from the client with google.script.run? – albert Sep 02 '20 at 04:18

1 Answers1

1

You can do it with Web Polling, that is with setInterval()

Sample:

code.gs

function dialog()//run me
{
  var html=HtmlService.createHtmlOutputFromFile('index');
  SpreadsheetApp.getUi().showModelessDialog(html, 'Select a range');
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <script>
  function onSuccess(range){
    console.log("onSuccess called");
    document.getElementById("range").innerHTML=range;
  }
  function polling(){
  console.log("polling called");
    setInterval( function(){google.script.run.withSuccessHandler(onSuccess).getValues()},500);
   }    
  </script>
  <body onload="polling()">
    <div> Range: </div>
    <div id="range">
    </div>
  </body>
</html>
ziganotschka
  • 25,866
  • 2
  • 16
  • 33