2017-04-24 10 views
2

메일을 보내야하는 일정 및 수신자 배열이 있습니다. 그때 나는 다른 해결책은있다, 난이 PARAMS (받는 사람)google-app-script에서 트리거 된 함수에 매개 변수를 전달하는 방법

function send_mail(recipients) 
{ 
    var id = DocumentApp.getActiveDocument().getId(); 
    for(i=0;i<recipients.length;i++) 
{ 
    var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html"; 
    var param = 
    { 
     method  : "get", 
     headers  : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}, 
     muteHttpExceptions:true, 
    }; 
    var html = UrlFetchApp.fetch(url,param).getContentText(); 
    var raw = Utilities.base64EncodeWebSafe("Subject: Test\r\n" + 
             "To:" +recipients[i]+"\r\n" + 
             "Content-Type: text/html; charset=UTF-8\r\n\r\n" + 
             html+"\r\n\r\n"); 
    var message = Gmail.newMessage(); 
    message.raw = raw; 
    var sentMsg = Gmail.Users.Messages.send(message, recipients[i]); 
} 
} 
function schedule_mail(recipients,schedule) 
{ 
    ScriptApp.newTrigger("send_mail").timeBased().at(schedule).create(); 
} 

PARAMS는 시간 기반 트리거에 전달 될 수없는 경우를 통과 할 수있는에 트리거를 만들기 위해 어떻게든지있을 경우 아는 열망?

+0

트리거에 매개 변수를 전달할 수 있다고 생각하지 않습니다. [PropertiesService] (https://developers.google.com/apps-script/reference/properties/properties-service) 사용을 고려해 보셨습니까? –

+0

PropertiesService에 변수를 저장하는 경우 트리거를 관리하는 방법 (트리거를 작성해야 할 때와 실행 후 트리거를 삭제하는 방법) @ Pierre-MarieRichard –

+0

트리거를 생성하고 변수를 PropertiesService에 저장합니다. 그런 다음 함수에 코드를 추가하여 PropertiesService에서 변수를 가져올 수 있습니다. 함수가 완료되면 트리거를 지 웁니다. function delTrigger() { var triggers = ScriptApp.getProjectTriggers(); for (var i = 0; i

답변

1

직접적인 방법은 없습니다. 하지만 여기에 해결 방법이 있습니다. 참고 : 이것은 실제 코드가 아닌 지시 사항 일뿐입니다. 코드에서 내 의견을 확인하십시오.

function send_mail(recipients) 
{ 
    ............ 
    ............ 
} 

function triggerHandler(e){ 
    var triggerId = e.triggerUid; 
    //Iterate through all triggers and get the matching triggerId and corresponding recipient 
    send_mail(recipient); 
} 

function schedule_mail(recipients,schedule) 
{ 
    var trigger = ScriptApp.newTrigger("triggerHandler").timeBased().at(schedule).create(); 
    var ID = trigger.getUniqueId(); 
    //Save this ID against the recipient details in user properties 
} 
+0

감사합니다. @ 하리 다스. 해결 방법은 다음과 같습니다. 괜찮 았어. –

+0

"e"가 무엇인지 궁금해하는 사람들에게는 함수 매개 변수가 아닌가? 다음 페이지에서 '이벤트 개체'를 확인하십시오. https://developers.google.com/apps-script/guides/triggers/events 요점은 다음과 같습니다. > 트리거가 실행되면 Apps Script는 이벤트 객체를 인수로 전달합니다. 일반적으로 e라고합니다. 이벤트 객체에는 트리거를 시작한 컨텍스트에 대한 정보가 들어 있습니다. – edelans