Does Google Script work with Google Docs that contain eSignature fields?

20 views Asked by At

I'm using an Apps script to:

  1. make a copy of a template Doc to another doc
  2. fill the newly made Doc with data from a Sheet (that's triggered by a Form entry but that's not relevant).

Both of these operations work when the Doc is a simple, text-only one - but when adding eSignature fields to it the script fails. I'm looking for a workaround or maybe a different API to use for this.

Here's my simplified code, nothing fancy in here.

function autofillCopiedTemplate(e) {

  var timestamp = e.values[0];
  var name = e.values[1];
  // more fields ...
  
  //file is the template file, and you get it by ID (id is in the URL)
  var file = DriveApp.getFileById('<TEMPLATE_FILE_ID>'); 
  
  //We can make a copy of the template, name it, and tell it what folder to live in
  //file.makeCopy will return a Google Drive file object
  var folder = DriveApp.getFolderById('<STORAGE_FOLDER_ID>');
  var copy = file.makeCopy(name + '.' + timestamp, folder); 

  //Once we've got the new file created, we need to open it as a document by using its ID
  var doc = DocumentApp.openByUrl(copy.getUrl()); // <--------- this fails
  
  //Since everything we need to change is in the body, we need to get that
  // Debug note: doc type should be application/vnd.google-apps.document
  var body = doc.getBody(); 
  
  //Then we call all of our replaceText methods on template placeholders
  body.replaceText('{{Name}}', name); 
  // More replacements ...
  
  //Lastly we save and close the document to persist our changes
  doc.saveAndClose(); 
}

The call fails with a Exception: Unexpected error while getting the method or property openByUrl on object DocumentApp. Again, this works if there are no eSignature fields in the copied doc.

I have not been able to find anything on this. eSignature may still be a relatively new product for Google and is only available on an individual workspace account. It works fine, except for this.

Removing the fields certainly worked. Also tried openById rather than openByUrl but it's probably the same underlying call so that made no difference.

0

There are 0 answers