javascript Google 电子表格脚本:创建简单函数时“无法在对象表中找到函数 getRange”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21995507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Spreadsheet Script: "Cannot find function getRange in object Sheet" when creating a simple function
提问by user3347814
Sorry, for the stupid question, but I′ve searched the whole internet and I could not find a good Tutorial to learn how to program in Google SpreadSheet Script.
抱歉,这个愚蠢的问题,但我已经搜索了整个互联网,但找不到一个好的教程来学习如何在 Google SpreadSheet Script 中进行编程。
I want to make a very simple function just for practice.
我想做一个非常简单的函数只是为了练习。
function simplesum(input) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets();
var range = sheet.getRange(input);
var x = 0;
for (var i = 1; i <= range.getNumRows(); i++) {
for (var j = 1; j <= range.getNumColumns(); j++) {
var cell = range.getCell(i, j);
x += (cell.getValue());
}
}
return x;
}
I know I could use =sum() to do exactly the same thing. The idea here is to learn how to program.
我知道我可以使用 =sum() 来做同样的事情。这里的想法是学习如何编程。
When I try to use my function in a cell: (i.e: =simplesum((A1:A8)) it gives an Error saying: "TypeError: Cannot find function getRange in object Sheet. (line 4)"
当我尝试在单元格中使用我的函数时:(即:=simplesum((A1:A8)) 它给出一个错误说:“类型错误:在对象表中找不到函数 getRange。(第 4 行)”
What should I do?
我该怎么办?
And again, sorry for the dumb question....
再次,对不起这个愚蠢的问题......
采纳答案by AdamL
In this case, you are implementing a Google Apps Script function as a custom function, invoked in a spreadsheet cell.
在本例中,您将 Google Apps 脚本函数实现为在电子表格单元格中调用的自定义函数。
When you pass a range to a custom function invoked in a spreadsheet cell, you are not passing a range object or a range reference, but rather a 2-D Javascript array of values. So your custom function should just process that array.
当您将范围传递给在电子表格单元格中调用的自定义函数时,您传递的不是范围对象或范围引用,而是一个二维 Javascript 值数组。所以您的自定义函数应该只处理该数组。
function simplesum(input)
{
var x = 0;
for (var i = 0; i < input.length; i++)
{
for (var j = 0; j < input[0].length; j++)
{
x += input[i][j];
}
}
return x;
}
回答by Emmanuel Delahaye
This is working :
这是工作:
function sum(input) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet(); // your mistake : getSheets()
var range = sheet.getRange(input);
var x = 0;
for (var i = 1; i <= range.getNumRows(); i++) {
for (var j = 1; j <= range.getNumColumns(); j++) {
var cell = range.getCell(i, j);
x += cell.getValue();
}
}
return x;
}
function main () // Yes, I am a former C-programmer ...
{
var s = sum ("A1:B3"); // Notice the quotes. A string must me entered here.
Logger.log('s = ' + s);
}
回答by Irvin Zhan
var sheet = ss.getSheets();
returns an Array of sheets, meaning sheets.getRange(input) will throw that error. Try this instead:
返回一个工作表数组,这意味着 sheet.getRange(input) 将抛出该错误。试试这个:
var sheet = ss.getSheets()[0];
which selects the first sheet of the array of sheets. Google has some decent documentation for this. For example, here's its documentation on getRange(). Note that it uses ss.getSheets()[0] as well. Hope this helped!
它选择工作表数组的第一张工作表。谷歌为此提供了一些不错的文档。例如,这是它关于 getRange() 的文档。请注意,它也使用 ss.getSheets()[0] 。希望这有帮助!