2014-02-21 85 views
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); 
} 

我想調用帳戶2中的腳本並傳遞日曆名稱作爲參數,在帳戶2中,腳本將以調用腳本和獲取某些事件的用戶身份運行如何從另一個Google腳本調用已發佈的Google腳本?腳本在不同的帳戶

回答

0

比方說,Script1是'客戶',或腳本調用'服務器',Script2。

在公佈SCRIPT2,進入菜單選項「文件>項目屬性」,和「項目的關鍵」屬性複製到SCRIPT1的「查找庫」對話框如下所述: https://developers.google.com/apps-script/guide_libraries#writingLibrary

你還需要暴露你想從SCRIPT1處理SCRIPT2功能,並通過在你的參數爲正常:

script1Var = Script2.myFunction(script1Param) 
0

你並不需要包含在URL中的參數使用引號,嘗試這樣的:

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

我假設你已經爲「服務器」應用程序正確設置了訪問權限。 –

+0

Serge,我忘了刪除最後一個字符串中的字符串,而且,我必須在帳戶1中共享日曆嗎?我測試了它並且響應文本相同,它看起來像一個驅動器網頁「<! DOCTYPE html> ... 」而不是帳戶2的日曆名稱,謝謝,問候 – user3325434

相關問題