问题描述
我想知道是否可以使用Google脚本在电子表格中选择一行,例如输入图片这里的说明
I want to know if we can select a row in a Spreadsheet using Google script like this enter image description here
无论如何,首先我使用这种功能尝试使用列,但是我收到数据错误(未找到):如果您有主意:)
Anyway first I've tried with the columns by using this fonction but I get an error of the data (is not found): If you have an idea :)
function testGetFullColumn()
{
getFullColumn('A', 1) ;
}
// This function gets the full column Range like doing 'A1:A9999' in excel
// @param {String} column The column name to get ('A', 'G', etc)
// @param {Number} startIndex The row number to start from (1, 5, 15)
// @return {Range} The "Range" object containing the full column: https://developers.google.com/apps-script/class_range
function getFullColumn(column, startIndex){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var lastRow = sheet.getLastRow();
return sheet.getRange(column+startIndex+':'+column+lastRow);
}
我对getRange有所了解,但我会很酷^^^^^^能像我所附的照片一样直观地看到选择内容,因为我尝试过
Edit :I know about getRange but I would be cool^^^^^^ to see in visually the selection like in the photo I've attached because I tried with
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; return sheet.getDataRange();
并且它不会在视觉上选择所有工作表,我的意思是我想看到一个交互式选择:)像这里
and it doesn't select all the sheet visually I mean I want to see an interactive selection :) like here
正如您所建议的那样,我尝试在getRange之后使用setActiveRange,谢谢Guilherme M,但是它不能直观地选择所有文档:(您的代码确实很酷^ 我担心无法选择,我会尝试做同样的感谢^^
Edit 2: I've tried to use setActiveRange after getRange as you suggested thanks Guilherme M but it doesn't select visually all the document :( Your code works it's really cool^ I was afraid that it's impossible to select I will try to do the same thanks ^^
function testGet()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getDataRange();
SpreadsheetApp.setActiveRange(range)
}
要选择所有我尝试过此代码的文档:
Edit 3: To select all the document I've tried this code:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var range = sheet.getRange(1,1,lastRow+1,lastColumn+1);
sheet.setActiveRange(range);
var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: tout le document
var activeRange = selection.getActiveRange();
推荐答案
是的,您可以使用Apps脚本在电子表格中选择一行或任何范围.您可以使用方法 setActiveRange()
进行操作.下面是一个示例:
Yes, you can select a row or any range in the spreadsheet using Apps Script. You do so with method setActiveRange()
. An example follows below:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange('A1:D4');
sheet.setActiveRange(range);
var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: A1:D4
var activeRange = selection.getActiveRange();
通知方法 选择范围其中存在数据.",因此不会选择空白单元格或整个文档.对于此类任务,您需要方法 getRange(row, column, numRows, numColumns)
与 getMaxRows()
组合和 getMaxColumns()
.
Notice The method getDataRange()
selects range "in which data is present.", so it won't select blank cells or the whole document. For such task, you need method getRange(row, column, numRows, numColumns)
combined with getMaxRows()
and getMaxColumns()
.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
sheet.setActiveRange(range);
var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: A1:D4
var activeRange = selection.getActiveRange();
这篇关于Google脚本在Google Spreadsheet中选择整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!