如何将参数从一个Google-Apps-Script传递到另一个并执行? [英] How to pass parameters from one Google-Apps-Script to another and execute?

查看:190
本文介绍了如何将参数从一个Google-Apps-Script传递到另一个并执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 目标是将数据从Google Apps脚本A传递到Google Apps脚本
    B.

  • 脚本A以execute为用户权限发布。

  • 脚本B以执行权限(所有者)的形式发布。



至少我希望能够将脚本A中的 Session.getActiveUser.getEmail()传递给脚本B.

这是我迄今为止...



脚本A



  //脚本作为应用程序模板。 
函数doGet(){
var app = UiApp.createApplication();

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

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

返回应用;
}

函数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;

尝试{
HTTPResponse = UrlFetchApp.fetch(url,params);
} catch(e){
Logger.log(e);
}
返回HTTPResponse;

$ / code>



脚本B



 函数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');

返回应用;
}



输出



去脚本此处页面加载按钮。点击按钮会导致Hello World!记录在脚本A的项目日志中,但脚本B的项目日志保持空白。
TryCatch不记录任何错误。

解决方案

我相信你的问题是由于你试图通过响应参数uiapp元素。
这里是html服务中脚本的一些变体。

demo
a>

脚本:

  // ####部分
函数doGet (e){
var html =< input type ='text'id ='text'/>< input type ='button'onclick ='myClick()'value ='submit'> ; //要传递给脚本的文本B
html + =< div id ='output'>< / div>; //显示脚本B的地方回答
html + =< script>;
html + =function myClick(){google.script.run.withSuccessHandler(showResults).myClickHandler(document.getElementById('text')。value);}; //处理程序在脚本中执行作业A
html + =function showResults(result){document.getElementById('output')。innerHTML = result;}< / script>; //函数显示urlfetch的结果(脚本B的响应)
return HtmlService.createHtmlOutput(html);



函数myClickHandler(text){
var url = ScriptApp.getService()。getUrl();
var payload = {
name:Gene,
text:text,
time:new Date()
};
$ b var params = {
method:post,
payload:payload
}
Logger.log(text:+ text);

var HTTPResponse;

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

// ###### B部分
函数doPost(e){
if(typeof e ==='undefined'){
回报e是空的!!;
}
var htmlOut =< ul>;
for(var i in e.parameter){
htmlOut + =< li>+ i +:+ e.parameter [i] +< / li>;
if(i ==text){
htmlOut + =< li>文本哈希值:+ Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5,e.parameter [i] ))+ < /锂> 中;
}
}
htmlOut + =< / ul>;
返回ContentService.createTextOutput(htmlOut);
}

请注意,您将无法获得记录器事件的脚本B(因为它是在你不在的时候触发的 - 你不是那个触发脚本B的人)。这是触发器脚本B的脚本A,当它生成一个urlfetch时脚本A未被识别为你 )。如果你想得到脚本b记录器的结果,你应该把它返回给脚本a。
重要的是要注意:当脚本A对脚本B执行UrlFetch时,它不会被标识为你,因此脚本B必须接受由任何人打开(在谁有权访问该应用程序:你需要选择任何人甚至匿名)。

注意:我把所有内容放在同一个脚本中(你可以把它分成两个不同的脚本,它不是一个问题),因为B部分需要匿名访问,所以我无法自动检索部分A中的电子邮件地址,所以我改变了一下这里做了什么。


  • 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.

解决方案

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.

这篇关于如何将参数从一个Google-Apps-Script传递到另一个并执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆