How to pass parameters from one Google-Apps-Script to another and execute?

后端 未结 1 1912
独厮守ぢ
独厮守ぢ 2020-12-17 02:06
  • Goal is to pass data from Google Apps Script A to Google Apps Script B.
  • Script A is published with execute as user permissions.
  • Script B is publi
相关标签:
1条回答
  • 2020-12-17 02:40

    I believe your problem is due that you try to pass as a response argument a uiapp element. here a little variation of your script in html service.
    the demo
    the script:

    // #### Part A
    function doGet(e) {
      var html ="<input type='text' id='text' /><input type='button' onclick='myClick()' value='submit'>"; // a text to be passed to script B
      html+="<div id='output'></div>"; // a place to display script B answer
      html+="<script>";
      html+="function myClick(){google.script.run.withSuccessHandler(showResults).myClickHandler(document.getElementById('text').value);}"; // handler to do the job in script A
      html+="function showResults(result){document.getElementById('output').innerHTML = result;}</script>"; // function to show the result of the urlfetch (response of script B)
      return HtmlService.createHtmlOutput(html);
    }
    
    
    function myClickHandler(text) {  
      var url = ScriptApp.getService().getUrl();
      var payload = { 
        name : "Gene",
        text : text,
        time : new Date()
      };
    
      var params = { 
        method : "post",
        payload : payload
      }
      Logger.log("text: "+text);
    
      var HTTPResponse;
    
      try{
        HTTPResponse = UrlFetchApp.fetch(url, params);
      }catch(e){
        Logger.log(e);
      }
      return HTTPResponse.getContentText();
    }
    
    // ###### Part B
    function doPost(e){
      if(typeof e === 'undefined'){
        return "e was empty!!"; 
      }
      var htmlOut="<ul>"; 
      for(var i in e.parameter){
        htmlOut+="<li>"+i+ " : " + e.parameter[i]+"</li>";
        if(i=="text"){
          htmlOut+="<li> Text hash : "+Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, e.parameter[i]))+"</li>";
        }
      }
      htmlOut+="</ul>";
      return ContentService.createTextOutput(htmlOut);
    }
    

    It is important to note that you won't have the ability to get the logger events of the script B (because it is triggered when you are not there - you are not the one who trigger script B. this is script A that's trigger script B and script A is not identified as "you" when it make a urlfetch). If you want to get the result of the script b logger you should return it to the script a. It's important to note: again, when script A do the UrlFetch to script B it is not identified as "you" so the script B must accept to be opened by anyone (in the publish option under "Who has access to the app:" you need to select anyone even anonymous).

    NB: i put everything in the same script for commodity (you can split that in two differents script it's not a problem) and because B part need to be accessed to anonymous persons I can't retrieve automatically the email adress in the part A so I changed a little bit what was done here.

    0 讨论(0)
提交回复
热议问题