0

I am using a script to process a form as it is submitted. Part of it involves merging data from the form with a google doc and then emailing that doc as a pdf. Is there anyway to delete the doc once it has been sent?

If it helps, here is my script:

function onFormSubmit(e) {
  //Get Form Values
    var candID = parseInt(e.values[1]);
    var emailAddress = e.values[2];

  //Get Additional Settings
    var ssMaster = SpreadsheetApp.openById("0AierVcXWELCudDY2Y3J2Z1hoX3pLOXYzTW1pOVF3Wmc");
    var settings_sheet = ssMaster.getSheetByName("Settings");
    var candCodeLength = settings_sheet.getRange("B20").getValue();
    var candCodeChars = settings_sheet.getRange("B21").getValue();
    var tempDocumentID = settings_sheet.getRange("B14").getValue();
    var candInfoSheet = ssMaster.getSheetByName("Candidate Information");
    var candInfoLastRow = candInfoSheet.getLastRow();
    var candInfoArray = candInfoSheet.getRange(2,1,candInfoLastRow,7).getValues();

  //Find Corresponding Row for Form Entry
    for(var i=0; i<candInfoArray.length; i++) {
      if (candInfoArray[i][3] === candID) {
        var row = i+2;
        }
      }
     var candIDRowNumber = row;

  //Create & Record Candidate Code
    do { 
      var candCode = createCandCode(candCodeLength, candCodeChars);
      }
      while(checkCandCode(candInfoArray, candCode) === true);
      candInfoSheet.getRange(row,6,1,1).setValue(candCode);
      candInfoSheet.getRange(row,7,1,1).setValue(emailAddress);

  //Create PDF
    var docid = DocsList.getFileById(tempDocumentID).makeCopy("Character Reference Instructions").getId();
    var doc = DocumentApp.openById(docid);
    Logger.log(candInfoArray);
    Logger.log(row);
    var firstName = candInfoArray[(row-2)][1];
    var lastName = candInfoArray[(row-2)][0];
    var body = doc.getActiveSection();
    body.replaceText("<<first>>", firstName);
    body.replaceText("<<last>>", lastName);
    body.replaceText("<<code>>", candCode);
    doc.saveAndClose();

  //Send Email
    var message = "You have successfully completed the NHS Registration form.\n\n Your Candidate Code (different from your Candidate ID) is \"" + candCode + "\" and should be given to your character reference so that they can fill out your character reference form.\n\n If you have any questions, please email Ann Perham at [email protected]." ;
    var subject = "NHS Registration Confirmation & Character Reference Instructions";
    var advancedArgs = {name: "Ann Perham", replyTo: "[email protected]", attachments: doc.getAs("application/pdf")};
    MailApp.sendEmail(emailAddress, subject, message, advancedArgs);
  }

1 Answer 1

4

You can use doc.setTrashed right at the end of your function while it is still the active document.

1
  • 1
    Thanks @serge-insas! I ended up adding the following code just after my "Send Mail" section. var file = DocsList.getFileById(docid); file.setTrashed(true); I searched the documentation yesterday for this answer, but for some reason I never thought to search for the term "trash".
    – will w
    Jun 13, 2012 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.