Steve Haddley
Steve Haddley

Reputation: 37

Adding save pdf option GAS

A year ago with the help of another user I was able to use google app script to take the form responses and email a pdf (code below)

I now need to save the pdf to a specific folder I also need to add a link to the saved pdf within the google sheet. Is this possible?

var docTemplate = "doc ID"; 
var docName = "Vehicle check with images";

function onFormSubmit(e) {
  var replaceTextToImage = function(body, searchText, fileId) {
    var width = 300; // Please set this.
    var blob = DriveApp.getFileById(fileId).getBlob();
    var r = body.findText(searchText).getElement();
    r.asText().setText("");
    var img = r.getParent().asParagraph().insertInlineImage(0, blob);
    var w = img.getWidth();
    var h = img.getHeight();
    img.setWidth(width);
    img.setHeight(width * h / w);
  }

  //Get information from form and set as variables
  var email_address = "[email protected]";
  var vehicle_vrn = e.values[1];
  var front_desc = e.values[2];
  var front_image = e.values[3].split("=")[1];
  var rear_desc = e.values[4];
  var rear_image = e.values[5].split("=")[1];
  var driver_desc = e.values[6];
  var driver_image = e.values[7].split("=")[1];
  var passenger_desc = e.values[8];
  var passenger_image = e.values[9].split("=")[1];

  // Get document template, copy it as a new temp doc, and save the Doc’s id
  var copyId = DriveApp.getFileById(docTemplate)
  .makeCopy(docName+' for '+vehicle_vrn)
  .getId();
  // Open the temporary document
  var copyDoc = DocumentApp.openById(copyId);
  // Get the document’s body section
  var copyBody = copyDoc.getBody();

  copyBody.replaceText('keyVrn', vehicle_vrn);
  copyBody.replaceText('keyFrontdesc', front_desc);
  replaceTextToImage(copyBody, 'keyFrontimage', front_image);
  copyBody.replaceText('keyReardesc', rear_desc);
  replaceTextToImage(copyBody, 'keyRearimage', rear_image);
  copyBody.replaceText('keyDriversdesc', driver_desc);
  replaceTextToImage(copyBody, 'keyDriversimage', driver_image);
  copyBody.replaceText('keyPassdesc', passenger_desc);
  replaceTextToImage(copyBody, 'keyPassimage', passenger_image);

  copyDoc.saveAndClose();
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
  var subject = "sample attachment file";
  var body = "sample text: " + vehicle_vrn + "";
  MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});

  DriveApp.getFileById(copyId).setTrashed(true);
}```

Upvotes: 1

Views: 184

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I believe your goal as follows.

  • You want to save the PDF data to the specific folder as a file.
  • You want to retrieve the URL of the saved PDF file and put it to the specific column of the Spreadsheet.

Modification points:

  • In order to save the PDF data (in your case, it's a blob.) to the specific folder, you can use DriveApp.getFolderById("folderId").createFile(pdf).
  • In order to retrieve the URL of the created file, you can use getUrl().
  • In order to put the URL to the specific column of the Spreadsheet.
    • You want to put the value to the next column of the last row.

Modified script:

Please modify your script as follows. Before you use this modified script, please set the folder ID, Spreadsheet ID and sheet name.

From:
copyDoc.saveAndClose();
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
To:
copyDoc.saveAndClose();
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
var url = DriveApp.getFolderById("###folderId###").createFile(pdf).getUrl();
var sheet = SpreadsheetApp.openById("###spreadsheetId###").getSheetByName("###sheetName###");
sheet.getRange(sheet.getLastRow(), sheet.getLastColumn() + 1).setValue(url);
  • When you run the script, pdf is created to the folder ###folderId### as a file, and url is put to the next column of the last row of the sheet ###sheetName### on the Spreadsheet ###spreadsheetId###.

Note:

  • If you want to give the filename of PDF file, please modify as follows.

    • From

        var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
      
    • To

        var pdf = DriveApp.getFileById(copyId).getAs("application/pdf").setName("###filename###");
      

References:

Upvotes: 1

Related Questions