0
votes

I'm passing information from Google Sheets to a Document via Google Apps Script. All the information transfers with two exceptions...

  1. The link of the picture shows rather than the image. How can I insert the image rather than the link?

  2. In the folder it saves to, I get 2 versions of the document. One with the name I want, and another named 'Copy of Template'. I don't want the copy of the template, how can I either not create it, or delete it in the script?

Any help would be greatly appreciated.

 function myFunction(e) {

   var timestamp = e.values[0];
   var studentName = e.values[1];
   var firstName = e.values[2];
   var studentNumber = e.values[3];
   var dateOfBirth = e.values[4];
   var studentImage = e.values[5];

   var file = DriveApp.getFileById('fileId'); 

   var folder = DriveApp.getFolderById('folderId')
   var copy = file.makeCopy(studentName, folder); 

   var doc = DocumentApp.openById(copy.getId()); 

   var body = doc.getBody(); 

   body.replaceText('{{Time}}', timestamp);
   body.replaceText('{{Name}}', studentName);  
   body.replaceText('{{Preferred Name}}', firstName);
   body.replaceText('{{Student Number}}', studentNumber); 
   body.replaceText('{{DOB}}', dateOfBirth); 
   body.replaceText('{{Image}}', studentImage); 

   doc.saveAndClose(); 

 }

The expected output is a document with relevant information, an image of the student, and only 1 version of the document in the folder.

1

1 Answers

0
votes

I am unsure of why your code creates an extra copy of your file. This might be due to something else.

However, to change "{{image}}" to an image from an URL you can do this:

body.findText('{{Image}}').getElement().asParagraph()
  .setText("") //Clears text
  .appendInlineImage(UrlFetchApp.fetch(studentImage).getBlob()); //Adds image element from URL