title: Script functions but trigger fails
body: <p>I have a workflow where users can fill out a Google Form, then the associated Google Sheet information populates a Google Doc and sends an email.
  The script is fully functioning in all my tests, but when I try to set up an Installable Trigger, it fails time after time.</p>
  <pre><code>function CreatePro2() {
    
    // specify doc template and get values from spread
    var sleepINT = 1500
    var templateid = &quot;XXXXXXXXXXXXXXXXXX&quot;; // template file id
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    ss.toast(&quot;ENGINE INITIALIZING &amp; Feeding the unicorns&quot;);
    Utilities.sleep(sleepINT);
    var sheet = ss.getActiveSheet();
    var data = sheet.getRange(2,1,1,23).getValues(); // starting with row 2 and column 1 as our upper-left most column, 
                                                     // get values from cells from 1 row down, and 15 columns along - hence (2,1,1,15)
    //sheet.getRange(&quot;F7&quot;).setValue('=IMAGE(&quot;https://s-media-cache-ak0.pinimg.com/564x/58/5d/8f/585d8f802867c25df8f1ecc0cf7cadc8.jpg&quot;,1)');                                                 
    ss.toast(&quot;10%: data captured&quot;); 
    Utilities.sleep(sleepINT);
    
    
    //Create and set a counter for the invoice number, then grab the new invoice number
    var oldInvoiceNumber = sheet.getRange(&quot;G2&quot;).getValue();
    oldInvoiceNumber += 1;
    sheet.getRange(&quot;G2&quot;).clear();
    sheet.getRange(&quot;G2&quot;).setValue(oldInvoiceNumber);
    sheet.getRange(&quot;G2&quot;).setBackgroundColor(&quot;#cecece&quot;);
    
    var newInvNumber = sheet.getRange(&quot;G2&quot;).getValue();
    
    ss.toast(&quot;20%: invoice number updated&quot;); 
    Utilities.sleep(sleepINT);
    
    // Make a copy of the invoice template, then Fill up it up with the data from the spreadsheet.
    //NOTE: body.replace method does not have to be in any specific order.
    
    for (var i in data) {
       var replaceTextToImage = function(body, searchText, fileId) {
      var width = 590; // Please set this.
      var blob = DriveApp.getFileById(fileId).getBlob();
      var r = body.findText(searchText).getElement();
      r.asText().setText(&quot;&quot;);
      var img = r.getParent().asParagraph().insertInlineImage(0, blob);
      var w = img.getWidth();
      var h = img.getHeight();
      img.setWidth(width);
        img.setHeight(width * h / w);}
       
      var row = data[i];
      var docid = DriveApp.getFileById(templateid).makeCopy().getId();
      var doc = DocumentApp.openById(docid);
      var body = doc.getActiveSection();
      var Image = row[10].split(&quot;=&quot;)[1];
      var selectBody = doc.getBody();    
      
      
      body.replaceText(&quot;%NAME%&quot;, row[0]);
      body.replaceText(&quot;%ADD_LN1%&quot;, row[1]);
      body.replaceText(&quot;%EMAIL%&quot;, row[2]);
      body.replaceText(&quot;%PHONE%&quot;, row[3]);
      body.replaceText(&quot;%DATE%&quot;, row[4]);
      body.replaceText(&quot;%EXPDATE%&quot;, row[5]);
      body.replaceText(&quot;%INV_NUM%&quot;, row[6] +=1);
      body.replaceText(&quot;%PTOTAL%&quot;, row[7]);
      body.replaceText(&quot;%DESC1%&quot;, row[8]);
      body.replaceText(&quot;%FEE1%&quot;, row[9]);  
      replaceTextToImage(body, '%IMAGE%', Image);
      
      doc.saveAndClose();
      
      ss.toast(&quot;30%: template data replaced&quot;);
      Utilities.sleep(sleepINT);
      
      //copy the modified template to the specified folder, then delete the first copy we made (to modify it) 
      var file = DriveApp.getFileById(doc.getId());
      var newfolder = DriveApp.getFolderById(&quot;YYYYYYYYYYYYYYYYYYYY&quot;);
      var oldfolder = DriveApp.getFolderById(&quot;ZZZZZZZZZZZZZZZZZZZZ&quot;);
      newfolder.addFile(file);
      oldfolder.removeFile(file);
      
      ss.toast(&quot;40%: invoice has been put in correct folder&quot;);
      Utilities.sleep(sleepINT);
      
      //customize the title for the invoice
      var usernamefordoctitle = sheet.getRange(2, 1, 1, 1).getValues() // this is grabbing the customer name field (A2)
      var name = doc.getName();
      doc.setName(newInvNumber + ' - Invoice for ' + usernamefordoctitle);
      ss.toast(&quot;50%: named new invoice&quot;);
      Utilities.sleep(sleepINT);
  
      //create and organize pdf version
      var pdffolder = DriveApp.getFolderById(&quot;***************&quot;);
      var pdfFILE = DriveApp.getFileById(doc.getId()).getAs('application/pdf');
      pdfFILE.setName(doc.getName() + &quot;.pdf&quot;);
      var theFolder = pdffolder;
      var theFile = DriveApp.createFile(pdfFILE);
      theFolder.addFile(theFile);
      ss.toast(&quot;60%: PDF generated&quot;);
      Utilities.sleep(sleepINT);
      
      var email_status = sheet.getRange(&quot;C4&quot;).getValue();
      
      if (email_status == &quot;YES&quot; ) {
          //send a pdf copy to customer 
          var pdfEMAIL = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
        var emailName = sheet.getRange(&quot;A11&quot;).getValue()
        var emailToName = emailName; // add designer name here
          var message = &quot;Hi &quot; + emailToName + &quot;!, please find &quot; + usernamefordoctitle + &quot;'s project proposal attached. Reply to this email if you run into any issues.&quot;;
          var emailAdd = sheet.getRange(&quot;A10&quot;).getValue()
          var emailTo = emailAdd; // add designer email here
          var subject = &quot;Proposal for &quot; + usernamefordoctitle + &quot; by&quot; + emailToName + &quot; - Proposal No: &quot; + newInvNumber;
          
          var attach = {fileName:&quot;PROPOSAL &quot; + newInvNumber + &quot; &quot; + usernamefordoctitle + '.pdf',content:pdfEMAIL, mimeType:'application/pdf'};
          MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
          ss.toast(&quot;70%: emailed customer&quot;);
          Utilities.sleep(sleepINT);
          
          }
          
      else {
          
          ss.toast(&quot;No email sent&quot;);
          
          }
        }
    
  
    Utilities.sleep(sleepINT);
    ss.toast(&quot;90%: feeding the unicorns some more&quot;)
    Utilities.sleep(sleepINT);
    ss.toast(&quot;100%: high-fiving the neighbour&quot;)
    Utilities.sleep(sleepINT);
    ss.toast(&quot;SUCCESS Invoice &quot; + newInvNumber + &quot; has been created. It's in a new doc with the ID &quot; + docid); 
    Utilities.sleep(10000);
  }
  </code></pre>
  <p>When I run this script, everything works as expected and the email gets sent.
  When I try to use an Installed Trigger, it gives me the following error:</p>
  <p><code>Exception: Invalid argument: id at replaceTextToImage(Code:35:25) at CreatePro2(Code:62:5)</code></p>
  <p>I don't know javascript so I've just been piecing this together through trial and error so I apologize for the messy code.</p>
  <p>Is there an alternate trigger method I could use that wouldn't cause this issue?</p>
  <p>Any help would be appreciated!</p>