2012-09-25 35 views
0

我在獲取文檔的內容時遇到問題。我能夠獲取文檔ID,但無法獲取文檔的文本。 我使用下面的代碼是:如何在Google應用中獲取Google文檔的內容?

function getDocuments(user){ 
    var scope = 'https://docs.google.com/feeds/'; 
    //oAuth 
    user="email-id" 
    var fetchArgs = googleOAuth_('docs', scope); 
    var url = scope + user+'/private/full?v=3&alt=json'; 
    var urlFetch = UrlFetchApp.fetch(url, fetchArgs); 
    var json=Utilities.jsonParse(urlFetch.getContentText()) 
    var entry = json.feed.entry; 
    var docs = []; 
    for(var i in entry){ 
    var tempDoc = {}; 
    for(var j in entry[i]){ 

     tempDoc.id = entry[i].id.$t.split('%3A')[1]; 
    } 
    docs.push(tempDoc); 
    //Logger.log(docs[i]) 
    } 


    var url='https://docs.google.com/feeds/download/documents/Export?docID=?' 

    var data=UrlFetchApp.fetch(url,fetchArgs).getAs('text/html').getDataAsString() 
    MailApp.sendEmail("email","","",{htmlBody:data}) 
} 

//-------------------------------------------------------------------------------------- 
//Google oAuth 
//Used by getDocuments(user) 
function googleOAuth_(name,scope) { 
    var oAuthConfig = UrlFetchApp.addOAuthService(name); 
    oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); 
    oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); 
    oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 
    oAuthConfig.setConsumerKey("consumerkey"); 
    oAuthConfig.setConsumerSecret(consumersecret); 
    return {oAuthServiceName:name, oAuthUseToken:"always",method:"GET"}; 
} 

它返回的文檔HTML格式,但不能返回的文檔內容。 請儘快與我分享任何建議..

+0

要與文檔進行何種操作內容?顯示在用戶界面?把它作爲郵件發送?還有:你想要完整的格式還是隻有文本? –

+0

我想將它存儲在谷歌雲存儲上。並希望完整格式的文件。 –

回答

2

,如果你想在文檔成爲郵件的HTML身體,你可以使用此代碼:

var id = 'the ID of your document' 
    var bodytext = DocumentApp.openById(id).getText();//the text content of your doc (optional) 
    var url = 'https://docs.google.com/feeds/'; 
    var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=html&format=html&id='+id, 
    googleOAuth_('docs',url)).getContentText(); 


    MailApp.sendEmail(destination email, subject, bodytext, {htmlBody:doc}); 

和OAuth的功能:

function googleOAuth_(name,scope) { 
    var oAuthConfig = UrlFetchApp.addOAuthService(name); 
    oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); 
    oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); 
    oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 
    oAuthConfig.setConsumerKey('anonymous'); 
    oAuthConfig.setConsumerSecret('anonymous'); 
    return {oAuthServiceName:name, oAuthUseToken:"always"}; 
} 
0

到目前爲止我不知道任何可以獲取文件內容的功能,可以參考文檔中的文件共享鏈接。

我希望能幫到你!

相關問題