使用应用程序脚本将HTML呈现为Google文档? [英] Render HTML to a Google Doc using app script?

查看:102
本文介绍了使用应用程序脚本将HTML呈现为Google文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Google云端硬盘,Google文档等。



问题背景:我们的部门之一需要打印数百封电子邮件,并提供页面信息包括在内。我们目前有一个Google Web应用程序,它接受gmail线程的内容,将消息保存为HTML blob,然后将这些消息以PDF格式导出到文件夹。然后,我们的用户将这些PDF下载到硬盘上,然后批量打印出来。



问题本身:我们的用户需要在这些PDF文件打印。为此,我们首先将应用保存电子邮件作为Google文档,将页脚添加相关信息,然后再转换为PDF。然而,我们正在尝试的是不工作,现在我们只是在墙上扔东西,看看会发生什么。



这是我们目前拥有的工作(allMessages是一个包含电子邮件线程的HTML内容的字符串)

  // save将HTML内容转换为PDF文件。 
var htmlBodyFile = myFolder.createFile(fileName.html,allMessages,text / html);
var pdfBlob = htmlBodyFile.getAs(application / pdf);

我们试过这个(并失败):

  //将HTML内容保存为Google文档。 getAs()需要MimeType。 

var htmlBodyFile = myFolder.createFile(fileName.html,allMessages,text / html);
var myDoc = htmlBodyFile.getAs(GOOGLE_DOCS);
// application / GOOGLE_DOCS,以及application / vnd.google-apps.document不起作用

我注意到我可以使用用户界面右键单击云端硬盘中的HTML文件并使用文档打开。它会创建一个新的Google Doc并正确呈现HTML。当我尝试编写脚本(并失败)时,

  var htmlBodyFile = myFolder.createFile(filename.html,allMessages, text / html的); 
var myDoc = DocumentApp.openById(htmlBodyFile.getId());

它给我缺少文件(可能已被删除?)



以下内容将创建一个文档,但实际上不会呈现HTML。

  var myDoc = DocumentApp.create(docname); 
var myBody = myDoc.getBody();
myBody.setText(allMessages);
myDoc.saveAndClose();

现在我的想法已经出来。



我们试图研究涉及Adobe的其他解决方案。不幸的是,Acrobat很昂贵,并以随机顺序打印文档(并且不允许命令行打印,所以我们可以编写脚本)。而Reader没有能力添加我们需要的页面信息。因此,看起来我们唯一的选择就是在保存到Google Drive时插入这些信息。



有谁知道如何使用应用程序脚本将HTML呈现为Google文档?

解决方案

我有一个解决方案。
这行代码会将任何文件转换为适当的Google文档。例如如果它是一个csv文件,它会变成一个电子表格,如果它是html,它将被转换为一个谷歌文档。

  var newFileId = Drive.Files.copy({title:newFileName},oldFileId,{convert:true})。id; 

或者,您可以将包含html的blob直接转换为文档。

  var newFileId = Drive.Files.insert({title:newFileName},blob,{convert:true})。id; 

要做到这一点,您首先必须按照 here



整个高级驱动器API的解释此处,但该文档适用于REST版本的API,因此您必须以某种方式了解如何使用的解释将其转换为应用程序脚本网站位于如何确定方法签名和示例此处。应用程序脚本编辑器中的自动完成功能也有助于此。


We work using Google Drive, Docs, etc.

Background of the problem: One of our departments needs to print hundreds of emails to file away, with page information included. We currently have a Google web app that takes the contents of an gmail thread, saves the messages as an HTML blob, and then exports these to a folder as a PDF. Our users then download these PDFs to the hard drive, and mass-print them.

The problem itself: our users need to have page information on these PDFs when they are being printed. To do this, we're having the app save emails as a Google Doc first, adding footers with the relevant info, then convert to PDFs afterwards. However, what we're trying isn't working, and now we're just throwing stuff at the wall to see what will stick.

This is what we have currently working (allMessages is a String containing the HTML content of an email thread):

//save the HTML content to a PDF file.
var htmlBodyFile = myFolder.createFile("fileName.html", allMessages, "text/html");
var pdfBlob = htmlBodyFile.getAs("application/pdf");

We tried this (and failed):

//save the HTML content to a Google Doc. getAs() needs the MimeType.

var htmlBodyFile = myFolder.createFile("fileName.html", allMessages, "text/html");
var myDoc = htmlBodyFile.getAs("GOOGLE_DOCS");
// application/GOOGLE_DOCS, and application/vnd.google-apps.document don't work either

I noticed that I could use the UI to right-click the HTML file in Drive and open with Docs. It would create a new Google Doc and render the HTML properly. When I tried to script it (and failed),

var htmlBodyFile = myFolder.createFile("filename.html", allMessages, "text/html");
var myDoc = DocumentApp.openById(htmlBodyFile.getId());

it give me "Document is missing (perhaps it was deleted?)"

The following will create a doc, but doesn't actually render the HTML.

var myDoc = DocumentApp.create("docname");   
var myBody = myDoc.getBody();
myBody.setText(allMessages);
myDoc.saveAndClose();

Now I'm about out of ideas.

We tried looking into other solutions involving Adobe. Unfortunately, Acrobat is expensive and prints the docs in random order (and doesn't allow command-line printing so we can script something). And Reader doesn't have the ability to add the page information we need. So it seems our only choice is to insert that info here, when saving in Google Drive.

Does anyone know how to render HTML to a Google Doc using app script?

解决方案

I have a solution for this. This line of code will convert any file to an appropriate google document. e.g. if it's a csv file it will be turned into a spreadsheet and if it's html it will be converted to a google document.

var newFileId = Drive.Files.copy({title: newFileName}, oldFileId, {convert: true}).id;

Alternatively you can convert a blob containing html directly to a document.

var newFileId = Drive.Files.insert({title: newFileName}, blob, {convert: true}).id;

For this to work you first have to turn on the advanced Drive API as explained here

The entire advanced drive API is explained here but that documentation is for the REST version of the API so you have to somehow figure out how to translate that to apps script using the explanation on this site under "How method signatures are determined" and the examples here. The auto complete feature in the apps script editor also helps with this.

这篇关于使用应用程序脚本将HTML呈现为Google文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆