是否可以獲得圍繞特定單元格的範圍,類似於電子表格中的Ctrl + A?在電子表格的應用程序腳本中選擇連續範圍
1
A
回答
6
我有一些電子表格,其中的表格由QUERY()
函數創建,所以邊界很靈活。在過去,我採取了設置命名範圍的方法,這些命名範圍的大小我預計是QUERY結果需要的最大值,並將這些命名範圍用於其他操作。
不再!
這裏的一個效用函數,getContiguousRange()
接受在A1表示法的細胞的位置,並返回包含它的連續單元的Range
。此代碼可用in a gist。
/**
* Return the contiguous Range that contains the given cell.
*
* @param {String} cellA1 Location of a cell, in A1 notation.
* @param {Sheet} sheet (Optional) sheet to examine. Defaults
* to "active" sheet.
*
* @return {Range} A Spreadsheet service Range object.
*/
function getContiguousRange(cellA1,sheet) {
// Check for parameters, handle defaults, throw error if required is missing
if (arguments.length < 2)
sheet = SpreadsheetApp.getActiveSheet();
if (arguments.length < 1)
throw new Error("getContiguousRange(): missing required parameter.");
// A "contiguous" range is a rectangular group of cells whose "edge" contains
// cells with information, with all "past-edge" cells empty.
// The range will be no larger than that given by "getDataRange()", so we can
// use that range to limit our edge search.
var fullRange = sheet.getDataRange();
var data = fullRange.getValues();
// The data array is 0-based, but spreadsheet rows & columns are 1-based.
// We will make logic decisions based on rows & columns, and convert to
// 0-based values to reference the data.
var topLimit = fullRange.getRowIndex(); // always 1
var leftLimit = fullRange.getColumnIndex(); // always 1
var rightLimit = fullRange.getLastColumn();
var bottomLimit = fullRange.getLastRow();
// is there data in the target cell? If no, we're done.
var contiguousRange = SpreadsheetApp.getActiveSheet().getRange(cellA1);
var cellValue = contiguousRange.getValue();
if (cellValue = "") return contiguousRange;
// Define the limits of our starting dance floor
var minRow = contiguousRange.getRow();
var maxRow = minRow;
var minCol = contiguousRange.getColumn();
var maxCol = minCol;
var chkCol, chkRow; // For checking if the edge is clear
// Now, expand our range in one direction at a time until we either reach
// the Limits, or our next expansion would have no filled cells. Repeat
// until no direction need expand.
var expanding;
do {
expanding = false;
// Move it to the left
if (minCol > leftLimit) {
chkCol = minCol - 1;
for (var row = minRow; row <= maxRow; row++) {
if (data[row-1][chkCol-1] != "") {
expanding = true;
minCol = chkCol; // expand left 1 column
break;
}
}
}
// Move it on up
if (minRow > topLimit) {
chkRow = minRow - 1;
for (var col = minCol; col <= maxCol; col++) {
if (data[chkRow-1][col-1] != "") {
expanding = true;
minRow = chkRow; // expand up 1 row
break;
}
}
}
// Move it to the right
if (maxCol < rightLimit) {
chkCol = maxCol + 1;
for (var row = minRow; row <= maxRow; row++) {
if (data[row-1][chkCol-1] != "") {
expanding = true;
maxCol = chkCol; // expand right 1 column
break;
}
}
}
// Then get on down
if (maxRow < bottomLimit) {
chkRow = maxRow + 1;
for (var col = minCol; col <= maxCol; col++) {
if (data[chkRow-1][col-1] != "") {
expanding = true;
maxRow = chkRow; // expand down 1 row
break;
}
}
}
} while (expanding); // Lather, rinse, repeat
// We've found the extent of our contiguous range - return a Range object.
return sheet.getRange(minRow, minCol, (maxRow - minRow + 1), (maxCol - minCol + 1))
}
作爲測試,請考慮這個電子表格。它有兩個連續的範圍,都有不規則的邊緣。
下面是我們的測試功能:
function testRanges() {
var range1 = getContiguousRange("C3").getA1Notation();
var range2 = getContiguousRange("B8").getA1Notation();
debugger; // Pause if running in debugger
}
而這就是我們得到:
我希望幫助!
+0
非常聰明!感謝分享+1。 – 2013-03-05 12:51:57
2
我嘗試了以上方法,但由於缺乏聲譽,我無法在包含多張工作表的工作表中發表評論,並發現如果更改該行,效果會更好: var contiguousRange = SpreadsheetApp.getActiveSheet() .getRange(cellA1); 與 var contiguousRange = sheet.getRange(cellA1);
相關問題
- 1. Excel vba:從非連續範圍中選擇隨機單元格
- 2. 如何讓應用程序腳本的ID電子表格
- 3. 如何在Google表格中引用非連續範圍的列?
- 4. Google表格腳本 - 選擇下一行+合併範圍?
- 5. 如何在Google電子表格中分享我的應用程序腳本?
- 6. vb web應用程序中的電子表格連接
- 7. 使用腳本Google電子表格數據透視表範圍更新
- 8. 在Google應用程序腳本中讀取多個電子表格
- 9. 在Google Apps腳本Web應用程序中嵌入Google電子表格
- 10. 用VBA在Word表格中選擇不連續的單元格
- 11. 在應用程序腳本中將Google電子表格中的表格單元格拆分(拆分)
- 12. 電子表格和應用程序腳本中的圖像問題
- 13. 谷歌電子表格範圍名稱
- 14. 有沒有辦法在谷歌應用腳本中獲取不同電子表格的數據範圍?
- 15. Google電子表格腳本
- 16. Excel VBA - 在非連續範圍內選擇最遠/最外側的單元格
- 17. 在Google電子表格中獲取子範圍
- 18. 使用應用程序腳本的電子表格重命名事件
- 19. 非連續範圍
- 20. 如何在電子表格中使用鼠標獲取範圍
- 21. 將谷歌應用程序腳本函數應用於單元格範圍
- 22. 僅在滿足條件時才更改範圍中的單元格值(Google表格應用程序腳本)
- 23. 從非連續範圍中選擇隨機數
- 24. 谷歌腳本的電子表格 - 錯誤:參數必須是範圍
- 25. 授權範圍2在谷歌應用程序腳本
- 26. 排序範圍值而不對電子表格進行排序
- 27. 使用GitLab連續部署電子應用程序
- 28. 子腳本超出範圍
- 29. 應用程序腳本將數據行復制到不同的電子表格
- 30. Google表格腳本:在電子表格中按字母順序排序表
你的意思是讓整張紙在一個範圍內? – 2013-03-03 18:24:02
@Sergeinsas不,這是一個連續的地區。也許在一個電子表格中有兩個單獨的區域,我想選擇其中的一個。 – 2013-03-04 03:49:24
你必須在你的問題上更清楚一些。你在談論命名範圍嗎?正如Serge提到的,電子表格中的Ctrl-A選擇整個工作表 – Srik 2013-03-04 05:08:38