通过 Javascript 方法从 Google 电子表格中获取光标行/列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10966631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the cursor row/column from a Google spreadsheet through a Javascript method
提问by HymanTheLabRat
On 29 Mar 2010, Jason Hale wanted to use the spreadsheet cursor location to select an e-mail address for a Javascript app he was writing - See http://productforums.google.com/forum/#!topic/apps-script/U10q44vptPU. Respondents suggested the getActiveSelection and concomitant getRowIndex/getColumnIndex methods. Unfortunately, they didn't work for Hale and they didn't work for me two years later when I wanted to write a similar application. The cursor's location has no effect on getRowIndex and getColumnIndex. These methods always return 1, which is useless and probably a bug. Google Apps lacks a cursor service such as the one Python offers. Has anyone found anything that Google Apps returns that might be useful in this context?
2010 年 3 月 29 日,Jason Hale 想使用电子表格光标位置为他正在编写的 Javascript 应用程序选择电子邮件地址 - 请参阅http://productforums.google.com/forum/#!topic/apps-script/ U10q44vptPU。受访者建议使用 getActiveSelection 和伴随的 getRowIndex/getColumnIndex 方法。不幸的是,它们对 Hale 不起作用,两年后当我想编写一个类似的应用程序时,它们对我也不起作用。光标的位置对 getRowIndex 和 getColumnIndex 没有影响。这些方法总是返回 1,这是无用的并且可能是一个错误。Google Apps 缺少 Python 提供的游标服务。有没有人发现 Google Apps 返回的任何内容可能在这种情况下有用?
回答by Serge insas
Have you tried a simple function like this one ?
您是否尝试过像这样的简单功能?
function getRCposition(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getActiveCell();
var R = cell.getRow();
var C = cell.getColumn();
Browser.msgBox('Row = '+R+' and Col = '+C);
}
or a more 'universal' function like this :
或者像这样的更“通用”的功能:
function getposition(){
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
var C = range.getColumnIndex();
var W = range.getWidth();
var H = range.getHeight();
var A1not = range.getA1Notation();
Browser.msgBox('Row = '+R+' / Col = '+C+' Width = '+W+' / Heigth = '+H+' A1 notation = '+A1not);
}
回答by Peter Herrmann
The following code works for me. It prompts me with the row number of my current position in the spreaadsheet:
以下代码对我有用。它提示我在电子表格中当前位置的行号:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Test1", functionName: "menuItem1"}];
ss.addMenu("Tests", menuEntries);
}
function menuItem1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
Browser.msgBox("You have selected row " + ss.getActiveCell().getRow());
}