How to make a google sheets function reference the cell it was called from(如何使Google Sheets函数引用从中调用它的单元格)
问题描述
背景
我遵循了this tutorial,它基本上使按钮调用Google Sheet脚本,如下所示
function jumpToDetail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("details");
ss.setActiveSheet(sheet).setActiveSelection("A2");
}
这会使您跳到特定的单元格。我查看了Google Sheetsdocs,但不知道如何使此函数引用从中调用它的单元格
问题
如何使Google Sheets中的函数引用从中调用该函数的函数。例如,我希望最终代码如下所示:
function jumpToDetail(clickedCell) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("details");
// get the value of "id" of the same row the cell was clicked in
ss.setActiveSheet(sheet).setActiveSelection(function_of(id));
}
推荐答案
活动范围通过SpreadsheetApp的getActiveRange方法获取。当从定制函数调用脚本时,这很有用:活动范围就是定制函数所在的单元格。示例:
function currentCell() {
return SpreadsheetApp.getActiveRange().getA1Notation();
}
在工作表中的任何位置输入=currentcell()将返回该单元格的A1表示法。
但是,单击按钮(插入的图形)对活动范围没有影响,因为图形不与工作表中的任何单元格关联。它们在自己的层中,漂浮在床单上。在使用按钮调用的脚本中,活动区域位于选定单元格所在的任何位置,与按钮的位置无关。
为了将某个位置传递给脚本,用户必须首先选择一个单元格,然后(通过按钮或菜单)调用脚本。
下面是一个函数,它返回当前行中带有"id"列的单元格的内容。(假定标签位于工作表的第一行。)
function getId() {
var row = SpreadsheetApp.getActiveRange().getRow();
var sheet = SpreadsheetApp.getActiveSheet();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var column = headers[0].indexOf('id') + 1;
if (column > 0) {
return sheet.getRange(row, column).getValue();
}
else {
throw new Error('No column labeled id');
}
}
这篇关于如何使Google Sheets函数引用从中调用它的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使Google Sheets函数引用从中调用它的单元格
基础教程推荐
- Bokeh Div文本对齐 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
