I am trying to run a function which has an optional argument/parameter in Google Apps Script. The function will create a Google Document and will also create a Google Slide if the user selects Yes. My problem is when the extractReplace function is being called, an error will occur because the copySlide is not defined.
Here's how I'm calling the function:
var copyId = DriveApp.getFileById(documentTemplate)
.makeCopy(documentName + folderName, nsdestinationFolder)
.getId();
var copyDocument = DocumentApp.openById(copyId);
var copyBody = copyDocument.getActiveSection();
//call function to create technical slide proposal
if(createTemplate == "Yes"){
createSlideProposal();
}
else{
extractReplace(copyBody, copySlide = 0);
}
I have tried copySlide = 0, false, null, but it did not work as the copySlide is not defined when is passes to the copySlide.replaceAllText line. There is another function which includes the copySlide variable but it will only be called and passed to the extractReplace function if the user select "Yes" to create the slide proposal (powerpoint).
Here's the function that uses copySlide
function extractReplace(copyBody, copySlide) {
var result = ""; //result variable string = 0
var ss = SpreadsheetApp.openById("IDKEYHERE");
var firstDatabasesheet = ss.getSheets()[0];
var data = firstDatabasesheet.getDataRange().getValues();
data.forEach(function(row) {
if (row[32] == TMSreferenceNumber){
result = row[2] || row[16]; //chooses whichever cell that has something in it
copyBody.replaceText('repbranchOffice', result); //replaces text in document template
result = row[3] || row[17];
copyBody.replaceText('repsalesContact', result);
result = row[4] || row[18];
copyBody.replaceText('repbranchReferenceNumber', result);
result = row[5] || row[19];
copyBody.replaceText('rependUser', result);
copySlide.replaceAllText('rependUser', result); //for slide
}
...