简体   繁体   English

如何在自定义函数中使用Google Apps脚本编辑自定义函数的调用单元格?

[英]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.

相关问题 Google Apps Script - 从自定义菜单调用自定义函数 - 非嵌套 - Google Apps Script - Calling custom function from a custom menu - Not nested 通过Google Apps脚本自定义函数读取和写入单元格公式 - Read and write cell formulas from Google Apps Script custom function 如何在 Google Apps 脚本 function 中获取当前单元格? - How do I get the current cell in a Google Apps Script function? 如何将未指定数量的变量传递给 Google Apps 脚本自定义 Function - How do I pass unspecified number of variables to Google Apps Script Custom Function 如何使 Google Apps Script 中的自定义函数对 Google Sheets 中单元格背景的变化做出反应? - How to make a custom function made in Google Apps Script reactive to changes in the background of a cell in Google Sheets? 如何在编辑时触发自定义应用程序脚本 Function? - How to make Custom Apps Script Function trigger on edit? 编辑名为 function 的单元格上方的单元格? - 谷歌表格应用脚本 - Edit cell above cell that called a function? - Google Sheets apps script 如何将单元格引用传递给Apps脚本自定义函数? - How to pass a cell reference to an Apps Script custom function? 调试 Google 应用程序脚本时,如何在自定义对话框的右键菜单中启用 Inspect function? - How to enable Inspect function within right-click menu on custom dialog when debugging Google apps script? 从自定义函数谷歌应用程序脚本的范围中读取特定单元格 - read specific cells from a range for a custom function google apps script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM