无法从 Google 脚本生成 PDF

问题描述 投票:0回答:1

我按照 Youtube 教程创建了一个谷歌脚本,可以从谷歌表格生成 pdf。我能够重新创建我在视频中看到的内容并生成 pdf 文件。当需要输入我的具体规格时,PDF 不会为我生成。我已经尝试过故障排除,但我是个新手,无法弄清楚。

function afterFormSubmit(e) {
  const info = e.namedValues;
  createPDF(info);

}


function createPDF(info){

   const pdfFolder = DriveApp.getFolderById("1TzHSBQVLBTFRMJLNc3y_ZfFSJCN8YN-u");
   const tempFolder = DriveApp.getFolderById("1dAdxsFtuowCSQgHOrU6GrkQvUH86YG_E");
   const templateDoc = DriveApp.getFileById("1lC50FzTsvwvO-5981OCob4-ideeqKN91VzC1WMO7dRw");

   const newTempFile = templateDoc.makeCopy(tempFolder);
   const openDoc = DocumentApp.openById(newTempFile.getId());
   const body = openDoc.getBody();

   body.replaceText("{{name}}", info['Employee name'][0]);
   body.replaceText("{{restaurant}}", info['Restaurant where employee works'][0]);
   body.replaceText("{{position}}", info['Employees position'][0]);
   body.replaceText("{{race}}", info['What is the employees race'][0]);
   body.replaceText("{{origin}}", info['What is the employees national orgin'][0]);
   body.replaceText("{{age}}", info['What is the employees exact age'][0]);
   body.replaceText("{{gender}}", info['What is the employees gender'][0]);
   body.replaceText("{{orientation}}", info['What is the employees sexual orientation if   known'][0]);
   body.replaceText("{{health}}", info['Has the employee ever disclosed that they have any   ongoing health issues to anyone in management'][0]);
   body.replaceText("{{leave}}", info['Has the employee recently taken a leave'][0]);
   body.replaceText("{{pregnant}}", info['Is the employee pregnant or was the employee   recently pregnant'][0]);
   body.replaceText("{{religion}}", info['Has the employee ever requested any religious accommodations'][0]);
   body.replaceText("{{complaint}}", info['Has the employee ever raised a complaint of harassment, discrimination, or sexual harassment to anyone in management'][0]);
   body.replaceText("{{information}}", info['Please provide any other relevant information about the employee'][0]);
   body.replaceText("{{reason}}", info['Reason for termination request'][0]);
   body.replaceText("{{answer}}", info['Has the employee ever been issued any written   disciplinary action form? If yes, please provide copies of all disciplinary warnings with this request'][0]);
   body.replaceText("{{manager}}", info['Name of manager completing this form'][0]);
   openDoc.saveAndClose();

   const blobPDF = newTempFile.getAs(MimeType.PDF);
   const pdfFile = pdfFolder.createFile(blobPDF).setName(info['Employee name'][0] + " "+       info['Restaurant where employee works'][0]);
   tempFolder.removeFile(newTempFile);
   }

}
arrays google-sheets google-apps-script error-handling
1个回答
0
投票

您可以尝试使用以下函数为电子表格创建 PDF 文件:

const pdfBlob = DriveApp.getFileById("ID OF THE SPREADSHEET FILE").getAs("application/pdf");
  
  const pdfName = `${name}_Certificate.pdf`;

您可以检查的另一种方法是使用 url 进行导出并添加您要添加的参数:

//Retrieve the blob from the export URL.
  const id = spreadsheet.getId();
  const xlsxBlob = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/export?id=${id}&exportFormat=pdf`, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}}).getBlob();
  • format=pdf:设置导出文件格式为PDF
  • portrait=false:将方向设置为横向(设置 Portrait=true 将导致纵向)
  • gridlines=false:不显示网格线
  • size=b5:设置页面尺寸为“B5”

您可以在此处查看有关导出电子表格文件的更多信息:https://developers.google.com/apps-script/samples/automations/generate-pdfs

这是另一个完整的指南,您可以在其中检查如何成功完成导出为 pdf:https://spreadsheet.dev/compressive-guide-export-google-sheets-to-pdf-excel-csv-apps-script

© www.soinside.com 2019 - 2024. All rights reserved.