google-apps-scriptweb-applications

Apps Script parameters with script.google.run


With Apps Script, it's not allowed to use iframes or AJAX calls. So, I was hoping to restructure my script. Basically, it's just supposed to be a single file chooser.

function doGet(e)
{
  return HtmlService.createHtmlOutputFromFile('index');
}


function browse(e)
{
  var use_root_folder = typeof(e.parameter.folder_id) == "undefined";

  var base_folder;
  if(use_root_folder)
  {
    base_folder = DriveApp.getRootFolder();
  }
  else
  {
    base_folder = DriveApp.getFolderById(e.parameter.folder_id);
  }
  //more code...
}

In sample code on apps script, it looks like I'm supposed to create an html file and put this in it...

<script>
  google.script.run.browse();
</script>

Except the "e" argument is only available from within the actual script and not the html file? How would I go about passing a query string parameter from within the HTML file back to the script?


Solution

  • When using google.script.run, you pass variables directly. So you can simply call browser() with the ID of the folder you want.

    In the script file:

    function browse(folderID)
    {
      var use_root_folder = typeof(folderID) == "undefined";
    
      var base_folder;
      if(use_root_folder)
      {
        base_folder = DriveApp.getRootFolder();
      }
      else
      {
        base_folder = DriveApp.getFolderById(folderID);
      }
      //more code...
    }
    

    And in the HTML file:

    <script>
      google.script.run.browse(selectedFolderId);
    </script>