我正在使用一个脚本来收集表单响应并将其合并到Google Doc模板中,转换为PDF和电子邮件。该脚本非常适合这些事情。当用户编辑表单响应时,我会遇到麻烦。这些提交仍会触发脚本,但是结果文档会丢失所有合并字段(已编辑的字段除外)。我意识到这与功能(e)的“事件”性质有关。
当用户重新提交表单时,是否有一种方法可以使此文档重新合并具有所有字段的整个文档?也许基于E列中“是”的If / Else语句表明这是经过修改的答案?
以下是工作表:https://docs.google.com/spreadsheets/d/1sLW9T9XTqx9YcskDoRXJaferQCH2W3tBgthsArtLNIo/edit?usp=sharing
// Get template from Google Docs and name it
var docTemplate = '1BKhC-YwD1Wg0dzeQLfCVBKco0DT6EBkLj37bAScLSTE'; // *** replace with your template ID ***
var docName = 'TravelFax';
// When Form Gets submitted
function travelRequest(e) {
// edit response
var editResponse = e.values[3];
var form = FormApp.openById('1fevVeA-8K3s6EIST5drmETMAr3GSuYc5jngaPRxMN3Y');
//enter form ID here
var urlSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
//Change the sheet name as appropriate
var urlData = urlSheet.getDataRange().getValues();
var urlCol = 114; // column number where URL's should be populated; A = 1, B = 2 etc
var urlResponses = form.getResponses();
var urlTimestamps = [], urls = [], resultUrls = [];
var pos1st = 7;
for (var i = 0; i < urlResponses.length; i++) {
urlTimestamps.push(urlResponses[i].getTimestamp().setMilliseconds(0));
urls.push(urlResponses[i].getEditResponseUrl());
}
for (var j = pos1st - 1; j < urlData.length; j++) {
resultUrls.push([urlData[j][0]?urls[urlTimestamps.indexOf(urlData[j][0].setMilliseconds(0))]:'']);
}
urlSheet.getRange(pos1st, urlCol, resultUrls.length).setValues(resultUrls);
// row number where the first response is located; 1 = 1, 2 = 2, etc for (var j = pos1st-1; j < data.length; j++) { resultUrls.push(['=HYPERLINK("' + [data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:''] + '","Edit")']); } sheet.getRange(pos1st, urlCol, resultUrls.length).setValues(resultUrls);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
var startRow = 2;
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var dataRange = sheet.getRange(sheet.getLastRow(), 1, 1, 114);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i){
if(data == ""){
data.setValue(" ");}
var row = data[i];//Get information from form and set as variables
var rowForLookup = e.range.getRow();
var timestamp = e.values[0];
var columnOfEditUrl = 114;
var email_address = "cormc@firenet.gov";
var columnOfContactPhone = 4;
var travelArranger = e.values[1];
var contactPhone = sheet.getRange(rowForLookup, columnOfContactPhone).getValue();
var editUrl = sheet.getRange(rowForLookup, columnOfEditUrl).getValue();
var incidentName = e.values[4];
var incidentNumber = e.values[5];
var finCode = e.values[6];
var depDateTime = e.values[7];
var arrDateTime = e.values[8];
var destCity = e.values[9];
var destState = e.values[10];
var carRental = e.values[11];
var vehSize = e.values[12];
var rentalDays = e.values[13];
var specInstr = e.values[14];
var singRONum = e.values[16];
var singFName = e.values[17];
var singMName = e.values[18];
var singLName = e.values[19];
var singGender = e.values[20];
var singDOB = e.values[21];
var singDepJP = e.values[22];
var trav01RO = e.values[23];
var trav01FName = e.values[24];
var trav01MName = e.values[25];
var trav01LName = e.values[26];
var gender01 = e.values[27];
var dOB01 = e.values[28];
var depJP01 = e.values[29];
var trav2RO = e.values[32];
var trav2FName = e.values[33];
var trav2MName = e.values[34];
var trav2LName = e.values[35];
var gender2 = e.values[36];
var dOB2 = e.values[37];
var depJP2 = e.values[38];
var trav3RO = e.values[41];
var trav3FName = e.values[42];
var trav3MName = e.values[43];
var trav3LName = e.values[44];
var gender3 = e.values[45];
var dOB3 = e.values[46];
var depJP3 = e.values[47];
var trav4RO = e.values[50];
var trav4FName = e.values[51];
var trav4MName = e.values[52];
var trav4LName = e.values[53];
var gender4 = e.values[54];
var dOB4 = e.values[55];
var depJP4 = e.values[56];
var trav5RO = e.values[59];
var trav5FName = e.values[60];
var trav5MName = e.values[61];
var trav5LName = e.values[62];
var gender5 = e.values[63];
var dOB5 = e.values[64];
var depJP5 = e.values[65];
var trav6RO = e.values[68];
var trav6FName = e.values[69];
var trav6MName = e.values[70];
var trav6LName = e.values[71];
var gender6 = e.values[72];
var dOB6 = e.values[73];
var depJP6 = e.values[74];
var trav7RO = e.values[77];
var trav7FName = e.values[78];
var trav7MName = e.values[79];
var trav7LName = e.values[80];
var gender7 = e.values[81];
var dOB7 = e.values[82];
var depJP7 = e.values[83];
var trav8RO = e.values[86];
var trav8FName = e.values[87];
var trav8MName = e.values[88];
var trav8LName = e.values[89];
var gender8 = e.values[90];
var dOB8 = e.values[91];
var depJP8 = e.values[92];
var trav9RO = e.values[95];
var trav9FName = e.values[96];
var trav9MName = e.values[97];
var trav9LName = e.values[98];
var gender9 = e.values[99];
var dOB9 = e.values[100];
var depJP9 = e.values[101];
var trav10RO = e.values[104];
var trav10FName = e.values[105];
var trav10MName = e.values[106];
var trav10LName = e.values[107];
var gender10 = e.values[108];
var dOB10 = e.values[109];
var depJP10 = e.values[110];
}
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy('TRAVEL REQUEST'+ " "+singLName+", "+singFName+" // "+incidentName)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
copyBody.replaceText('timestamp', timestamp);
copyBody.replaceText('travelArranger', travelArranger);
copyBody.replaceText('contactPhone', contactPhone);
copyBody.replaceText('incidentName', incidentName);
copyBody.replaceText('incidentNumber', incidentNumber);
copyBody.replaceText('finCode', finCode);
copyBody.replaceText('depDateTime', depDateTime);
copyBody.replaceText('arrDateTime', arrDateTime);
copyBody.replaceText('destCity', destCity);
copyBody.replaceText('destState', destState);
copyBody.replaceText('carRental', carRental);
copyBody.replaceText('vehSize', vehSize);
copyBody.replaceText('rentalDays', rentalDays);
copyBody.replaceText('specInstr', specInstr);
copyBody.replaceText('singRONum', singRONum);
copyBody.replaceText('singFName', singFName);
copyBody.replaceText('singMName', singMName);
copyBody.replaceText('singLName', singLName);
copyBody.replaceText('singGender', singGender);
copyBody.replaceText('singDOB', singDOB);
copyBody.replaceText('singDepJP', singDepJP);
copyBody.replaceText('trav01RO', trav01RO);
copyBody.replaceText('trav01FName', trav01FName);
copyBody.replaceText('trav01MName', trav01MName);
copyBody.replaceText('trav01LName', trav01LName);
copyBody.replaceText('gender01', gender01);
copyBody.replaceText('dOB01', dOB01);
copyBody.replaceText('depJP01', depJP01);
copyBody.replaceText('trav2RO', trav2RO);
copyBody.replaceText('trav2FName', trav2FName);
copyBody.replaceText('trav2MName', trav2MName);
copyBody.replaceText('trav2LName', trav2LName);
copyBody.replaceText('gender2', gender2);
copyBody.replaceText('dOB2', dOB2);
copyBody.replaceText('depJP2', depJP2);
copyBody.replaceText('trav3RO', trav3RO);
copyBody.replaceText('trav3FName', trav3FName);
copyBody.replaceText('trav3MName', trav3MName);
copyBody.replaceText('trav3LName', trav3LName);
copyBody.replaceText('gender3', gender3);
copyBody.replaceText('dOB3', dOB3);
copyBody.replaceText('depJP3', depJP3);
copyBody.replaceText('trav4RO', trav4RO);
copyBody.replaceText('trav4FName', trav4FName);
copyBody.replaceText('trav4MName', trav4MName);
copyBody.replaceText('trav4LName', trav4LName);
copyBody.replaceText('gender4', gender4);
copyBody.replaceText('dOB4', dOB4);
copyBody.replaceText('depJP4', depJP4);
copyBody.replaceText('trav5RO', trav5RO);
copyBody.replaceText('trav5FName', trav5FName);
copyBody.replaceText('trav5MName', trav5MName);
copyBody.replaceText('trav5LName', trav5LName);
copyBody.replaceText('gender5', gender5);
copyBody.replaceText('dOB5', dOB5);
copyBody.replaceText('depJP5', depJP5);
copyBody.replaceText('trav6RO', trav6RO);
copyBody.replaceText('trav6FName', trav6FName);
copyBody.replaceText('trav6MName', trav6MName);
copyBody.replaceText('trav6LName', trav6LName);
copyBody.replaceText('gender6', gender6);
copyBody.replaceText('dOB6', dOB6);
copyBody.replaceText('depJP6', depJP6);
copyBody.replaceText('trav7RO', trav7RO);
copyBody.replaceText('trav7FName', trav7FName);
copyBody.replaceText('trav7MName', trav7MName);
copyBody.replaceText('trav7LName', trav7LName);
copyBody.replaceText('gender7', gender7);
copyBody.replaceText('dOB7', dOB7);
copyBody.replaceText('depJP7', depJP7);
copyBody.replaceText('trav8RO', trav8RO);
copyBody.replaceText('trav8FName', trav8FName);
copyBody.replaceText('trav8MName', trav8MName);
copyBody.replaceText('trav8LName', trav8LName);
copyBody.replaceText('gender8', gender8);
copyBody.replaceText('dOB8', dOB8);
copyBody.replaceText('depJP8', depJP8);
copyBody.replaceText('trav9RO', trav9RO);
copyBody.replaceText('trav9FName', trav9FName);
copyBody.replaceText('trav9MName', trav9MName);
copyBody.replaceText('trav9LName', trav9LName);
copyBody.replaceText('gender9', gender9);
copyBody.replaceText('dOB9', dOB9);
copyBody.replaceText('depJP9', depJP9);
copyBody.replaceText('trav10RO', trav10RO);
copyBody.replaceText('trav10FName', trav10FName);
copyBody.replaceText('trav10MName', trav10MName);
copyBody.replaceText('trav10LName', trav10LName);
copyBody.replaceText('gender10', gender10);
copyBody.replaceText('dOB10', dOB10);
copyBody.replaceText('depJP10', depJP10);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var folder = DriveApp.getFolderById('15aWAg70xUxreFCTj2UBWdGaP1Coi7zw1');
var PDF_FILE_NAME = "TRAVEL REQUEST"+" "+singLName+trav01LName+", "+singFName+trav01FName+" // "+incidentName+".pdf";
var pdfBlob = DriveApp.getFileById(copyId).makeCopy(PDF_FILE_NAME);
var doc = pdfBlob.getAs("application/pdf");
folder.createFile(doc);
// Attach PDF and send the email
var message = "Attached is the Travel Fax for "+singFName+trav01FName+" "+singLName+trav01LName+" for travel to "+incidentName+". Please carefully review all details in the form to ensure accuracy."+"\n"+"If corrections are required, use the following edit link to re-submit the corrected request: "+editUrl+"\n"+"If all details are correct, forward this email to: Usda.incidentdispatch@bcdtravel.com."+"\n"+"A BCD travel agent should contact you within 30 minutes.";
var emailTo = "brooke_malcolm@fws.gov";
var subject = "Travel Request for "+singLName+trav01LName+", "+singFName+trav01FName+" // "+incidentName+" "+timestamp;
var pdf = DriveApp.getFileById(copyId).getAs('application/pdf').getBytes();//getAs('application/pdf')
var attach = {fileName:"TRAVEL REQUEST"+" "+singLName+trav01LName+", "+singFName+trav01FName+" // "+incidentName+".pdf",content:pdf, mimeType:'application/pdf'}; // customize file name
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
pdfBlob.setTrashed(true);
}