TypeError: Cannot read property 'saveAndClose' of undefined

341 views
Skip to first unread message

marka...@gmail.com

unread,
Jul 24, 2022, 1:53:30 AM7/24/22
to Google Apps Script Community
 I'm hoping someone can tell me why this saveAndClose code that has not been changed in months is suddenly getting this error:

[22-07-23 22:41:04:664 PDT] TypeError: Cannot read property 'saveAndClose' of undefined
    at ProcForms(Code:372:12)

I went to run the script today and have gotten this error repeatedly. Below is the section of code in the script where this error is being flagged and this is the only saveAndClose statement in the script. I don't see what is wrong and why it suddenly gets this error when this exact code has been working until today and has not been changed.

//Make a copy of the template file and store in the appropriate subfolder.
   
   var folder = DriveApp.getFolderById(sfid);
   var mimetyp = DriveApp.getFileById(fileId).getMimeType() ;  // Get the document  MimeType to see if file is a PDF/JPEG
   var documentId = DriveApp.getFileById(fileId).makeCopy(pcode + ' ' + form, folder).getId();
   if (mimetyp == 'application/pdf' || mimetyp == 'image/jpeg') continue ;  // If document is a PDF or JPEG then do no further processing after copy.
   var copyDoc = DocumentApp.openById(documentId);
   
   //Get the document body as a variable and replace variables with agent values or blanks.
   var ninfo = PropertiesService.getScriptProperties().getProperty("ninfo")
   if(ninfo == 'no') {
    var body = DocumentApp.openById(documentId).getBody();
    body.replaceText('##First##' , fname) ;
    body.replaceText('##Last##' , lname) ;
    body.replaceText('##mobile##' , mobile) ;
    body.replaceText('##dre##' , dre) ;
    body.replaceText('##emaddr##', emaddr) ;
    body.replaceText('##sep##' , '||') ;
    body.replaceText('##apos##' , "'s") ;
   }
   else {
    if(ninfo == 'yes') {
    var body = DocumentApp.openById(documentId).getBody();
    body.replaceText('##sep##' , '       ') ;
    body.replaceText('##First##' , '         ') ;
    body.replaceText('##Last##' , '        ') ;
    body.replaceText('##mobile##' , '         ') ;
    body.replaceText('##dre##' , '       ') ;
    body.replaceText('License #' , '         ') ;
    body.replaceText('##apos##' , '        ') ;
    body.replaceText('DRE' , '   ') ;
    }
   }
  }
   copyDoc.saveAndClose();     <== This is statement 372

An help would be appreciated. Thanks

 Mark
   

ShiizophreN

unread,
Jul 24, 2022, 2:44:53 AM7/24/22
to Google Apps Script Community
try this

   function test(){
    var folder = DriveApp.getFolderById(sfid);
    var mimetyp = DriveApp.getFileById(fileId).getMimeType() ; // Den MimeType des Dokuments abrufen, um zu sehen, ob die Datei eine PDF/JPEG-Datei ist

    var documentId = DriveApp.getFileById(fileId).makeCopy(pcode + ' ' + form, folder).getId();
        if (mimetyp == 'application/pdf' || mimetyp == 'image/jpeg') Continue ; // Wenn das Dokument ein PDF oder JPEG ist, dann keine weitere Verarbeitung nach dem Kopieren.
        var copyDoc = DocumentApp.openById(documentId);
   
   //Den Dokumentkörper als Variable abrufen und Variablen durch Agentenwerte oder Leerzeichen ersetzen.

    var ninfo = PropertiesService.getScriptProperties().getProperty("ninfo")
        if(ninfo == 'no'){
        var body = DocumentApp.openById(documentId).getBody();
            body.replaceText('##First##' , fname) ;
            body.replaceText('##Letzte##' , lname) ;
            body.replaceText('##mobile##' , mobil) ;

            body.replaceText('##dre##' , dre) ;
            body.replaceText('##emaddr##', emaddr) ;
            body.replaceText('##sep##' , '||') ;
            body.replaceText('##apos##' , "'s") ;
        }
        else if(ninfo == 'yes') {
        var body = DocumentApp.openById(documentId).getBody();
            body.replaceText('##sep##' , ' ') ;
            body.replaceText('##Erster##' , ' ') ;
            body.replaceText(' ##Letzte##' , ' ') ;

            body.replaceText('##mobile##' , ' ') ;
            body.replaceText('##dre##' , ' ') ;
            body.replaceText('Lizenz #' , ' ') ;

Mark Anthony Amezquita

unread,
Jul 24, 2022, 5:36:14 PM7/24/22
to google-apps-sc...@googlegroups.com
Thank you.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/1935bf16-cb70-4288-b8c0-91bd51eac700n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages