繁体   English   中英

Google 表格自定义函数错误“结果不是数字”

[英]Google sheets custom function error “Result was not a number”

我有一个产生错误的 Google Sheets Apps Script 自定义函数: "Result was not a number" 尽管无论应用于单元格的“格式”如何,都不会将数字视为数字,但从自定义函数返回的值。 计算出的值然后不能用于其他内置函数,例如 SUM()。

已经问过类似的问题,请参阅此处此处此处(可能还有其他人...),但不解释如何更改返回类型。

DataTypes 的 google apps-script 文档不清楚如何将您的自定义函数“强制”为特定的返回类型。

链接到带有脚本的示例

这是我遇到问题的函数(注意下面不包括 getCell()):

/**
 Sum all transactions across sheets that match category and date range.

 @param category_name The category to filter on
 @param date_min Minimum date in range to filter
 @param date_max Maximum Date in range to filter
 @param [time] Optional update time/date
 @return The sum of transactions that match input criteria
 @customfunction
 */
function sumCategory(category_name, date_min, date_max, time) {      
  var date_min = new Date(date_min);
  var date_max = new Date(date_max);
  var date_month = date_min.getMonth();
  var date_year = date_min.getYear();

  var sheets = ['Account1', 'Account2'];

  var column_name = { 
    Date:   "Date",
    Category: "Category",
    Amount: "Amount"
  };

  var msg = "";
  var sum = 0.0;
  var spreadsheet = SpreadsheetApp.getActive();

  // Number of seconds before the cache expires.
  var cache_time = 120;

  var cache = CacheService.getScriptCache();

  // Build the cache key with requested category_year_month
  var cache_key = category_name + '_' + date_year.toString() + '_' + date_month.toString();

  // If we have this category cached, then return it...
  var cached = cache.get(cache_key);
  if (cached != null) {
    return cached;
  }

  // Category sum for cache
  var cat_sum = { }; 

  // Loop through all sheets, and all transactions building the category sum cache
  for(var x = 0; x < sheets.length; x++){  
    var s = sheets[x];
    //Browser.msgBox("Working on sheet: " + s);
    var cur_sheet = spreadsheet.getSheetByName(s);

    // Get data range for whole sheet 
    var data =  cur_sheet.getDataRange();
    var num_rows = data.getNumRows();
    var num_cols = data.getNumColumns();

    // Loop through all rows in this sheet
    for(var i = 2; i < num_rows; i++){
      var xdate = new Date(getCell(column_name.Date, i, data));
      var cat = getCell(column_name.Category, i, data);
      var amount = getCell(column_name.Amount, i, data);

      // If the transaction category is empty, set to 'Uncategorized'
      if(!cat){
        cat = "Uncategorized";
      }
      var xkey = cat + '_' + xdate.getYear().toString() + '_' + xdate.getMonth().toString();

      // Check if we have not already stored this category
      if(!cat_sum.hasOwnProperty(xkey)){
        // initialize
        cat_sum[xkey] = 0.0;
      }
      cat_sum[xkey] = cat_sum[xkey] + parseFloat(amount);

    }// end loop rows
  }// end loop sheets

  // Build cache
  for (var key in cat_sum){
    //console.log( key, dict[key] );
    cache.put(key, cat_sum[key], cache_time);
  }

  // If category not found... set to 0.
  if(!cat_sum[cache_key]){
    cat_sum[cache_key] = 0.0;
    cache.put(cache_key, 0.0, cache_time);
  }

  var return_value = cat_sum[cache_key];

  // Return the requested category sum
  return return_value;
}

Javascript 和 Google 应用程序脚本不提供将自定义函数的返回类型“强制”为数字的方法。

这个答案很接近,但返回类型是一个数组,而不是一个数字。

这个问题的答案是正确的,但没有关于如何强制返回值的正确类型的示例。

链接包含将 Javascript 字符串转换为数字的示例,并列出了一些陷阱。

为了“强制”返回一个数字,添加return Number(cached); return Number(return_value); 到函数。

带有脚本的示例已更新以返回一个数字。

是带有脚本代码的 JSFiddle。

暂无
暂无

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

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