Sam Murdock
Sam Murdock

Reputation: 13

Trying to save a google form response as pdf, getting an error reading the array of inputs from the template doc?

Trying to set up a code that whenever the form linked to this sheet is submitted it takes those responses, saves them as a pdf, and then eventually (haven't coded this part yet) emails them to a specific user. When testing using the provided test function it can't figure out the array for some reason, which is the template for the pdf. I've double checked all document id's are correct, and if i comment out the first line then the next one throws the same issue. I also tried putting them in as ['0'] but that throws the same issue at the same lines.

edit to remove defunct function and replace with modern equivalent

TypeError: Cannot read properties of undefined (reading '0')
createPDF   @ Code.gs:47
afterFormSubmit @ Code.gs:33
test_onFormSubmit   @ Code.gs:26
/**
 * Test function for Spreadsheet Form Submit trigger functions.
 * Loops through content of sheet, creating simulated Form Submit Events.
 *
 * Check for updates: https://stackoverflow.com/a/16089067/1677912
 *
 * See https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
 */ 

function test_onFormSubmit() {
  var dataRange = SpreadsheetApp.getActiveSheet().getDataRange();
  var data = dataRange.getValues();
  var headers = data[0];
  // Start at row 1, skipping headers in row 0
  for (var row=1; row < data.length; row++) {
    var e = {};
    e.values = data[row].filter(Boolean);  // filter: https://stackoverflow.com/a/19888749
    e.range = dataRange.offset(row,0,1,data[0].length);
    e.namedValues = {};
    // Loop through headers to create namedValues object
    // NOTE: all namedValues are arrays.
    for (var col=0; col<headers.length; col++) {
      e.namedValues[headers[col]] = [data[row][col]];
    }
    // Pass the simulated event to afterFormSubmit
    afterFormSubmit(e);
  }
}


function afterFormSubmit(e) {
  const info = e.namedValues;
  createPDF(info);
}

function createPDF(info ){

const pdfFolder = DriveApp.getFolderById("1g58GUQLPjPonsHtxj5LlxyoDgXs5wj2R");
const tempFolder = DriveApp.getFolderById("1Fkzf0xeZcedfq7BF2k3V4mn4Pz_LsXsv");
const templateDoc = DriveApp.getFileById("1eOqom8SqhuDUpIqYEVum-EvQ09cVz2d_XCLcRNAz8jE");

const newTempFile = templateDoc.makeCopy(tempFolder);

const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();


*/ these arrays are the issue, all of them are coming back undefined /* 



body.replaceText("{fn}", info['First Name'][0]);
body.replaceText("{ln}", info['Last Name'][0]); 
body.replaceText("{bd}", info['Birthday'][0]);
body.replaceText("{em}", info['Email'][0]);
body.replaceText("{pn}", info['Phone Number'][0]);
body.replaceText("{pv}", info['Province'][0]);
body.replaceText("{cm}", info['Contact Method'][0]);
body.replaceText("{lg}", info['Language'][0]);
body.replaceText("{ts}", info['Type Of Service'][0]); 
body.replaceText("{cn}", info['Child Name'][0]);
body.replaceText("{cbd}", info['Child Birthday'][0]);
body.replaceText("{sr}", info['Services Required'][0]);
body.replaceText("{stf}", info['Staff Requested'][0]);
body.replaceText("{pri}", info['Priority'][0]);
body.replaceText("{ref}", info['Referral'][0]);
body.replaceText("{jc}", info['Jane Consent'][0]);





openDoc.saveAndClose();

const blobPDF = newTempFile.getAs(MimeType.PDF);
  pdfFolder.createFile(blobPDF).setName(info['First Name'][0] + ' ' + (info['Last Name'][0]));
  // tempFolder.removeFile(newTempFile);
  tempFolder.getFilesByName(newTempFile).next().setTrashed(true);

}

Upvotes: -1

Views: 40

Answers (0)

Related Questions