0

What I am trying to do is use an HTML file (that exists within Apps Scripts of a Google sheet) to populate the body of an auto generated Doc.

Process goes like this:

  1. Drop down menu in sheets > click button
  2. Scripts locates a Doc template with no body content > copies it, renames it, saves it to the same folder that the template is in
  3. Closes the Doc.

I can't figure out how to populate the Doc body with the HTML file.

This is what I have right now: (htmlTemplate() is called when drop down button in sheets is clicked)

My HTML file is called practice-template.HTML

function htmlTemplate() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheetTemplate = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('template')
  var DOC_ID = '1I5q7BnAdj-KcF10Q6N9TgemMZt0Vkq15ipUfhA_iJTI'

  // opens Doc Template in Drive
  // var template = DocumentApp.openById(DOC_ID)

  // open in Docs when you're saving/creating a new doc
  var temploc = DriveApp.getFileById(DOC_ID);


  var dataRange = sheetTemplate.getRange('A1:H1').getValues();
  //get data from cells
  for (i in dataRange) {
    var rowData = dataRange[i]
    var dataOne = rowData[0]
    var dataTwo = rowData[1]
    var dataThree = rowData[2]
  }

   // Get ID of Active sheet
  var ssloc = DriveApp.getFileById(sheet.getId());
 
  // Get root directory of active workbook
  var ssfolder = ssloc.getParents().next();

  // makes a new google doc based on the template and named appropriately, in the same folder as the template
  var newcopy = temploc.makeCopy(dataOne,ssfolder);
  var newcopyurl = newcopy.getUrl();
  var doc = DocumentApp.openByUrl(newcopyurl);

  var docBody = doc.getBody()

  var htmlBody = HtmlService.createTemplateFromFile('practice-template')

  var data = {
    data_one: dataOne,
    data_two: dataTwo,
    data_three: dataThree,
  }
  htmlBody.data = data;
  var content = htmlBody.evaluate().getContent();
  
  docBody.replaceText("{{body}}",content)
  doc.saveAndClose()
}

When I run the above code I get a new google Doc in the appropriate folder, and the document is populated with the HTML from the practice-html.html file. The problem is that the text in the new google doc is in html syntax and not plain text.

The google Doc template file only has {{body}} as content.

mrerikmt
  • 1
  • 2
  • In your script, `htmlBody.evaluate()` returns `HtmlOutput` object. If you want to use the HTML of text, please modify `var content = htmlBody.evaluate().getContent()`. But, in your showing script, `sheetTemplate` is not declared. So, I think that your script occurs an error at `var dataRange = sheetTemplate.getRange('A1:H1').getValues();`. So, from `I get an error message about docBody.replaceText(content)`, I'm worried that you might have miscopied your current script for correctly replicating your current issue. How about this? – Tanaike Nov 07 '22 at 02:34
  • And also, about `htmlBody.data = data`, in this case, can you provide your HTML `practice-template`? By the way, your goal is to put the HTML data (unrendered raw HTML text data) into Google Document. Is my understanding correct? – Tanaike Nov 07 '22 at 02:34
  • Thanks for responding. I have made some progress and edited my question to reflect where I am at with this. Does this clarify things at all? – mrerikmt Nov 21 '22 at 00:26
  • Thank you for replying. Now, I noticed that an answer has already been posted. In this case, I would like to respect the existing answer. – Tanaike Nov 21 '22 at 00:36

1 Answers1

0

The code in the question is using DocumentApp.Body.replaceText but it passed only one parameter while this method requires two, both parameters should be string. By the way, the content variable have assigned a HtmlOutput object, not a string.

You could use HTML to create a file in Google Drive, then convert that file to Google Docs format, but it's very likely that the best approach is to use a Google document as template, or if you need to create the content programmatically, then use Documents Service (Class DocumentApp), the Advanced Google Docs Service or the Google Docs API.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Thanks for responding. I'm still struggling with this. It does not appear to me that Documents Service or the Advanced Documents Service will help me to solve my problem. I have looked into the Google Docs API and there is quite a lot to learn in order to use it. I don't want to spend time learning that if it won't help me solve my problem. Any idea if it will help me to render HTML to a newly created Google Doc? Thanks – mrerikmt Nov 21 '22 at 01:22