0
votes

I am reusing code Sandy Good provided to answer a similar question about how to email various cells from a spreadsheet. I have 2 calling functions as follows:

function onFormSubmit() {
  var sheet = getSpreadsheet().getSheetByName(RESPONSES_SHEET);
  handler = SheetHandler(sheet);
  handler.processSheet();
  writeToLog();

};

function prepForPayrollEntry() {
  var sheet = getSpreadsheet().getSheetByName(RESPONSES_SHEET);
  handler = SheetHandler(sheet);
  handler.generateListForPaychex();
};

The functions being called are both inside the Sheethandler() function:

function SheetHandler(sheet) {
  var _sheet = sheet;
  var _data = getRowsData(_sheet);

// Removed other functions not relevant to this code

  var processSheet = function() {
    _data.forEach(function(d) {
      if(d.state == BLANK_STATE) {
        _markPending(d);
      }
    });
  }

  var generateListForPaychex = function() {

    var allRowsData = "";
    var body = "",
      payPeriod,
      employeeName,
      numberOfHours,
      leaveType;


    _data.forEach(function(d) {

        if(d.state == APPROVED_STATE) { //Only list approved entries
        payPeriod = d.payrollPeriod;
        employeeName = d.yourName;
        numberofHours = d.numberOfHoursRequested;
        leaveType = d.typeOfLeave;

        body = ""; //Reset on every loop
        body = payPeriod + " " + 
               employeeName + " " +
               numberOfHours + " " + 
               leaveType +
               "\n";
        allRowsData = allRowsData + body;
      }
    });

    Logger.log(allRowsData);

    MailApp.sendEmail(SETTINGS.APPROVAL_NOTICE_EMAIL_TO,SETTINGS.PAYCHEX_LIST_FOR_REVIEW,allRowsData);

  }

The onFormSubmit() function works fine but when I run the prepForPayrollEntry() function I get an error saying TypeError: Cannot find function generateListForPaychex in object [object Object]. (line 542, file "Code").

The code I'm referencing came from Google Sheets content to email (lay out)

Would appreciate pointers on what I'm doing wrong.

1
Personally I'd turn this into a data type and initialize it by adding 'this' to each property in the SheetHandler function instead of 'var'. Then add the 'new' keyword anytime you call that function.Gerneio
I'm new to javascript programming, and so don't fully understand the 'this' and 'new' keywords. To minimize code changes, can you see why one function is seen but the other is not?Peter O'Riordan
That's the part I don't understand. I've personally never used a JavaScript function like you are right now. I don't have the ability to test it as is right now. But maybe later when I get time. Check this out.Gerneio

1 Answers

0
votes

At the end of the function definition for Sheethandler() is the following:

  return {
    'processSheet': processSheet,
    'approveByKey': approveByKey,
    'denyByKey': denyByKey,
  }
};

I added one more line to define the newly added function and now it works:

  return {
    'processSheet': processSheet,
    'approveByKey': approveByKey,
    'denyByKey': denyByKey,
    'generateListForPaychex' : generateListForPaychex

  }
};