我需要使用 Google Sheets 文件中的数据填写 PDF 表单,其中包含可调整字段。我当前制作了一个脚本(下面的当前脚本),该脚本能够创建一个 Google 文档,其中的字段是从 Google 表格中填写的。现在我需要将新创建的 Google 文档转换为 PDF。任何帮助将不胜感激!!
对于上下文,基本上我需要做的是能够创建一堆类似于附图的 PDF,我所要做的就是将数据加载到 Google 工作表中,然后将该数据很好地填充到 Googledoc/ PDF 因此,如果有比我已经在做的更简单的方法,我也愿意接受新的建议。(https://i.sstatic.net/H3CpkfPO.png)
这是我当前拥有的脚本:
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('19Yy2kyLKnvXYPJcOTxmv5GQ46JmpE1JHZSYkcfw1Z9s');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('1mWUtqLaaSpPbxIQdn0aARSnnRUH1DpQ6')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//Now we get all of the values as a 2D array
const rows = sheet.getDataRange().getValues();
//Start processing each spreadsheet row
rows.forEach(function(row, index){
//Here we check if this row is the headers, if so we skip it
if (index === 0) return;
//Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
if (row[5]) return;
//Using the row data in a template literal, we make a copy of our template document in our destinationFolder
const copy = googleDocTemplate.makeCopy(`${row[1]}, ${row[0]} Employee Details` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In this line we do some friendly date formatting, that may or may not work for you locale
const friendlyDate = new Date(row[3]).toLocaleDateString();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{Name of Investment}}', row[0]);
body.replaceText('{{Call #}}', row[1]);
body.replaceText('{{Investor Name}}', row[2]);
body.replaceText('{{Due Date}}', friendlyDate);
body.replaceText('{{Current Call}}', row[4]);
body.replaceText('{{Agreement Type}}', row[6]);
body.replaceText('{{Total Commitment}}', row[7]);
body.replaceText('{{Total Previous Called}}', row[8]);
body.replaceText('{{Remaining Uncalled}}', row[9]);
body.replaceText('{{Bank Name}}', row[10]);
body.replaceText('{{Bank Account Name}}', row[11]);
body.replaceText('{{Routing #}}', row[12]);
body.replaceText('{{Bank Account #}}', row[13]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 6).setValue(url)
})
}
您可以更改:
const url = doc.getUrl();
sheet.getRange(index + 1, 6).setValue(url)
到
const pdf = DriveApp.getFileById(copy.getId()).getAs('application/pdf');
const file = destinationFolder.createFile(pdf).setName(`${row[1]}, ${row[0]} Employee Details.pdf`);
const url = file.getUrl();
copy.setTrashed(true);
sheet.getRange(index + 1, 6).setValue(url);
这会将转换后的文档粘贴到
generated Google Document
中的 PDF
,而不是粘贴 Column F
。