0

我正在使用脚本在提交表单时对其进行处理。其中一部分涉及将表单中的数据与谷歌文档合并,然后将该文档作为 pdf 通过电子邮件发送。文档一旦发送,是否有删除的方法?

如果有帮助,这是我的脚本:

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 ann_perham@needham.k12.ma.us." ;
    var subject = "NHS Registration Confirmation & Character Reference Instructions";
    var advancedArgs = {name: "Ann Perham", replyTo: "ann_perham@needham.k12.ma.us", attachments: doc.getAs("application/pdf")};
    MailApp.sendEmail(emailAddress, subject, message, advancedArgs);
  }
4

1 回答 1

4

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

于 2012-06-12T19:53:21.627 回答