0

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.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Malte
  • 1
  • 1
    Welcome to [so]. Questions on this site should contain only information about what is being asked (ref. [ask], https://stackoverflow.com/help/behavior), i.e. when including code it should show what you tried to convert the Google doc to PDF. – Rubén Mar 16 '22 at 18:11

1 Answers1

0

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.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Hi Cooper! Thank you so much for your answer! Drive API v2 is enabled. I am sorry but I am a beginner. I created a created a trigger for this function to run when a form is submitted. I receive the following error: "Exception: Invalid argument: id" I simply copy and pasted your code. Do you know what is causing this issue? thank you! – Malte Mar 17 '22 at 09:03
  • You will have to hardwire the parameters because triggers cannot supply parameters. They will pass the event object into the first parameter if you wish to use it then it is customary to use the letter e as the parameter for the event object to populate – Cooper Mar 17 '22 at 09:14
  • Thank you Cooper for your swift response!!! – Malte Mar 17 '22 at 09:39