将单元格引用传递给电子表格函数 [英] passing cell references to spreadsheet functions

查看:97
本文介绍了将单元格引用传递给电子表格函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用一个电子表格函数时,比如 int(f2),函数就会根据单元格中的值进行操作。如果 cell(F2)包含3.14159,结果将是3.
但是当我调用不同类型的函数时 - 例如: row(f8) - 函数获取单元格引用,而不是该值,在这种情况下,返回8。



我得到我的自定义函数来处理引用,而不是值?

我可以传递一个字符串,并使用 getRange() code>,但是,如果我移动或更新工作表上的单元格,这些字符串将不会更改。

非常简单的示例:

 函数GetFormula(cellname){
return SpreadsheetApp.getActiveSheet()。getRange(cellname).getFormula();





$ b

在我的工作表代码中,我可以像这样检索C4中的公式: = GetFormula(C4)



但是,这个参数是一个字符串,细胞参考。一个稍微复杂的问题需要调用单元格在复制和粘贴时更新。



有什么想法?

解决方案

几个月前,我正在研究这个问题,并提出了一个非常简单的问题:创建一个以每个单元格的名称作为其内容的新工作表:
单元格A1可能看起来像:
$ b $ $ p $ = arrayformula(cell(address,a1:z500))

编辑:上述不再有效。我的新公式'Ref'!A1'是

  = ArrayFormula(char(64 + column(A1:Z100))& (A1:Z100))

命名工作表Ref。
然后,当你需要一个字符串引用而不是内容时,你可以使用:

  = some_new_function ('Ref'!C45)

当然,您需要检查函数是否通过一个字符串(一个单元格)或一维或二维数组。如果你得到一个数组,它将把所有的单元格地址作为字符串,但是从第一个单元格,宽度和高度,你可以找出你需要的。


When I call a spreadsheet function, say int(f2), the function operates on the value in the cell. If cell("F2") contains 3.14159, the result would be 3. But when I call a different type of function — for example: row(f8) — the function takes the cell reference, and not the value, in this case, returning 8.

How do I get my custom function to work with the reference, rather than the value?

I can pass a string, and use getRange(), but, if I move or update the cells on the sheet, the strings won't change.

Really simple example:

function GetFormula(cellname) {
  return SpreadsheetApp.getActiveSheet().getRange(cellname).getFormula();
}

With this in my sheet's code, I can retrieve the formula in C4 like this: =GetFormula("C4")

But, this argument is a string, and I would rather pass a cell reference. A somewhat more complicated issue requires the calling cells to update when copied and pasted.

Any ideas?

解决方案

I was working on this a few months ago and came up with a very simple kludge: create a new sheet with the name of each cell as its contents: Cell A1 could look like:

= arrayformula(cell("address",a1:z500))

EDIT: The above no longer works. My new formula for 'Ref'!A1 is

= ArrayFormula(char(64+column(A1:Z100))&row(A1:Z100))

Name the sheet "Ref". Then when you need a reference to a cell as a string instead of the contents, you use:

= some_new_function('Ref'!C45)

Of course, you'll need to check if the function gets passed a string (one cell) or a 1D or 2D Array. If you get an array, it will have all the cell addresses as strings, but from the first cell and the width and height, you can figure out what you need.

这篇关于将单元格引用传递给电子表格函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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