如何从其他谷歌脚本调用已发布的谷歌脚本?脚本在不同的帐户中

时间:2014-02-21 02:59:49

标签: google-apps-script call communicate

//script calling the published script, this script is in the account 1
function callScript(){
    try{//published url
       var response =UrlFetchApp.fetch("https://script.google.com/macros/s/../exec?  calendar='CalendarName'");//calendar name as parameter

    }catch(e){
  Logger.log(e);
 }
 Logger.log(response.getContentText());
}

//account 2
//the code is published, it will run as the user who call the script(account 1), the   access is for anyone 
function doGet(e){//published script

  var app = UiApp.createApplication();
  var calendar=e.parameter.calendar; //calendar name as parameter
  // Determines how many events are happening now 
  var now = new Date();
  var oneMinuteFromNow = new Date(now.getTime() + (60 * 1000));
  var events = CalendarApp.getCalendarsByName(calendar)  [0].getEvents(now,oneMinuteFromNow);
  var email=Session.getActiveUser().getEmail();
  for(i in events){
     var tituloEvento=events[i].getTitle();
     var descEvento= events[i].getDescription();
     var insertar=leerBD(email,tituloEvento,descEvento);
     if (insertar) {
       var inserto=insertarBD(email,tituloEvento,descEvento); 
     } 
  }  
  return ContentService.createTextOutput(e.parameter.string.calendar);
 }

我想调用帐户2中的脚本并将日历名称作为参数传递,在帐户2中,脚本将作为调用脚本并获取某些事件的用户运行

2 个答案:

答案 0 :(得分:0)

假设Script1是'客户端',或称为'服务器'的脚本,Script2。

在已发布的Script2中,转到菜单选项'文件>项目属性',并将'Project key'属性复制到Script1的'Find a library'对话框中,如下所述: https://developers.google.com/apps-script/guide_libraries#writingLibrary

您还需要从Script1公开要处理的Script2函数,并照常传递参数:

script1Var = Script2.myFunction(script1Param)

答案 1 :(得分:0)

您不需要在网址中包含的参数中使用引号,请尝试以下操作:

   var response =UrlFetchApp.fetch("https://script.google.com/macros/s/../exec?calendar=CalendarName");//calendar name as parameter

还有:

为什么要在示例代码的最后一刻写e.parameter.string.calendar?什么是string?我想目的是返回别的东西,而不仅仅是日历名称...请解释。

这是一个工作演示,我删除了您的特定函数调用并使用公共议程。 它只返回记录器中日历的名称。

//script calling the published script, this script is in the account 1
function callScript(){
    try{//published url
       var response =UrlFetchApp.fetch("https://script.google.com/macros/s/AKfycbxg-XiPOSIzTATNO520r2DYqLWFjSJXIZ4h-9G_4eV4UZTgxnjo/exec?calendar=test_agenda");//calendar name as parameter

    }catch(e){
  Logger.log(e);
 }
 Logger.log(response.getContentText());
}

//account 2
//the code is published, it will run as the user who call the script(account 1), the   access is for anyone 
function doGet(e){//published script

  var app = UiApp.createApplication();
  var calendar=e.parameter.calendar; //calendar name as parameter
  // Determines how many events are happening now 
  var now = new Date();
  var oneMinuteFromNow = new Date(now.getTime() + (60 * 1000));
  var events = CalendarApp.getCalendarsByName(calendar)  [0].getEvents(now,oneMinuteFromNow);
  var email=Session.getActiveUser().getEmail();
  for(i in events){
     var tituloEvento=events[i].getTitle();
     var descEvento= events[i].getDescription();
  }  
  return ContentService.createTextOutput(e.parameter.calendar);
 }