2013-02-02 43 views
7

我有一個瘋狂的想法,我可以爲使用Google Drive文檔進行備份的不熟練的用戶朋友構建網站博客。我能夠創建一個編譯文檔列表的contentService。但是,我看不到將文檔轉換爲HTML的方法。我知道Google可以在網頁中呈現文檔,因此我想知道是否可以獲取供我的內容服務使用的渲染版本。將Google文檔設置爲HTML

這可能嗎?

回答

3

有氣體沒有直接的方法來獲取文檔的HTML版本,這是一個相當古老enhancement requestworkaround described originally由恩裏克·阿布雷烏工作得很好,我用它所有的時間...

的只有在需要從腳本編輯器中調用的授權過程中令人討厭的事情,這使得在共享應用程序中使用它(使用「腳本無法」用戶)時不易使用,但這隻發生一次;)。

還有一個LibraryRomain Vialard創建,使事情(一點)更容易...並添加了一些其他有趣的功能。

+2

我很想重溫自己從@HenriqueAbreu提到的解決方法,但該鏈接是沒有可用時間更長:是否在別處發佈? - 謝謝,Fausto –

+0

是的,我知道,他們刪除了檔案......無論如何,代碼在很多地方仍然可以看到。這一個例如http://stackoverflow.com/questions/10954075/unexpected-exception-upon-serializing-continuation。並在問題跟蹤器上。 –

+0

謝謝,已經在使用它 –

-2

也許這會爲你工作...

function doGet() { 
    var blob = DriveApp.getFileById('myFileId').getAsHTML(); 
    return HtmlService.createHtmlOutput(blob); 
} 
+0

你從哪裏找到任何名爲'DriveApp'的東西? –

+0

你參考過Romain Vialard的圖書館嗎?如果是這種情況,你應該在你的評論中提到它 –

11

你可以試試這個代碼:

function getGoogleDocumentAsHTML(){ 
    var id = DocumentApp.getActiveDocument().getId() ; 
    var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested 
    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(); 
    Logger.log(html); 
} 
1

這裏是古爾AOuth之後發表的理念,新的版本有點文檔片斷恩裏克:

function exportAsHTML(){ 
    var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested 
    var docID = DocumentApp.getActiveDocument().getId(); 
    var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+docID+"&exportFormat=html"; 
    var param = { 
    method  : "get", 
    headers  : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}, 
    muteHttpExceptions:true, 
    }; 
    var html = UrlFetchApp.fetch(url,param).getContentText(); 
    return html; 

} 

,然後使用普通的mailApp:

function mailer(){ 
    var docbody = exportAsHTML(); 
    MailApp.sendEmail({ 
    to: "[email protected]", 
    subject: "document emailer", 
    htmlBody: docbody }); 
} 

希望新的解決方法有助於

JD

1

Node.js的解決方案

這裏是你如何能得到一個谷歌文檔的使用谷歌驅動的node.js的客戶端庫的HTML。

// import googleapis npm package 
var google = require('googleapis'); 

// variables 
var fileId = '<google drive doc file id>', 
    accessToken = '<oauth access token>'; 

// oauth setup 
var OAuth2 = google.auth.OAuth2, 
    OAuth2Client = new OAuth2(); 

// set oauth credentials 
OAuth2Client.setCredentials({access_token: accessToken}); 

// google drive setup 
var drive = google.drive({version: 'v3', auth: OAuth2Client}); 

// download file as text/html 
var buffers = []; 
drive.files.export(
    { 
     fileId: fileId, 
     mimeType: 'text/html' 
    } 
) 
    .on('error', function(err) { 
     // handle error 
    }) 
    .on('data', function(data) { 
     buffers.push(data); // data is a buffer 
    }) 
    .on('end', function() { 
     var buffer = Buffer.concat(buffers), 
      googleDocAsHtml = buffer.toString(); 
     console.log(googleDocAsHtml); 
    }); 

查看Google Drive V3 download docs瞭解更多語言和選項。

請注意,Google APIs Node.js Client是alpha版(2017年1月)。

0

您可以使用該解決方案here

/** 
* Converts a file to HTML. The Advanced Drive service must be enabled to use 
* this function. 
*/ 
function convertToHtml(fileId) { 
    var file = Drive.Files.get(fileId); 
    var htmlExportLink = file.exportLinks['text/html']; 
    if (!htmlExportLink) { 
    throw 'File cannot be converted to HTML.'; 
    } 
    var oAuthToken = ScriptApp.getOAuthToken(); 
    var response = UrlFetchApp.fetch(htmlExportLink, { 
    headers:{ 
     'Authorization': 'Bearer ' + oAuthToken 
    }, 
    muteHttpExceptions: true 
    }); 
    if (!response.getResponseCode() == 200) { 
    throw 'Error converting to HTML: ' + response.getContentText(); 
    } 
    return response.getContentText(); 
} 

通作爲FILEID,谷歌的文檔的ID,並啓用先進的驅動服務按照指示here

0

我也有這個問題。該文檔HTML出口吐出來的HTML實在是太醜了,所以這是我的解決方案:

/** 
* Takes in a Google Doc ID, gets that doc in HTML format, cleans up the markup, and returns the resulting HTML string. 
* 
* @param {string} the id of the google doc 
* @param {boolean} [useCaching] enable or disable caching. default true. 
* @return {string} the doc's body in html format 
*/ 
function getContent(id, useCaching) { 

    if (!id) { 
    throw "Please call this API with a valid Google Doc ID"; 
    } 

    if (useCaching == null) { 
    useCaching = true; 
    } 

    if (typeof useCaching != "boolean") { 
    throw "If you're going to specify useCaching, it must be boolean."; 
    } 

    var cache = CacheService.getScriptCache(); 
    var cached = cache.get(id); // see if we have a cached version of our parsed html 
    if (cached && useCaching) { 
    var html = cached; 
    Logger.log("Pulling doc html from cache..."); 
    } else { 

    Logger.log("Grabbing and parsing fresh html from the doc..."); 

    try { 
     var doc = DriveApp.getFileById(id); 
    } catch (err) { 
     throw "Please call this API with a valid Google Doc ID. " + err.message; 
    } 

    var docName = doc.getName(); 

    var forDriveScope = DriveApp.getStorageUsed(); // needed to get Drive Scope requested in ScriptApp.getOAuthToken(); 
    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(); 

    // nuke the whole head section, including the stylesheet and meta tag 
    html = html.replace(/<head>.*<\/head>/, ''); 
    // remove almost all html attributes 
    html = html.replace(/ (id|class|style|start|colspan|rowspan)="[^"]*"/g, ''); 
    // remove all of the spans, as well as the outer html and body 
    html = html.replace(/<(span|\/span|body|\/body|html|\/html)>/g, ''); 
    // clearly the superior way of denoting line breaks 
    html = html.replace(/<br>/g, '<br />'); 

    cache.put(id, html, 900) // cache doc contents for 15 minutes, in case we get a lot of requests 

    } 

    Logger.log(html); 

    return html; 

} 

https://gist.github.com/xd1936/cc229d14a89e6327336177bb07ac2980

相關問題