2014-05-01 3 views
1

Google 캘린더에서 약속을 가져 와서 Google 시트로 복사하고 XLS로 변환 한 다음 이메일을 보내려면 Google Apps Script를 사용했습니다. 이번 주까지는 괜찮 았어.Google Apps 스크립트에서 XLS로 시트를 내보낼 때 OAuth 오류가 발생했습니다.

처음에는 새 버전의 스프레드 시트로 인해 302 오류가 발생했습니다. 여기에 설명되어 있습니다 : Export (or print) with a google script new version of google spreadsheets to pdf file, using pdf options

HTTP 예외를 음소거하고 그에 따라 URL을 조정하여 파일의 새 위치를 얻었습니다. 또한 제안 된대로 OAuth 범위를 https://docs.google.com/feeds/으로 업데이트했습니다.

프로그램이 "OAuth 오류"메시지와 함께 실패합니다. muteHttpExceptions가 true로 설정되면 메시지는 "서비스 인증을받지 못했습니다 : google"입니다.

이것이 범위 문제라고 생각하지만 실제로 잘못한 것을 볼 수 없습니다. 당연히, 나는 운없이 다른 가능성을 시도했다.

아래 코드를 포함 시켰습니다. 주석 처리 된 코드는 이번 주까지 작업 한 수업입니다.

function getSSAsExcel(ssID) 
{ 
    var format = "xls"; 

    //var scope = "https://spreadsheets.google.com/feeds/"; 
    var scope = "https://docs.google.com/feeds/"; 
    var oauthConfig = UrlFetchApp.addOAuthService("google"); 
    oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 
    oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=" + scope); 
    oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); 
    oauthConfig.setConsumerKey("anonymous"); 
    oauthConfig.setConsumerSecret("anonymous"); 

    var requestData = { 
    //"muteHttpExceptions": true, 
    "method": "GET", 
    "oAuthServiceName": "google", 
    "oAuthUseToken": "always" 
    }; 

    //var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + ssID 
    var url = "https://docs.google.com/spreadsheets/d/" + ssID 
     + "/feeds/download/spreadsheets/Export?" 
     + "&size=A4" + "&portrait=true" +"&fitw=true" + "&exportFormat=" + format; 

    var result = UrlFetchApp.fetch(url , requestData); 
    var contents = result.getContent(); 

    return contents; 
} 

감사합니다.

+0

최신 답변은 다음과 같습니다. http://stackoverflow.com/questions/31809987/google-app-scripts-email-a-spreadsheet-as-excel –

답변

2

대신 OAuthConfig (스크립트 편집기에서 인증해야 함) 대신 ScriptApp.getOAuthToken()을 통해 검색 가능한 OAuth2 토큰을 전달할 수 있습니다. (단순히 어딘가에 DriveApp.getRootFolder() 호출을 포함

아래의 코드 단편은 수출 URL을 얻을 수있는 Advanced Drive service를 사용하지만, 당신이 손 URL을 구성하는 경우는 드라이브 범위가 여전히 스크립트에 의해 요청되어 있는지 확인해야합니다 당신의 스크립트 코드).

function exportAsExcel(spreadsheetId) { 
    var file = Drive.Files.get(spreadsheetId); 
    var url = file.exportLinks[MimeType.MICROSOFT_EXCEL]; 
    var token = ScriptApp.getOAuthToken(); 
    var response = UrlFetchApp.fetch(url, { 
    headers: { 
     'Authorization': 'Bearer ' + token 
    } 
    }); 
    return response.getBlob(); 
} 
+1

다른 내보내기 유형에 대한 참조는 [link] {https://developers.google.com/drive}입니다./web/integrate-open # open_and_convert_google_docs_in_your_app}. 드라이브 API를 사용하도록 설정하는 방법은 [link] {https://developers.google.com/apps-script/guides/services/advanced}입니다. – user3591689

+0

감사합니다. @ Eric Koleda, 3 세, 내가 어떻게 부탁 할 수 있습니까? 나는 실제로 수출하는 기능을 보지 못했다. 나는 그것이 얼룩을 돌려주는 것을 본다. –

관련 문제