表格未定义

时间:2015-06-18 15:01:41

标签: javascript google-apps-script google-sheets undefined validation

我有一张包含收入和费用数据的电子表格。这是一个电子表格,其中包含数据编辑的内容:https://docs.google.com/spreadsheets/d/1xYXt_pvhpzCO7D61SoOwcmNNIGaFHkExwLHhl56c-NU/edit?usp=sharing

我想创建一个脚本,通过强制用户输入日期来验证C列中的数据。输入日期后,我想在其下方返回一个新行。代码的工作原理是验证数据并输入新行,但是当我在C96中输入日期时,新行将“日期”单元格(当前在C100中)推送到C101并说日期无效。我设置了我的代码,以便它应该重新运行脚本而不是验证C101数据。另外,我的'getRange'方法出错了。它说'TypeError:无法调用方法'getRange“未定义。” 目前,我有onEdit和onOpen的触发器,用于dateRalidation函数和onEdit触发器,用于addRow函数。

出于某种原因,在我的原始文件中,尽管存在这些错误,代码仍会运行。我已在下面的代码中加注了错误的位置。我对Google Scripts /编程很新,所以如果有人能帮助我,我将不胜感激。

这是我目前的代码:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("General Ledger");

  // Get row of first revenue
  var firstRowRevLookup = 'Revenue';
  var firstRowRange = sheet.getRange('B1:B');
  var firstRowValues = firstRowRange.getValues();
  var x = [];
  for (var i = 0; i<firstRowValues.length; i++) {
    if(firstRowValues[i] == firstRowRevLookup) {
     x.push(i+3); 
    }
  }
  var firstRowRevenue = Number(x);
  Logger.log(firstRowRevenue);

  //Get row of last revenue
  var lastRowRevLookup = 'End Revenues';
  var lastRowRange = sheet.getRange('C1:C');
  var lastRowValues = lastRowRange.getValues();
  var y = [];
  for (var i = 0; i < lastRowValues.length; i++) {
    if(lastRowValues[i] == lastRowRevLookup) {
     y.push(i);
    }
  }
  var lastRowRevenue = Number(y);
  Logger.log(lastRowRevenue)

  //Get row of first expense
  var firstRowExLookup = 'Expenses';
  var z = [];
  for(var i = 0; i<firstRowValues.length; i++) {
    if(firstRowValues[i] == firstRowExLookup){
     z.push(i+3); 
    }
  }
  var firstRowExpense = Number(z);
  Logger.log(firstRowExpense);

  //Get row of last expense
  var lastRowExLookup = 'End Expenses';
  var q = [];
  for(var i = 0; i <lastRowValues.length; i++) {
    if( lastRowValues[i] == lastRowExLookup) {
     q.push(i); 
    }
  }
  var lastRowExpense = Number(q);
  Logger.log(lastRowExpense);

dateValidationExpenses(sheet, firstRowExpense, lastRowExpense);
dateValidationRevenue(sheet, firstRowRevenue, lastRowRevenue);

//User must enter date into column C
function dateValidationExpenses(sheet, firstRowExpense, lastRowExpense) {
 // Logger.log(sheet);
  **var dateRange = sheet.getRange(firstRowExpense, 3, lastRowExpense-firstRowExpense+1);**
  //Logger.log(dateRange);
  var rule = SpreadsheetApp.newDataValidation()
       .requireDate()
       .setAllowInvalid(false)
      .setHelpText('Please enter date using format "=date(year,month,day)"')
      .build();
  dateRange.setDataValidation(rule);
  dateRange.setFontWeight('normal');
}

//User must enter date into column C
function dateValidationRevenue(sheet,firstRowRevenue,lastRowRevenue) {
  **var dateRange = sheet.getRange(firstRowRevenue, 3, lastRowRevenue-firstRowRevenue+1);**
   var rule = SpreadsheetApp.newDataValidation()
       .requireDate()
       .setAllowInvalid(false)
       .setHelpText('Please enter a date using format   "=date(year,month,day)"')
       .build();
  dateRange.setDataValidation(rule);
  dateRange.setFontWeight('normal');
}

function addRowExpenses () {
 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('General Ledger');
  var testCell = sheet.getRange(lastRowExpense,3).getValue();
  //Logger.log(testCell);
  if (testCell !== '') {
  sheet.insertRows(lastRowExpense+1);
  }

}

function addRowRevenue () {
 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('General Ledger');
  var testCell = sheet.getRange(lastRowRevenue,3).getValue();
  //Logger.log(testCell);
  if (testCell !== '') {
  sheet.insertRows(lastRowRevenue+1);
  }
}

function editTrigger1() {
  var sheet = SpreadsheetApp.getActive()
  ScriptApp.newTrigger('addRowExpenses')
      .forSpreadsheet(sheet)
      .onEdit()
      .create();
}

function editTrigger2() {
  var sheet = SpreadsheetApp.getActive()
  ScriptApp.newTrigger('addRowRevenue')
      .forSpreadsheet(sheet)
      .onEdit()
      .create();
}

1 个答案:

答案 0 :(得分:0)

变量sheet在函数之外声明,这使它成为“全局”。您无需通过sheet

目前:

dateValidationExpenses(sheet, firstRowExpense, lastRowExpense);
dateValidationRevenue(sheet, firstRowRevenue, lastRowRevenue);

更改为:

dateValidationExpenses(firstRowExpense, lastRowExpense);
dateValidationRevenue(firstRowRevenue, lastRowRevenue);

sheet可用于dateValidationExpenses函数,而无需传递它。这应该解决你在这一行上遇到的错误的问题:

var dateRange = sheet.getRange(firstRowExpense, 3, lastRowExpense-firstRowExpense+1);