Home > database >  Insert Image From Google Form to Google Slides
Insert Image From Google Form to Google Slides

Time:06-04

I am trying to make a report that's generated from google form answers. I found some way to populate the text but the problem is i need to add the images from google form answers on my report in google slides. I am not a developer so can anyone help me with it? Here's the code that i've tried:

function onFormSubmit(e) {
  //open the template presentation by ID
  var templateDoc = DriveApp.getFileById('1ytA5aje8eTW4tQOf9U_DCKlpLrnLYZna3KIvjz9rx1Q');
  //create a copy of the template
  var newTempFile = templateDoc.makeCopy();
  
  //open the presentation for editing
  var openSlide = SlidesApp.openById(newTempFile.getId());

  //get the responses triggered by On Form Submit
  var items = e.response.getItemResponses();
  //var amountof_item = items[4].getResponse();

  //find the text in the presentation and replace it with the Form response
  //items[0].getResponse() is the first response in the Form
  openSlide.replaceAllText('{PM In Charge :}', items[0].getResponse());
  openSlide.replaceAllText('{Homeowner Name :}', items[1].getResponse());
  openSlide.replaceAllText('{Location :}', items[2].getResponse());
  openSlide.replaceAllText('{Week :}', items[3].getResponse());
  openSlide.replaceAllText('{Report Date :}', items[4].getResponse());
  openSlide.replaceAllText('{Start Date :}', items[6].getResponse());
  openSlide.replaceAllText('{End Date :}', items[7].getResponse());
  openSlide.replaceAllText('{Rooms :}', items[8].getResponse());
  openSlide.replaceAllText('{Work Item :}', items[9].getResponse());
  openSlide.replaceAllText('{Work Item Progress :}', items[10].getResponse());
  openSlide.replaceAllText('{Work Item File/Image :}', items[11].getResponse());

  //Save and Close the open document
  openSlide.saveAndClose();
  DriveApp.getFileById(newTempFile.getId()).setName(items[1].getResponse()   "-"   items[3].getResponse()   "-Progress");
 }

CodePudding user response:

I believe your goal is as follows.

  • In your Google Form and your script, only items[11].getResponse() is the uploaded image.
  • Your Google Slides has multiple slides.

When replaceAllText is used, the text is replaced. In this case, the text cannot be replaced with the image. And, I think that the value of items[11].getResponse() is the file ID. This is the reason for your issue. In your script, how about the following modification?

From:

openSlide.replaceAllText('{Work Item File/Image :}', items[11].getResponse());

To:

var image = items[11].getResponse();
openSlide.getSlides().forEach(s => {
  s.getShapes().forEach(e => {
    if (e.getText().asString().trim() == '{Work Item File/Image :}') {
      e.replaceWithImage(DriveApp.getFileById(Array.isArray(image) ? image[0] : image).getBlob());
    }
  })
});
  • When this script is run, the file ID of the image file is retrieved from items[11].getResponse(), and retrieve the blob from the file, and then, the image blob is replaced with the text of {Work Item File/Image :} of each page.

References:

  • Related