1

I have a function that makes a copy of an existing document (template) and then merges data in dynamically by matching the header names to the tags listed within the document. The function worked without any problems, but now suddenly I'm receiving an error message whenever it tries to merge. Can anyone give me some insight into what the issue might be?

Error Message: Exception: Invalid argument: replacement

The weird thing is that it doesn't prevent the information from merging, but the error does stop the function from completing the other tasks.

Line with the error

headers.forEach(function(e){
     body.replaceText("<<"+e+">>",data[e]);
    return;
  });

The whole code:

function documents(sheet, data){
     var headers = Object.keys(data[0]);     
     var docsToMerge = data.map(function(e){      
       var name = e.location +" - "+e.employeeLastName+", "+e.employeeFirstName+" - "+e.docName+" "+Utilities.formatDate(new Date(e.effectivePayDate), "UTC-4", "M/d/yy");
       var newDoc = DriveApp.getFileById(e.template).makeCopy(name, DriveApp.getFolderById(e.folderId));  
       e.documentLink = newDoc.getUrl();
       e.documentId = newDoc.getId();
       return e;
     });

      docsToMerge.forEach(function(e){
        mergeDocuments(e, headers, signatureFolderId);
      });
}

function mergeDocuments(data, headers){
  var id = DocumentApp.openByUrl(data.documentLink).getId();
  var doc = DocumentApp.openById(id);      
  var body = doc.getBody();

  headers.forEach(function(e){
     body.replaceText("<<"+e+">>",data[e]);
    return;
  });
  doc.saveAndClose();
  return;
}
Niya
  • 79
  • 1
  • 1
  • 12
  • Can you share an image of your spreadsheet? And headers doesn't seem to be defined anywhere? – Cooper Mar 03 '20 at 19:02
  • Can you provide more details about docsToMerge? – Cooper Mar 03 '20 at 19:08
  • Yes! Sorry, I've updated it with the missing info. – Niya Mar 03 '20 at 22:04
  • Hello @Niya, which runtime environment are you using? Is it Rhino or V8? Cheers! – ale13 Mar 04 '20 at 16:03
  • It was V8 which ended up being the problem. Since google is migrating everyone over automatically, I'm going to need to rearrange my code a bit so that even with the error, it completes the script. It'll be a temporary fix until the problem is fixed. I feel like it's a glitch of some kind... Thank you!! – Niya Mar 04 '20 at 16:40

1 Answers1

8

Desactive Runtime V8 in Run section of your script.

  • That did it! I thought that might be the issue but didn't actually think I could deactivate it since they're migrating every automatically. Thank you! – Niya Mar 04 '20 at 16:39