【问题标题】:Generating PDF's from form responses with a template.使用模板从表单响应生成 PDF。
【发布时间】:2017-04-02 04:48:49
【问题描述】:

我目前正在使用 Form Publisher add in 从表单响应中使用我所需的模板制作 PDF,但它允许我在一个月内仅生成 100 个文件。我有每月至少 500 个文件的要求,我买不起 Form Publisher 的高级许可证。请帮助我使用基本脚本,使用我想要的模板根据表单响应数据生成 PDF。

如果可以的话,我会分享模板和样本表。

问候 戈皮克里希纳

【问题讨论】:

    标签: google-apps-script pdf-generation google-forms


    【解决方案1】:

    您可以使用哪些工具?如果您使用pdftk,则有一个fill_form 命令可以获取PDF & FDF/XFDF 数据,并将两者结合起来。

    【讨论】:

    • 感谢您对 Robbat 的回复!实际上我没有这样的工具,而且真的不知道这个工具如何将谷歌表单响应链接到一个将保存为 PDF 的单词模板。我真的在寻找一个脚本,我们可以使用 google appscript 实现自动化,而不是使用第三方工具。
    【解决方案2】:

    这个 sn-p 使用 Google Doc 模板和 Google 电子表格中的值创建一个 PDF 文件。只需将其粘贴到您正在使用的 GSheet 的脚本编辑器中即可。

    // Replace this with ID of your template document.
    var TEMPLATE_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
    
    // var TEMPLATE_ID = '1wtGEp27HNEVwImeh2as7bRNw-tO4HkwPGcAsTrSNTPc' // Demo template
    
    // You can specify a name for the new PDF file here, or leave empty to use the 
    // name of the template.
    var PDF_FILE_NAME = ''
    
    /**
     * Eventhandler for spreadsheet opening - add a menu.
     */
    
    function onOpen() {
    
      SpreadsheetApp
        .getUi()
        .createMenu('Create PDF')
        .addItem('Create PDF', 'createPdf')
        .addToUi()
    
    } // onOpen()
    
    /**  
     * Take the fields from the active row in the active sheet
     * and, using a Google Doc template, create a PDF doc with these
     * fields replacing the keys in the template. The keys are identified
     * by having a % either side, e.g. %Name%.
     *
     * @return {Object} the completed PDF file
     */
    
    function createPdf() {
    
      if (TEMPLATE_ID === '') {
    
        SpreadsheetApp.getUi().alert('TEMPLATE_ID needs to be defined in code.gs')
        return
      }
    
      // Set up the docs and the spreadsheet access
    
      var copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy(),
          copyId = copyFile.getId(),
          copyDoc = DocumentApp.openById(copyId),
          copyBody = copyDoc.getActiveSection(),
          activeSheet = SpreadsheetApp.getActiveSheet(),
          numberOfColumns = activeSheet.getLastColumn(),
          activeRowIndex = activeSheet.getActiveRange().getRowIndex(),
          activeRow = activeSheet.getRange(activeRowIndex, 1, 1, numberOfColumns).getValues(),
          headerRow = activeSheet.getRange(1, 1, 1, numberOfColumns).getValues(),
          columnIndex = 0
    
      // Replace the keys with the spreadsheet values
    
      for (;columnIndex < headerRow[0].length; columnIndex++) {
    
        copyBody.replaceText('%' + headerRow[0][columnIndex] + '%', 
                             activeRow[0][columnIndex])                         
      }
    
      // Create the PDF file, rename it if required and delete the doc copy
    
      copyDoc.saveAndClose()
    
      var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'))  
    
      if (PDF_FILE_NAME !== '') {
    
        newFile.setName(PDF_FILE_NAME)
      } 
    
      copyFile.setTrashed(true)
    
      SpreadsheetApp.getUi().alert('New PDF file created in the root of your Google Drive')
    
    } // createPdf()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2015-02-13
      • 2015-06-13
      相关资源
      最近更新 更多