无法在对象中查找函数

时间:2015-12-28 18:55:11

标签: javascript google-apps-script prototype

我正在开发Google表格应用程序来替换我的订阅任务管理器。脚本适用于每日,每周,每两个月,每季度,每半年和每年。脚本失败了#34;每个工作日"错误:

  

TypeError:找不到对象添加对象Mon Dec 28 2015 00:00:00 GMT-0600(CST)。 (第69行,文件"代码")。

原型功能似乎在代码的每日,每周等部分工作正常,但是在每个工作日"部分。我已经尝试将原型功能移动到不同的部分但无济于事。

Sheet的结构如下:

  • 每行代表一个离散任务
  • A列=" X"表示任务已完成
  • C列描述了任务
  • 第I列包含重复日期为mm / dd / yyyy
  • 列K包含重复类型(每日,每周等) screenshot

这是我的代码:

// 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() < 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
  });
}

compTask run in debugger showing variable types.

0 个答案:

没有答案