I am trying to automate populating a report using only Google Apps. The sequence goes as followed:
Google Form > Google Sheets > Google Docs Template
The Google Form serves the purpose to enter all details for the final report: A quality score (1, 2 or 3) and some text. In Google Sheets, the quality score gets transformed into a text based label (#good#, #ok#, #bad#).
I've found a script here on Stack Overflow that picks up a Google Doc Template and populates the placeholder labels (%placeholder%) with the values in the Google Sheet. However, I haven't been able to solve for replacing the quality score label with an image/icon (a green, orange and red icon) that I have in my Google Drive. I got as far as replacing the label with the icon's file name (e.g. icon-green.png), but not the icon itself. Good to know is that the quality score label is placed within a table cell.
I'm incredibly new to javascript and could really use your help! I've indicated in the code beneath where I'm stuck exactly (towards the end).
// Replace this with ID of your template document.
var TEMPLATE_ID = '1AatkH57Iq3FwmsvZkCMYddjACItWS_XuO9sCsfcaMso'
// You can specify a name for the new PDF file here, or leave empty to use the
// name of the template.
var DOC_FILE_NAME = 'First GA Doc Template Try'
/**
* Eventhandler for spreadsheet opening - add a menu.
*/
function onOpen() {
SpreadsheetApp
.getUi()
.createMenu('Create Doc')
.addItem('Create Doc', 'createDoc')
.addToUi()
} // onOpen()
/**
* Take the fields from the active row in the active sheet
* and, using a Google Doc template, create a Doc with these
* fields replacing the keys in the template. The keys are identified
* by having a % either side, e.g. %Name%.
*
* @return {Object} the completed Doc file
*/
function createDoc() {
if (TEMPLATE_ID === '') {
SpreadsheetApp.getUi().alert('TEMPLATE_ID needs to be defined in code.gs')
return
}
// Set up the docs and the spreadsheet access
var copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy(),
iconGreen = DriveApp.getFileById('0B0C0sFLzFLyWOUVSeWxQa2o2MFE'),
copyId = copyFile.getId(),
copyDoc = DocumentApp.openById(copyId),
copyBody = copyDoc.getActiveSection(),
activeSheet = SpreadsheetApp.getActiveSheet(),
numberOfColumns = activeSheet.getLastColumn(),
activeRowIndex = activeSheet.getActiveRange().getRowIndex(),
activeRow = activeSheet.getRange(activeRowIndex, 1, 1, numberOfColumns).getValues(),
headerRow = activeSheet.getRange(1, 1, 1, numberOfColumns).getValues(),
columnIndex = 0
// Replace the keys with the spreadsheet values
for (;columnIndex < headerRow[0].length; columnIndex++) {
copyBody.replaceText('%' + headerRow[0][columnIndex] + '%',
activeRow[0][columnIndex])
}
copyBody.replaceText('#good#', iconGreen) // This is where I'm stuck..
copyFile.setTrashed(false)
SpreadsheetApp.getUi().alert('New Doc created in the root of your Google Drive')
} // createDoc()