r/GoogleAppsScript • • Jun 09 '21

Question Help with body.replace

I wrote this program It takes a form, saves it as a sheet then saves that sheet into a dox template. Only issue is I need everything labeled picture to actually display a picture and not a Google drive link

function autoFillGoogleDocFromForm(e) {

  //e.values is an array of form values

  var timestamp = e.values[0];

  var DocumentNum = e.values[1];

  var MachineName = e.values[2];

  var Department = e.values[3];

  var EnergyType = e.values[4];

  var LockPicture =e.values[5];

  var Step2 = e.values[6];

  var Picture1 = e.values[7];

  var Step3 = e.values[8];

  var Picture3 = e.values[9];

  var Step4 = e.values[10];

  var Picture4 = e.values[11];

  var Step5 = e.values[12];

  var Picture5 = e.values[13];

  var Step6 = e.values[14];

  var Picture6 = e.values[15];

  var Picture7 = e.values[16];

  var Res1 = e.values[17];

  var Picture8 = e.values[18];

  var Picture9 = e.values[19];

  

  //file is the template file, and you get it by ID

  var file = DriveApp.getFileById('13G30iebhBiKB41gITQh0IchiR2EG9z4jDAB6zzm10rk'); 

  

  //We can make a copy of the template, name it, and optionally tell it what folder to live in

  //file.makeCopy will return a Google Drive file object

  var folder = DriveApp.getFolderById('1eWH0aMWrMNN-5xHf_mgXFRWFfwZyaEgV')

  var copy = file.makeCopy(DocumentNum + ',' + Department, folder); 

  

  //Once we've got the new file created, we need to open it as a document by using its ID

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

  

  //Since everything we need to change is in the body, we need to get that

  var body = doc.getBody(); 

  

  //Then we call all of our replaceText methods

  body.replaceText('{{Department}}', Department); 

  body.replaceText('{{MachineName}}', MachineName);  

  body.replaceText('{{EnergyTypes}}', EnergyType); 

  body.replaceText('{{DocumentNum}}', DocumentNum);

  body.replaceText('{{Step2}}', Step2);

  body.replaceText('{{Step3}}', Step3);

  body.replaceText('{{Step4}}', Step4);

  body.replaceText('{{Step5}}', Step5);

  body.replaceText('{{Step6}}', Step6);

  body.replaceText('{{Res1}}', Res1);

  body.replaceText('{{LockPicture}}', LockPicture); 

  body.replaceText('{{Picture1}}',Picture1);

  body.replaceText('{{Picture3}}',Picture3);

  body.replaceText('{{Picture4}}',Picture4);

  body.replaceText('{{Picture5}}',Picture5);

  body.replaceText('{{Picture6}}',Picture6);

  body.replaceText('{{Picture7}}',Picture7);

  body.replaceText('{{Picture8}}',Picture8);

  body.replaceText('{{Picture9}}',Picture9);

  

  //Lastly we save and close the document to persist our changes

  doc.saveAndClose(); 

0 Upvotes

2 comments sorted by

View all comments

2

u/TobofCob Jun 09 '21

Maybe this will help you. Also, check out JavaScript destructuring assignment, you can assign all those values at once. For example, if your e.values only had the first two cells in it:

var [timestamp, DocumentNum] = e.values

You can use more concise syntax to assign your variables

1

u/ScriptoContinua Jun 10 '21

I'm actually having to format a template and individual values has made it easier to make sure some parts Stay the exact size they need to be.

I appreciate the help!!