Google Scripts不是一个数字 [英] Google Scripts Returning Not a Number

查看:105
本文介绍了Google Scripts不是一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Google脚本,但似乎找不到解决我的问题的方法。我试图做的是将电子表格中两个单元格的内容与我的函数 calculateRates()相乘。这是我的代码:

  / ** 
@customFunction
* /
函数calculateRates (hourDecimal,personRate){
var ss = SpreadsheetApp.getActiveSpreadsheet(); //访问电子表格
var sheet = ss.getSheets()[0]; //访问表

var range = sheet.getDataRange(); //访问所有单元格

var values = range.getValues(); //调用获取某个单元格的值

var totalEarned = hourDecimal * personRate;
Logger.log(totalEarned);

返回totalEarned;
}

它的问题是在我指定两个单元格相乘之后电子表格上函数的参数,则会显示结果不是数字的错误。我知道我的电子表格中有正确的功能,所以不能成为问题.....任何想法?感谢您的帮助!

解决方案

当您尝试使用不是全部数字的值执行计算时,会发生此错误,例如Number以及一个字符串。

Google表格根据单元格的值存储数据,值可以是 Number 布尔值日期字符串将返回一个空的String)。当自定义函数使用这些值时,Apps脚本会将它们视为JavaScript中适当的数据类型。

您需要确保单元格的值是Numbers,考虑到如果单元格包含一些空格或其他非数字字符(如2 23/2它将被视为字符串,您需要在任何之前修剪或提取这些特殊字符计算。

I've been working on Google Scripts for a bit, but I can't seem to find the solution to my problem. What I am trying to do is multiply the contents of two cells on a spreadsheet together with my function calculateRates(). Here is my code:

/** 
@customFunction
*/
function calculateRates(hourDecimal, personRate) {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); //Access spreadsheet
  var sheet = ss.getSheets()[0];  //Access sheet

  var range = sheet.getDataRange(); //Access all cells

  var values = range.getValues(); //Call to get value of certain cell

  var totalEarned = hourDecimal * personRate;
  Logger.log(totalEarned);

  return totalEarned;
}

The problem with it is that after I designate two cells to multiply together as the parameters of the function on the spreadsheet, there is an error that reads "Result was not a number." I know I have the function correct on my spreadsheet, so that can't be the problem..... Any ideas? Thanks for your help!

解决方案

This error occurs when you try to perform calculations with values that are not all numbers, example a Number and a String.

Google Sheets stores data depending on the value of the cell, the values may be of type Number, Boolean, Date, or String (Empty cells will return an empty String). When these values are used by Custom Functions, Apps Script treats them as the appropriate data type in JavaScript.

You need to make sure the values of the cells are Numbers, take into account that if the cell contains some space or other non-numeric characters like "2 2" or "3/2" it will be treated as String and you will need to trim or extract those special characters before any calculation.

这篇关于Google Scripts不是一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆