Javascript 如何使GoogleSheets函数引用从中调用它的单元格 背景

Javascript 如何使GoogleSheets函数引用从中调用它的单元格 背景,javascript,google-apps-script,google-sheets,google-spreadsheet-api,Javascript,Google Apps Script,Google Sheets,Google Spreadsheet Api,我遵循的是一个基本的按钮调用,一个像这样的GoogleSheets脚本 function jumpToDetail() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("details"); ss.setActiveSheet(sheet).setActiveSelection("A2"); } 这会让你跳转到一个特定的单元格。我查看了google表单,但不知道如何使这个

我遵循的是一个基本的按钮调用,一个像这样的GoogleSheets脚本

function jumpToDetail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("details");
  ss.setActiveSheet(sheet).setActiveSelection("A2");
}
这会让你跳转到一个特定的单元格。我查看了google表单,但不知道如何使这个函数引用调用它的单元格

问题 如何在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”。(假定标签位于图纸的第一行。)


伟大的假设我有一个叫做“id”的专栏。。现在我已经选定了行。。如何获取所选行对应的id列的值?我在答案中添加了这样一个脚本。完美。。我已经给了你正确的奖励,你绝对超出了我的期望。。我现在正在想办法解决这个问题。。现在如何查询另一个spreadhseet的id列中的特定值。。然后让选择转到该特定行?在注释中,我只能概述:我将使用
.getRange(1,n,sheet.getLastRow(),1).getValues()
获取该列的值(这里n是它的索引),然后通过
.map(函数(a){returna[0]})将其从双数组展平到单数组
,然后用
indexOf
搜索,最后是
setActiveRange
。这里有一个一般性的评论:如何调试谷歌电子表格?我知道我可以在窗口已经打开时调试它。。但我想用一个真正的电子表格来调试它?这会让我的生活轻松多了
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');
  }
}