I'm running an Apps Script in Google Sheets to Mail merge and generate a PDF file which is then store in Drive.
I have the following variable which stores the Google Doc that serves as template for the new PDF being generated.
var GDOC_TEMPLATE_ID = '1olnBowCh-1apK3jWN-Lr2Ty3UT1NK_dmoJOVBjnzgO8'
I would like to have a function that modifies the value of this variable so that it can point to a different file.
What I have been trying to do is the following script but haven't accomplished at all:
Note: My mail merge script runs by selecting row and gets all the place holders from the selected row, so when one of those fields is empty, I'm trying to point to a different template that suits better.
Thanks
I believe your goal is as follows.
1olnBowCh-1apK3jWN-Lr2Ty3UT1NK_dmoJOVBjnzgO8
as GDOC_TEMPLATE_ID
. When the value of column "N" is not empty, you want to use the other file ID as GDOC_TEMPLATE_ID
.In this case, how about the following modification? Please modify getSheetData()
as follows.
function getSheetData() {
copyFile = DriveApp.getFileById(GDOC_TEMPLATE_ID).makeCopy()
var copyId = copyFile.getId()
copyDoc = DocumentApp.openById(copyId)
copyBody = copyDoc.getActiveSection()
var activeSheet = SpreadsheetApp.getActiveSheet()
var numberOfColumns = activeSheet.getLastColumn()
activeRowIndex = activeSheet.getActiveRange().getRowIndex()
var activeRowRange = activeSheet.getRange(activeRowIndex, 1, 1, numberOfColumns)
activeRowValues = activeRowRange.getDisplayValues()
headerRow = activeSheet.getRange(1, 1, 1, numberOfColumns).getValues()
}
Please replace ### other ID ###
with your file ID.
function getSheetData() {
var activeSheet = SpreadsheetApp.getActiveSheet()
var numberOfColumns = activeSheet.getLastColumn()
activeRowIndex = activeSheet.getActiveRange().getRowIndex()
var activeRowRange = activeSheet.getRange(activeRowIndex, 1, 1, numberOfColumns)
activeRowValues = activeRowRange.getDisplayValues()
headerRow = activeSheet.getRange(1, 1, 1, numberOfColumns).getValues()
var templateIds = ['1olnBowCh-1apK3jWN-Lr2Ty3UT1NK_dmoJOVBjnzgO8', '### other ID ###'];
GDOC_TEMPLATE_ID = templateIds[activeRowValues[0][13] ? 1 : 0];
copyFile = DriveApp.getFileById(GDOC_TEMPLATE_ID).makeCopy()
var copyId = copyFile.getId()
copyDoc = DocumentApp.openById(copyId)
copyBody = copyDoc.getActiveSection()
}
1olnBowCh-1apK3jWN-Lr2Ty3UT1NK_dmoJOVBjnzgO8
is used as GDOC_TEMPLATE_ID
. When the value of column "N" is not empty, the other file ID ### other ID ###
is used as GDOC_TEMPLATE_ID
.