0

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;
}
  • I believe this `const info = e.namedValues(info);` should be this `const info = e.namedValues["info"][0];` – Cooper Apr 05 '21 at 03:31
  • The code run after being triggered by onFormSubmit trigger which provide the event object e – Cooper Apr 05 '21 at 03:36
  • Related: https://stackoverflow.com/q/66548048/1595451 (no answers at this time), https://stackoverflow.com/q/66578662/1595451 – Rubén Apr 05 '21 at 03:52
  • You might missed that the code is intended to be used with on form submit trigger. The `e` on `AfterFormSubmit (e)` is intended to get the event object from that trigger., but the code has some problems i.e. `nameValues` is not a function. What is the original source of the code in your question? – Rubén Apr 05 '21 at 04:02
  • This seems quite similar to a question I answered [earlier today][1]. If you're trying to run code based on Spreadsheet Event change, you might need to define your range before getting values, rows, etc. [1]: https://stackoverflow.com/questions/66942371/trigger-google-script-without-form-submission/66942741?noredirect=1#comment118332905_66942741 – pgSystemTester Apr 05 '21 at 04:09

1 Answers1

0

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.

Rubén
  • 34,714
  • 9
  • 70
  • 166