title: Cannot Find Function In Object
body: <p>I'm developing a Google Sheets application to replace my subscription task manager. The Script works for daily, weekly, bimonthly, quarterly, semiannually, and yearly. The Script fails for "Every Weekday" with the error:</p>
  
  <blockquote>
    <p>TypeError: Cannot find function add in object Mon Dec 28 2015 00:00:00 GMT-0600 (CST). (line 69, file "Code").</p>
  </blockquote>
  
  <p>The prototype function seems to work fine in the daily, weekly, etc. sections of the code but bombs in the "Every Weekday" section. I've tried moving the prototype function into different sections to no avail.</p>
  
  <p>The structure of the Sheet is as follows:</p>
  
  <ul>
  <li>Each row represents a discrete task</li>
  <li>Column A = "X" indicates the task is complete</li>
  <li>Column C describes the task</li>
  <li>Column I contains the repeat date as mm/dd/yyyy</li>
  <li>Column K contains the repeat type (daily, weekly, etc.)
  <img src="https://i.stack.imgur.com/AT2xk.png" alt="screenshot"></li>
  </ul>
  
  <p>Here is my code:</p>
  
  <pre class="lang-js prettyprint-override"><code>// Declare global variables
  var ws = SpreadsheetApp.getActiveSheet();
  var lRow = ws.getLastRow();
  var lCol = ws.getLastColumn()
  var range = ws.getRange(lRow, 1, 2, lCol);
  
  // Create Custom menu in worksheet
  function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('Custom')
      .addItem('Complete Current Task', 'compTask')
      .addItem('Duplicate Task', 'dupeTask')
      .addItem('Re-sort', 'customSort')
      .addToUi();
  }
  
  function compTask() {
  
    var booFound; //declares booFound as False
    var actRow = ws.getActiveRange().getRow();
    var dateDue = ws.getRange('I' + actRow).getValue();
    var strRepeat = ws.getRange('K' + actRow).getValue();
    var dateRepeat = new Date();
    var dateNow = new Date();
  
    if (strRepeat == 'Daily') {
      booFound = true;
      dateRepeat = dateDue.add("d", 1);
    }
  
    if (strRepeat == 'Weekly') {
      booFound = true;
      dateRepeat = dateDue.add("d", 7);
    }
  
    if (strRepeat == 'Biweekly') {
      booFound = true;
      dateRepeat = dateDue.add("d", 14);
    }
  
    if (strRepeat == 'Monthly') {
      booFound = true;
      dateRepeat = dateDue.add("mo", 1);
    }
  
    if (strRepeat == 'Bimonthly') {
      booFound = true;
      dateRepeat = dateDue.add("mo", 2);
    }
  
    if (strRepeat == 'Quarterly') {
      booFound = true;
      dateRepeat = dateDue.add("mo", 3);
    }
  
    if (strRepeat == 'Semiannually') {
      booFound = true;
      dateRepeat = dateDue.add("mo", 6);
    }
  
    if (strRepeat == 'Yearly') {
      booFound = true;
      dateRepeat = dateDue.add("y", 1);
    }
  
    if (strRepeat == 'Every Weekday') {
      booFound = true;
      if (dateDue.getDay() &lt; 5) {
        dateRepeat = dateDue.add("d", 1);
      } else {
        dateRepeat = dateDue.add("d", 8);
        var dayNumber = dateDue.getDay();
        dayNumber = ~dayNumber + 1;
        dateRepeat = dateRepeat.add("d", dayNumber);
      }
    }
  
    if (booFound) {
      var range = ws.getRange(actRow, 1, 1, lCol);
      ws.insertRowsAfter(actRow, 1);
      range.copyTo(ws.getRange(actRow + 1, 1, 1, lCol), {
        contentsOnly: false
      });
      ws.getRange('A' + actRow).setValue('X');
      ws.getRange('J' + actRow).setValue(dateNow);
      ws.getRange('I' + (actRow + 1)).setValue(dateRepeat);
    } else {
      ws.getRange('A' + actRow).setValue('X');
      ws.getRange('J' + actRow).setValue(dateNow);
    }
  
  }
  
  Date.prototype.add = function(sInterval, iNum) {
    var dTemp = this;
    if (!sInterval || iNum == 0) return dTemp;
    switch (sInterval.toLowerCase()) {
      case "ms":
        dTemp.setMilliseconds(dTemp.getMilliseconds() + iNum);
        break;
      case "s":
        dTemp.setSeconds(dTemp.getSeconds() + iNum);
        break;
      case "mi":
        dTemp.setMinutes(dTemp.getMinutes() + iNum);
        break;
      case "h":
        dTemp.setHours(dTemp.getHours() + iNum);
        break;
      case "d":
        dTemp.setDate(dTemp.getDate() + iNum);
        break;
      case "mo":
        dTemp.setMonth(dTemp.getMonth() + iNum);
        break;
      case "y":
        dTemp.setFullYear(dTemp.getFullYear() + iNum);
        break;
    }
    return dTemp;
  
  }
  
  function customSort() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var allData = sheet.getDataRange();
    var range = sheet.getRange(2, 1, allData.getNumRows() - 1, allData.getNumColumns());
    range.sort([1, 9, 12]);
    ws.setActiveCell(ws.getRange(2, 1));
  }
  
  function dupeTask() {
    var actRow = ws.getActiveRange().getRow();
    var range = ws.getRange(actRow, 1, 1, lCol);
    ws.insertRowsAfter(actRow, 1);
    range.copyTo(ws.getRange(actRow + 1, 1, 1, lCol), {
      contentsOnly: false
    });
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/5UVqO.png" alt="compTask run in debugger showing variable types."></p>