2017-04-02 35 views
0

我目前正在使用Form Publisher添加PDF來從表單響應中使用我需要的模板,但它允許我在一個月內只生成100個文件。我有月最少500個文件的要求,我無法承擔Form Publisher的高級授權購買。請幫助我使用基本腳本來基於表單響應數據和我所需的模板生成PDF。使用模板從表單響應中生成PDF。

我會分享模板和樣本表,如果可以完成的話。

問候 Gopikrishna

回答

0

什麼工具可以爲您的使用情況?如果使用pdftk,則有一個fill_form命令可以將FDF/XFDF數據作爲PDF &,並將它們結合起來。

+0

謝謝你回覆Robbat!事實上,我沒有這樣的工具,真的不知道這個工具如何將谷歌形式的反應與保存爲PDF的單詞模板聯繫起來。我真的在尋找一個腳本,我們可以使用google appscript自動執行,而不是使用第三方工具。 –

-1

此片段使用Google文檔模板和Google Spreadsheet中的值創建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() 
相關問題