2015-01-26 3 views
0

내 이전 게시물 here의 확장 기능,이 기능은 더 구체적이고 자신의 게시물이어야한다고 생각했습니다.매 시간마다 실행되는 timeBased 애드온 트리거는 어떻게 만듭니 까?

추가 기능에는 트리거에 대한 제한이 있습니다. 하나는 한 시간에 한 번만 실행할 수 있다는 것입니다. 나는 그 일을하는 방법을 알아낼 수 없다.

아래 스크립트를 실행하면 추가 기능으로 실행될 때 "허용되지 않는 작업을 수행하려고 시도했습니다"라는 메시지가 표시됩니다 (). 따라서 아래의 스크립트가 한 시간에 한 번씩 스크립트에 적합한 애드온 방법이 아닌 경우 버그가 무엇인지, 또는 찾았습니까?

ScriptApp.newTrigger('updateDay').timeBased().everyHours(1).create(); 

나는 스펜서가 제안 인증 검사를 추가하는 시도, 및 설명서 here에 설명했다. 인증을 통과하지만 여전히 동일한 오류가 발생합니다.

function installTrigger(e) { 
    var addonTitle = 'Lab Scheduler'; 
    var props = PropertiesService.getDocumentProperties(); 
    var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL); 
    if (authInfo.getAuthorizationStatus() == 
     ScriptApp.AuthorizationStatus.REQUIRED) { 
    var lastAuthEmailDate = props.getProperty('lastAuthEmailDate'); 
    var today = new Date().toDateString(); 
    if (lastAuthEmailDate != today) { 
     if (MailApp.getRemainingDailyQuota() > 0) { 
     var html = HtmlService.createTemplateFromFile('AuthorizationEmail'); 
     html.url = authInfo.getAuthorizationUrl(); 
     html.addonTitle = addonTitle; 
     var message = html.evaluate(); 
     MailApp.sendEmail(Session.getEffectiveUser().getEmail(), 
          'Authorization Required', 
          message.getContent(), { 
          name: addonTitle, 
          htmlBody: message.getContent() 
          } 
         ); 
     } 
     props.setProperty('lastAuthEmailDate', today); 
    } 
    } else { 
    // Authorization has been granted, so continue to respond to the trigger. 
    try{ 
     var as = SpreadsheetApp.getActiveSpreadsheet(); 
     var userTriggers = ScriptApp.getUserTriggers(as); 
     var userTriggerL = userTriggers.length; 
     if (userTriggers.length == 0){ 
     ScriptApp.newTrigger('updateDay').timeBased().everyHours(1).create(); 
     } 
    } catch(err){ 
     catchToString_(err); 
    } // End try catch 
    } 
} 
+0

AddOn은 설치 또는 사용 중 하나의 상태 일 수 있습니다. [Google 문서 - 설치 모드] (https://developers.google.com/apps-script/add-ons/lifecycle#installed_versus_enabled) 애드온이 활성화 된 모드에서만 설치되면 ** AuthMode.LIMITED **에서 실행됩니다. 인증 모드. AddOn을 사용하는 사용자는 어떤 상태입니까? –

답변

0

난 당신이 날 (예 : 월요일)과 ATHOUR를 지정해야합니다 귀하의 예제에서, 그래서 당신은 매주 월요일을 실행하는 (1) 트리거를 만들 것, 특정 시간에 특정 일 동안 트리거를 사용하는 생각 1.

얼마나 자주 트리거해야하는지 지정하려면 ScriptApp.newTrigger ('myFunction'). timeBased()를 작성해야합니다. everyHours (1) .create();

+0

ScriptApp.newTrigger ('updateDay'). timeBased(). everyHours (1) .create(); 애드온으로 실행될 때도 여전히 동일한 오류가 발생합니다. –

+0

ScriptApp.newTrigger ("updateDay"). timeBased(). everyDays (1) .atHour (1) .create(); 또한 동일한 오류가 발생합니다. 이거 버그 야? –

0

실행중인 문제는 트리거가 생성되거나 실행될 때 추가 기능이 실행되는 범위입니다. 설치된 트리거는 AuthMode.FULL에서 실행됩니다. 트리거를 실행하기 전에 현재 권한 부여 레벨을 테스트해야합니다. 당신은 부가가 실행되는 authMode의 상태를 얻기 위해 ScriptApp.getAutorizationInfo (authMode)를 사용

여기 https://developers.google.com/apps-script/reference/script/script-app#getAuthorizationInfo(AuthMode)

는 애플리케이션 스크립트 문서에서 코드의 예 비트이다. https://github.com/googlesamples/apps-script-form-notifications-addon/blob/master/Code.gs

var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL); 

    // Check if the actions of the trigger require authorizations that have not 
    // been supplied yet -- if so, warn the active user via email (if possible). 
    // This check is required when using triggers with add-ons to maintain 
    // functional triggers. 
    if (authInfo.getAuthorizationStatus() == 
    ScriptApp.AuthorizationStatus.REQUIRED) { 
    // Re-authorization is required. In this case, the user needs to be alerted 
    // that they need to reauthorize; the normal trigger action is not 
    // conducted, since it authorization needs to be provided first. Send at 
    // most one 'Authorization Required' email a day, to avoid spamming users 
    // of the add-on. 
sendReauthorizationRequest(); 
    } else { 
    // All required authorizations has been granted, so continue to respond to 
    // the trigger event. 
    } 
+0

난 그냥 내 코드에 추가하고 추가 기능으로 실행하면 인증을 통과하지만 timeBased 트리거를 설치하려고하면 실패합니다. .timeBased() 트리거는 애드온으로 허용되지 않습니까? 나는 그들이 줄 알았는데? –

관련 문제