Search code examples
javascriptgoogle-apps-scriptgoogle-sheetstriggersgoogle-forms

Found error about event object .namedValues()


I'm just newbie in JavaScript, sorry for my poor english. I follow coding from somepeople . I try many time to understand and can not fix this problem (Didn't find answer in his post and comment too.). Here is original code. Log's say

TypeError: Cannot read property 'namedValues' of undefined".

Then I have 2 questions.

  1. How to Fix this error ? (This function i trying to create) error in line 3.)

  2. Can modify this code autorun after New row added in google spreadsheet (Sample : I have 2 records, when i insert another row (1 new record) by other way etc. typing via web application (not form), this script automatically run by detect new row.)

Modified Q2 2021/04/06

function AfterFormSubmit (e) {
  
  const info = e.namedValues(info);
  const pdfFile = createPDF(info);
  const entryRow = e.range.getRow();
  const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet");
  ws.getRange(entryRow, 6).setValue(pdfFile.getUrl());
  ws.getRange(entryRow, 7).setValue(pdfFile.getName());

function createPDF(info) {

  const pdfFolder = DriveApp.getFolderById("XXXX");
  const tempFolder = DriveApp.getFolderById("XXXX");
  const templateDoc = DriveApp.getFileById("XXXX");

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

  body.replaceText("{fn}", info['First Name'][0]);
  body.replaceText("{ln}", info['Last Name'][0]);
  body.replaceText("{addr}", info['shipping address'][0]);
  body.replaceText("{qty}", info['Quantity Required'][0]);
  openDoc.saveAndClose();

  const blobPDF = newTempFile.getAs("application/pdf");
  const pdfFile = pdfFolder.createFile(blobPDF).setName(info['First Name'][0]);
  tempFolder.removeFile(newTempFile);
  return pdfFile;
}

Solution

  • From the quesiton:

    Log's say

    TypeError: Cannot read property 'namedValues' of undefined".

    Then I have 2 questions.

    1. How to Fix this error ?

    This error happens when the a funtion that requrires a paramenter, in this case AfterFormSubmit is the function and the required parameter is e, is executed from the script editor.

    If you need to run it from the script editor you have to declare this variable before using it in the code.

    function AfterFormSubmit (e){
      const e = {
        namedValues: // add here the appropriate litera, function or object
      }
    
      // add here the rest of the code
    
    }
    

    but your code apparently has other problems. One of them was already mentioned by Cooper in a comment to the question:

    I believe this const info = e.namedValues(info); should be this const info = e.namedValues["info"][0];

    Again, from the question

    1. Can this code autorun after New row added in google spreadsheet (This function i trying to create) error in line 3.

    After you fix the errors in the code it might be "autorun" by using the appropriate trigger. The question doesn't include enough details to let us tell which could appropriate.