2016-02-28 158 views
0

我正在将JIRA中的数据导入Google文档。其中一个字段是描述,它基本上是HTML文本。使用Google应用程序脚本将HTML表导入Google文档

有什么方法可以在Google Doc上“添加”这个HTML文本吗?或者我真的不得不手动解析它并创建段落,表格等?

的HTML可能看起来如下:

<p>Hello There</p> 
<table class='confluenceTable'> 
    <tbody> 
     <tr> 
      <th class='confluenceTh'> Name </th> 
      <th class='confluenceTh'> Hours </th> 
      <th class='confluenceTh'> Cost </th> 
     </tr> 
     <tr> 
      <td class='confluenceTd'> Some Person</td> 
      <td class='confluenceTd'> 1 </td> 
      <td class='confluenceTd'> CHF 1</td> 
     </tr> 
    </tbody> 
</table> 

回答

0

我能够在this post创建文件来自于我的评论中提到的变化风格的HTML内容:

var blob = Utilities.newBlob(content, {mimeType:"text/html"}); 
var newfile = Drive.Files.insert(resource, blob, {"convert":"true"}); 
0

谢谢为您的提示 - 基于此,我发现了以下帖子:https://ctrlq.org/code/20102-convert-office-files-to-google-docs(加上http://scraping.pro/automaticly-converting-files-google-app-script/告诉你如何启用Drive API)。

下面的函数工作得很好对我来说:

function convertHtmlToDocs(html) { 

    var uploadFile = JSON.parse(UrlFetchApp.fetch(
    "https://www.googleapis.com/upload/drive/v2/files? uploadType=media&convert=true", 
{ 
    method: "POST", 
    contentType: "text/html", 
    payload: Utilities.newBlob("").setDataFromString(html, "UTF-8").getBytes(), 
    headers: { 
    "Authorization" : "Bearer " + ScriptApp.getOAuthToken() 
    }, 
    muteHttpExceptions: true 
} 
).getContentText()); 

    var doc = DocumentApp.openById(uploadFile.id); 
    var body = doc.getBody().copy(); 
    DriveApp.removeFile(DriveApp.getFileById(doc.getId())); 
    return body; 
} 
+0

你再追加'body'到现有的谷歌文档或创建一个新的? –

+0

或'html'是否被附加到doc中的'fetch' url? –

相关问题