0
votes

[How to insert picture to google docs using the url link coming from google form response spreadsheet]

I need help, I have a google form that allow the user to upload their photo. My problem is, I have to link the google docs report to the url photo under the column [11] of the response spreadsheet.

here is my code

var Photo = e.values[11];
      
  var templateFile = DriveApp.getFileById("link-of-docReport");
  var templateResponseFolder = DriveApp.getFolderById("this-is-the-path-for-uploaded-photo");
  var copy = templateFile.makeCopy(LastName + ", " + FirstName, templateResponseFolder);
  
  var doc = DocumentApp.openById(copy.getId());
  
  var body = doc.getBody();
  
  body.replaceText("Photo", Photo);
1
Are the usrs providing you an URL of a picture on THEIR drive or do they upload the picture as an attachment, so a file is created on YOUR drive?ziganotschka

1 Answers

0
votes

Using replaceText will replace a string through another string - not through an actual photo

Instead you need to

  • find the placeholder as an element
  • find its parent
  • insert the picture into the parent
  • delete the placeholder element

Sample how to insert a file that it on your drive where the id is known:

  var blob = DriveApp.getFileById("id").getBlob();
  var body = doc.getBody();
  var element = body.findText("Photo").getElement();
  element.getParent().asParagraph().insertInlineImage(1, blob);
  element.removeFromParent();

To extract the id from the url of a file on your drive, you nee to use regex, e.g. as done here.