Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • 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 published with execute as me permissions (owner).

At the very least I want to be able to pass Session.getActiveUser.getEmail() from script A to script B.

This is what I have so far...

Script A

// Script-as-app template.
function doGet() {
  var app = UiApp.createApplication();

  var button = app.createButton('Click Me');
  app.add(button);

  var handler = app.createServerHandler('myClickHandler');
  button.addClickHandler(handler);

  return app;
}

function myClickHandler(e) {  
  var url = "https://script.google.com/macros/s/AKfycbzSD3eh_SDnbA4a7VCkctHoMGK8d94SAPV2IURR3pK7_MwLXIb4/exec";
  var payload = { 
    name : "Gene",
    activeUser : Session.getActiveUser().getEmail(),
    time : new Date()
  };

  var params = { 
    method : "post",
    payload : payload
  }
  Logger.log("Hello World!");

  var HTTPResponse;

  try{
    HTTPResponse = UrlFetchApp.fetch(url, params);
  }catch(e){
    Logger.log(e);
  }
  return HTTPResponse;
}

Script B

function doPost(e){
  if(typeof e === 'undefined')
    return;

  var app = UiApp.createApplication();
  var panel = app.add(app.createVerticalPanel());
  for(var i in e.parameter){
    panel.add(app.createLabel(i + ' : ' + e.parameter[i]));
    Logger.log(i + ' : ' + e.parameter[i]); 
  }

  ScriptProperties.setProperty('Donkey', 'Kong');

  return app;
}

output

Going to script A here the page loads the button. Clicking the button causes "Hello World!" to be logged in Script A's project log but the log of Script B's project remains empty. TryCatch does not log any error.

like image 834
Gene Avatar asked Feb 01 '14 13:02

Gene


People also ask

How does onEdit work?

The onEdit(e) trigger runs automatically when a user changes the value of any cell in a spreadsheet. Most onEdit(e) triggers use the information in the event object to respond appropriately. For example, the onEdit(e) function below sets a comment on the cell that records the last time it was edited.

How do I handle GET and POST HTTP requests in Google Apps Script?

Handle POST Requests with Google ScriptsThe callback function doPost is invoked when an HTTP POST request is make to your Google Script URL that is published as a web app with anonymous access. const doPost = (request) => { console. log(request); return ContentService. crateTextOutput(JSON.


1 Answers

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.

like image 63
Harold Avatar answered Sep 28 '22 07:09

Harold