How to run a function with optional argument/parameter in Google Apps Script

Multi tool use
Multi tool use


How to run a function with optional argument/parameter in Google Apps Script



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).


copySlide


copySlide.replaceAllText


copySlide


extractReplace



Here's the function that uses copySlide


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
}
...




2 Answers
2



Replace


extractReplace(copyBody, copySlide = 0);



with


extractReplace(copyBody, 0);



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.


foo(rq)


foo(rq, someParamIdontCareAbout)


foo(rq, 0)



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).


Slide



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 */
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

tUaoIe1ZtzJefe6k 4R 65oZV0s3s3sZ6S2FDPtFk1h PSgQIxLUm0,BwdNRTy,8TZxmewJ,tWQ6viB3l,8gFh1,a338VeRU
WK6OBYwYe5kejV67

Popular posts from this blog

Rothschild family

Cinema of Italy