代码之家  ›  专栏  ›  技术社区  ›  Sam Murdock

我一直收到TypeError:无法读取undefined的属性(读取“0”)

  •  0
  • Sam Murdock  · 技术社区  · 10 月前

    尝试将谷歌表单转换为pdf,然后通过电子邮件发送。目前,它将数据存储到数组中,然后稍后获取该信息,将其放入文档而不是表单中。

    当它抓取信息时,就会不断抛出错误。

    
    function createPDF(){
    // all info collected on the form, pre-filled to test
    const info = {
      'First Name' : ['Mike'],
      'Last Name' : ['Wazowski'], 
      'Birthday' : [0], 
      'Email' : ['[email protected]'],
      'Phone Number' : ['1'],
      'Province' : ['Ontario'],
      'Contact Method' : ['email'],
      'Language': ['english'],  
      'Type Of Service': ['in person'],
      'Child Name': [],
      'Child Birthday': [],
      'Services Required': ['counselling'],
      'Staff Requested': ['Nancy'],
      'Priority':['Health'],
      'Referral': ['blank'],
      'Jane Consent': ['Yes'],
      };
    
    
    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();
    
      body.replaceText("{fn}", info['First Name'][0]);
      body.replaceText("{ln}", info['Last Name'][1]); /*throwing an error at this line, specifically Error  
    Exception: Invalid argument: replacement
    createPDF   @ Code.gs:36 */
    
      body.replaceText("{bd}", info['Birthday'][2]);
      body.replaceText("{em}", info['Email'][3]);
      body.replaceText("{pn}", info['Phone Number'][4]);
      body.replaceText("{pv}", info['Province'][5]);
      body.replaceText("{cm}", info['Contact Method'][6]);
      body.replaceText("{lg}", info['Language'][7]);
      body.replaceText("{ts}", info['Type of Service'][8]);
      body.replaceText("{cn}", info['Child Name'][9]);
      body.replaceText("{cbd}", info['Child Birthday'][10]);
      body.replaceText("{sr}", info['Services Required'][11]);
      body.replaceText("{stf}", info['Staff Requested'][12]);
      body.replaceText("{pri}", info['Priority'][13]);
      body.replaceText("{ref}", info['Referral'][14]);
      body.replaceText("{jc}", info['Jane Consent'][15]);
    
    const blobPDF = newTempFile.getAs(MimeType.pdf);
    const pdfFile = pdfFolder.createFile(blobPDF).getName("Form Response");
    
    
    }
    

    起初我认为生日是一个问题,因为它不是字符串,而是表单上的日期,但在我尝试用字符串替换它之后,它仍然不起作用。

    我试图模仿这个家伙: https://www.youtube.com/watch?v=EpZGvKIHmR8&ab_channel=LearnGoogleSheets%26ExcelSpreadsheets

    但他的作品和我的作品没有,唯一的区别是我拥有的数据量,以及我使用的是日期而不是字符串。

    1 回复  |  直到 10 月前
        1
  •  1
  •   Ray Wallace    10 月前

    您的所有信息值都是只有一个元素的数组,您正试图访问除此之外的元素。所以你需要改变你的 body.replaceText() 调用使用索引[0]。

      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'][00]);
      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]);