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:
- Drop down menu in sheets > click button
- Scripts locates a Doc template with no body content > copies it, renames it, saves it to the same folder that the template is in
- 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.