[英]How do I edit the calling cell of a custom function with google apps script from within the custom function?
After searching through various questions and the documentation I still cannot edit the calling cell of a custom function. 搜索了各种问题和文档后,我仍然无法编辑自定义函数的调用单元格。 According to the docs :
根据文档 :
Returns the range of cells that is currently considered active.
返回当前被认为是活动的单元格范围。 This generally means the range that a user has selected in the active sheet, but in a custom function it refers to the cell being actively recalculated.
这通常意味着用户在活动工作表中选择的范围,但是在自定义功能中,它是指正在主动重新计算的单元格。
Although this description is for the getActiveRange() function one assumes the getActiveCell() functions in the same way. 尽管此描述是针对getActiveRange()函数的,但仍以相同的方式假定了getActiveCell()函数。
I am trying to call a helper function from within my custom function which should return the 'active cell' which, to my knowledge, should be the calling cell of the custom function. 我正在尝试从我的自定义函数中调用一个辅助函数,该函数应返回“活动单元格”,据我所知,该“活动单元格”应为自定义函数的调用单元格。 Not working code:
不起作用的代码:
// returns the current active (calling) cell for use within custom functions
function callingCell() {
return SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getActiveRange()
}
// change background colour of calling cell
function getInfoFromPublicApi(type) {
... // get prices using UrlFetchApp
callingCell().setBackground('green')
return averagePrice(prices, type)
}
I have also tried using the SpreadsheetApp from within the custom function directly, as well as using: 我也尝试过直接在自定义函数中使用SpreadsheetApp,以及使用以下方法:
SpreadsheetApp.getActiveSpreadsheet().getActiveCell()
SpreadsheetApp.getActiveSpeadsheet().getActiveRange()
You can go about it two ways. 您可以通过两种方式进行处理。 First, set calling cell as a variable.
首先,将调用单元格设置为变量。 You cannot chain methods together like you're trying to do in the larger function.
您无法像在大型函数中尝试的那样将方法链接在一起。 Or, you can drop the
callingCell()
function because the information is handled in the event
object of onEdit()
. 或者,因为信息在
onEdit()
的event
对象中处理,所以可以删除callingCell()
函数。
Method 1 方法1
// returns the current active (calling) cell for use within custom functions
function callingCell() {
return SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getActiveRange();
}
// change background colour of calling cell
function getInfoFromPublicApi(type) {
var active = callingCell();
... // get prices using UrlFetchApp
active.setBackground('green')
return averagePrice(prices, type)
}
Method 2 方法二
function onEdit(e) {
// Not sure what your `type` param is, so you'd need to test for that somehow.
... // get prices using UrlFetchApp
e.getRange().setBackground('green');
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.