简体   繁体   English

如果没有可用的“返回”,Google Apps Script.getMonth 将不起作用

[英]Google Apps Script .getMonth isn't working if there is no available “return”

I'm trying to use "get.Month" but when I run the code there's an error:我正在尝试使用“get.Month”,但是当我运行代码时出现错误:

"TypeError: exSheet.getRange (...). GetValue (...). GetMonth is not a function" “TypeError:exSheet.getRange (...)。GetValue (...)。GetMonth 不是函数”

When I put the "return tempSum;"当我把“return tempSum;” outside the "if" No error and code works...在“如果”之外没有错误并且代码有效......

I tried putting ".setValue" instead of "return" and it didn't help.我尝试使用“.setValue”而不是“return”,但没有帮助。

Does anyone know how to fix it?有谁知道如何修理它?

function CALC_EX(chosenMonth){

  switch(chosenMonth){
  case "jan":
    chosenMonth = 1;
    break;
  case "feb":
    chosenMonth = 2;
    break;
  case "mar":
    chosenMonth = 3;
    break;
  case "apr":
    chosenMonth = 4;
    break;
  case "may":
    chosenMonth = 5;
    break;
  case "jun":
    chosenMonth = 6;
    break;
  case "jul":
    chosenMonth = 7;
    break;
  case "aug":
    chosenMonth = 8;
    break;
  case "sep":
    chosenMonth = 9;
    break;
  case "oct":
    chosenMonth = 10;
    break;
  case "nov":
    chosenMonth = 11;
    break;
  case "dec":
    chosenMonth = 12;
  }

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var exSheet = ss.getSheetByName('Expenses');
  var tempSum = 0;

  for(var i=2;i>0;i++){
    var range = exSheet.getRange(i, 5);
    var monthCell = exSheet.getRange(i, 5).getValue().getMonth()+1;

    if(!range.getValue()){
      i = 0;
      return tempSum;
    }
    if(monthCell === chosenMonth){
      tempSum = tempSum + exSheet.getRange(i, 12).getValue();
    }
  }
}

getMonth() is a Javascript function that can only be applied to a date object getMonth()是一个 Javascript function 只能应用于日期 object

TypeError: SpreadsheetApp.getActiveSheet(...).getRange(...).getValue(...).getMonth is not a function类型错误:SpreadsheetApp.getActiveSheet(...).getRange(...).getValue(...).getMonth 不是 function

means that your cell value is not recognized correctly as a date object, you can verify it with Logger.log(typeof exSheet.getRange(i, 5).getValue());表示您的单元格值未被正确识别为日期 object,您可以使用Logger.log(typeof exSheet.getRange(i, 5).getValue());

Verify that the cells you are iterating through are dates indeed (not strings or empty cells) and if necessary, set the data type to a date, eg by going from the Google Sheets UI to Format->Number->Date .验证您正在迭代的单元格确实是日期(不是字符串或空单元格),如有必要,将数据类型设置为日期,例如从 Google 表格 UI 转到Format->Number->Date Depending on your Locale settings, you might have to adjust your date format.根据您的区域设置,您可能需要调整日期格式。

Without seeing your spreadsheet, most likely the reason for the error comes from your loop definition, as pointed out by theMaster.如 theMaster 所指出的,在没有看到您的电子表格的情况下,错误的原因很可能来自您的循环定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM