0
//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);
 }

I want to call the script in the account 2 and pass a calendar name as parameter, in the account 2, the script will run as the user who call the script and get some events

2 Answers2

0

Let's say Script1 is the 'client', or the script calling the 'server', Script2.

In the published Script2, go to the menu option 'File > Project properties', and copy the 'Project key' property into the 'Find a library' dialog of Script1 as described here: https://developers.google.com/apps-script/guide_libraries#writingLibrary

You will also need to expose the Script2 functions you want to handle from Script1, and pass in your arguments as normal:

script1Var = Script2.myFunction(script1Param)
Tim
  • 756
  • 1
  • 7
  • 12
0

You don't need to use quotes in the parameters included in your URL, try like this:

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

and also :

why are you writing e.parameter.string.calendar in the last tine of your sample code ? What is string for ? I guess the purpose is to return something else than just the calendar name ... please explain.

here is a working demo where I removed your specific function calls and use a public agenda. it returns only the name of the calendar in the logger.

//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);
 }
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • I assume you have set the access rights correctly for the "server" app of course. – Serge insas Feb 21 '14 at 06:43
  • Serge, i forgot delete the string word in the last tine, and , do i have to share the calendar in the account 1?,i tested it and the response text is the same, it looks like a drive web page " ... " and not the calendar name from the account 2, thanks, regards – user3325434 Feb 22 '14 at 19:03