Search code examples
google-apps-scriptgoogle-sheetsonclickprompt

google.script.run.withSuccessHandler works for me but not other users


The following OnClick closes a DialogBox and its withSuccessHandler opens a UI.Prompt. This successfully closes the Starting UI for me but no other user. Why is this?

onClick="google.script.run.withSuccessHandler(function(){google.script.host.close();}).someOtherFunction()

Did I miss something about sharing my Excel App Scripts with other users?

Full Code here:

function onOpen() {
   var menu = SpreadsheetApp.getUi().createMenu("⚙️ Start App");
   menu.addItem("Start", "StartPopup");  
   menu.addToUi();
}

async function StartPopup(){

  var ui = SpreadsheetApp.getUi();
  
  var html = `
    <p>  App Modes </p> </center>
    <p class="body" style="font-family: sans-serif; color:gray; text-align:left">
    Options:</p>

    <body>

    <p>
    <input type="button" id="no" value="RUN" onClick="google.script.run.withSuccessHandler(function(){google.script.host.close();}).userPromptcustomFunction()"/>
    </p>
      </body>  ` 
  var htmlOutput = HtmlService
      .createHtmlOutput(html)
      .setWidth(700)
      .setHeight(600);


  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'My Excel App.');


}

function userPromptcustomFunction(){
  currentMonth = SpreadsheetApp.getUi().prompt("Choose Month to Perform Operation.", ui.ButtonSet.YES_NO);
}

Solution

  • Try this instead. I get the user input from the modal dialog and then send to the server.

    enter image description here

    function StartPopup(){
      try {
        var html = `
        <body>
          <p>  App Modes </p> </center>
          <p class="body" style="font-family: sans-serif; color:gray; text-align:left">
            Options:
          </p>
          <p>
            <label for="month">Choose Month to Perform Operation</label>
            <input type="text" id="month">
          </p>
          <p>
            <input type="button" id="no" value="RUN" onClick="buttonOnClick()"/>
          </p>
          <script>
            function buttonOnClick() {
              let month = document.getElementById("month").value;
              alert(month);
              google.script.run.withSuccessHandler( function () { google.script.host.close(); }).userPromptcustomFunction(month);
            }
          </script>
        </body>  `
        var htmlOutput = HtmlService.createHtmlOutput(html);
        SpreadsheetApp.getUi().showModalDialog(htmlOutput,"My Google App Script");
      }
      catch(err) {
        Logger.log(err);
      }
    }
    
    function userPromptcustomFunction(month) {
      try {
        let prop = PropertiesService.getUserProperties();
        prop.setProperty("month",month);
        Logger.log("montn = "+prop.getProperty("month"));
      }
      catch(err) {
        Logger.log(err);
      }
    }
    

    Execution log

    Head    userPromptcustomFunction    Unknown Oct 11, 2022, 9:03:47 AM    1.426 s 
    Completed
    Cloud logs
    Oct 11, 2022, 9:03:49 AM    Info    montn = Jan