Reputation: 271
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
}
...
Upvotes: 0
Views: 5888
Reputation: 9872
When working with optional arguments, you generally need to test for their absence if you do not provide a default value.
At the call site, you generally want to be able to omit parameters if they are optional and not important to you: foo(rq)
vs. foo(rq, someParamIdontCareAbout)
or foo(rq, 0)
, since it reduces the complexity of your call site code.
In the function with optional parameters, you want to initialize the values if they are needed (e.g. primitives, flags), or guard code blocks which interact with an optional class argument (like a reference to a Slide
object).
Initialization:
function foo(required, startVal = /* something */) {
...
}
function foo(required, startVal) {
if (startVal === undefined) {
/* more than a one-statement initialization */
}
...
}
Block guards
function foo(required, objectRef) {
/* Code that does not need the object reference */
if (objectRef) {
/*
* Code that needs the object reference
* objectRef.someMethod(...);
*/
}
/* More code that does not need the object reference */
}
Upvotes: 2
Reputation: 11278
Replace
extractReplace(copyBody, copySlide = 0);
with
extractReplace(copyBody, 0);
Upvotes: 0