简体   繁体   English

从已部署的 Web 应用程序(Google Apps 脚本)下载创建的 Google Doc

[英]Download a created Google Doc from a deployed web app (Google Apps Script)

I've created a script that, when deployed as a web app, asks the user for some input, and creates and writes to a google doc.我创建了一个脚本,当部署为 Web 应用程序时,它会询问用户一些输入,然后创建并写入 google 文档。

Apparently, downloading the google doc from the script is not possible, and the next best thing seems to convert the doc and save it in my drive.显然,从脚本下载谷歌文档是不可能的,接下来最好的事情似乎是转换文档并将其保存在我的驱动器中。

I have tried this very simple approach, inspired by Convert Google Doc to PDF using Google Script Editior我尝试过这种非常简单的方法,其灵感来自于使用 Google Script Editor 将 Google Doc 转换为 PDF

Project is deployed as a web app项目部署为 Web 应用程序

Code.gs:代码.gs:

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index');
}

function editDoc(data) {
  let doc = DocumentApp.create("Title");
  let body = doc.getBody();
  body.setText(data);
  
  docblob = doc.getAs('application/pdf');

  docblob.setName(doc.getName() + ".pdf");
  let file = DriveApp.createFile(docblob);
}

Index.html:索引.html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form onsubmit="edit()">
      <input type="text" id="input">
      <input type="submit" id="sub" value="Submit">
    </form>
    <script>
      function edit() {
        google.script.run.editDoc(document.getElementById("input").value);
      }
    </script>
  </body>
</html>

As a result, this adds a pdf to my google drive, which should be the pdf form of the created google doc, however it is blank.结果,这会在我的 google 驱动器中添加一个 pdf,它应该是创建的 google doc 的 pdf 形式,但是它是空白的。

I believe your goal as follows.我相信你的目标如下。

  • You want to create new Google Document including the value from the input tag.您想创建新的 Google 文档,包括输入标签中的值。
  • And, you want to export the Google Document as a PDF file.并且,您想将 Google 文档导出为 PDF 文件。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本来实现这一点。

Modification points:改装要点:

  • I think that the reason of however it is blank is that the Document is not saved.我认为however it is blank的原因是没有保存文档。
  • In order to download the PDF file, I would like to propose to convert the PDF data to the base64 data and set it as the data URL, and then, download it.为了下载PDF文件,我想建议将PDF数据转换为base64数据并将其设置为数据URL,然后下载。

When above points are reflected to your script, it becomes as follows.当以上几点反映到你的脚本中时,它变成如下。

Modified script:修改后的脚本:

Google Apps Script side: Code.gs Google Apps 脚本端: Code.gs
 function doGet() { return HtmlService.createHtmlOutputFromFile('Index'); } function editDoc(data) { let doc = DocumentApp.create("Title"); let body = doc.getBody(); body.setText(data); doc.saveAndClose(); return { data: "data:application/pdf;base64," + Utilities.base64Encode(doc.getBlob().getBytes()), filename: doc.getName() + ".pdf" }; }
HTML & Javascript side: Index.html HTML & Javascript 端: Index.html
 <!DOCTYPE html> <html> <head> <base target="_top"> </head> <body> <form onsubmit="event.preventDefault(); edit();"> <input type="text" id="input"> <input type="submit" id="sub" value="Submit"> </form> <script> function edit() { google.script.run.withSuccessHandler(({data, filename}) => { const a = document.createElement("a"); document.body.appendChild(a); a.download = filename; a.href = data; a.click(); }).editDoc(document.getElementById("input").value); } </script> </body> </html>
  • I thought that in this case, a simple button can be also used instead of the submit button.我认为在这种情况下,也可以使用一个简单的按钮来代替提交按钮。
  • In the case of Google Docs, when a blog is retrieved, in the current stage, the blob is the PDF format.以Google Docs为例,当检索到博客时,在当前阶段,blob是PDF格式。
  • In this modified script, when a text is inputted and a button is clicked, a PDF file is downloaded to the local PC.在这个修改后的脚本中,当输入一个文本并点击一个按钮时,一个PDF文件被下载到本地PC。

Note:笔记:

  • In your script, it seems that Web Apps is used.在您的脚本中,似乎使用了 Web Apps。 In this case, when the script of Web Apps is modified, please redeploy the Web Apps as new version.在这种情况下,当 Web Apps 的脚本被修改时,请将 Web Apps 重新部署为新版本。 By this, the latest script is reflected to the Web Apps.这样,最新的脚本就会反映到 Web Apps 中。 Please be careful this.请注意这一点。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM