使用电子表格中的数据填写 gdocs 后创建 PDF

发布于 2025-01-14 17:17:35 字数 1427 浏览 1 评论 0原文

我编写了这个脚本来创建一个 gdocs,其中填充了来自 google 电子表格的数据:

function autoFillGoogleDocFromForm(e) {
      var timestamp = e.values[0];
      
      var d = new Date();
      var curr_date = d.getDate();
      var curr_month = d.getMonth() + 1; 
      var curr_year = d.getFullYear();
      var theDate = curr_year + "0" + curr_month + curr_date;
    
      var contractualpartner = e.values[2];
      var address = e.values[3];
      var companyno = e.values[4];
      var signee = e.values[5];
      var title = e.values[6];
      
      var templateFile = DriveApp.getFileById("1eVqeRAyBPM4VDGlByDj3LAiDpzky2-eDO_pZz0IP_4E");
      var templateResponseFolder = DriveApp.getFolderById("1OVUma_-u6RYaw8qVRbxH8yF58x0MCwd8");
      
      var copy = templateFile.makeCopy(theDate + '_' + "YYY" + '_' + "XXX" + '&' + contractualpartner, templateResponseFolder);
      
      var doc = DocumentApp.openById(copy.getId())
      
      var body = doc.getBody();
      
      body.replaceText("{{Partner}}", contractualpartner);
      body.replaceText("{{Address}}", address);
      body.replaceText("{{Company Number}}", companyno);
      body.replaceText("{{Signee}}", signee);
      body.replaceText("{{Title}}", title);
      
    
      doc.saveAndClose();
    
    }

该脚本运行良好。现在我想将创建的 gdocs 转换为 pdf 文档。 我花了几个小时寻找解决方案,但没有找到。我找到了一个解决方案,其中文件夹中的所有文档都会从 gdocs 转换为 pdf,但我只想转换电子表格中的新条目。每个 gdoc 应该只有一个 pdf 文档。

I wrote this script to create a gdocs filled with data from a google spreadsheet:

function autoFillGoogleDocFromForm(e) {
      var timestamp = e.values[0];
      
      var d = new Date();
      var curr_date = d.getDate();
      var curr_month = d.getMonth() + 1; 
      var curr_year = d.getFullYear();
      var theDate = curr_year + "0" + curr_month + curr_date;
    
      var contractualpartner = e.values[2];
      var address = e.values[3];
      var companyno = e.values[4];
      var signee = e.values[5];
      var title = e.values[6];
      
      var templateFile = DriveApp.getFileById("1eVqeRAyBPM4VDGlByDj3LAiDpzky2-eDO_pZz0IP_4E");
      var templateResponseFolder = DriveApp.getFolderById("1OVUma_-u6RYaw8qVRbxH8yF58x0MCwd8");
      
      var copy = templateFile.makeCopy(theDate + '_' + "YYY" + '_' + "XXX" + '&' + contractualpartner, templateResponseFolder);
      
      var doc = DocumentApp.openById(copy.getId())
      
      var body = doc.getBody();
      
      body.replaceText("{{Partner}}", contractualpartner);
      body.replaceText("{{Address}}", address);
      body.replaceText("{{Company Number}}", companyno);
      body.replaceText("{{Signee}}", signee);
      body.replaceText("{{Title}}", title);
      
    
      doc.saveAndClose();
    
    }

This script works perfectly. Now I would like to convert the created gdocs into a pdf document.
I have spent some hours searching for a solution but didn't find one. I found a solutions where all documents in the folder would be converted from gdocs to pdf but I would like to only convert the new entry in the spreadsheet. Every gdoc should have only one pdf document.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2025-01-21 17:17:35

创建 PDF驱动器 API V2。

function createPDFFromDoc() {
  const fileid = "enter file id";
  const destfolderid = "enter destination folder id";
  const folder = DriveApp.getFolderById(destfolderid);
  const file = DriveApp.getFileById(fileid);
  const blob = file.getAs('application/pdf').setName(file.getName());//pdf name
  const pdffile = DriveApp.createFile(blob);
  Drive.Files.update({ "parents": [{ "id": folder.getId() }] }, pdffile.getId());//pdf folder
}

必须启用

Create PDF

function createPDFFromDoc() {
  const fileid = "enter file id";
  const destfolderid = "enter destination folder id";
  const folder = DriveApp.getFolderById(destfolderid);
  const file = DriveApp.getFileById(fileid);
  const blob = file.getAs('application/pdf').setName(file.getName());//pdf name
  const pdffile = DriveApp.createFile(blob);
  Drive.Files.update({ "parents": [{ "id": folder.getId() }] }, pdffile.getId());//pdf folder
}

Drive API V2 must be enabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文