2013-03-03 60 views
1

是否可以獲得圍繞特定單元格的範圍,類似於電子表格中的Ctrl + A?在電子表格的應用程序腳本中選擇連續範圍

+0

你的意思是讓整張紙在一個範圍內? – 2013-03-03 18:24:02

+0

@Sergeinsas不,這是一個連續的地區。也許在一個電子表格中有兩個單獨的區域,我想選擇其中的一個。 – 2013-03-04 03:49:24

+1

你必須在你的問題上更清楚一些。你在談論命名範圍嗎?正如Serge提到的,電子表格中的Ctrl-A選擇整個工作表 – Srik 2013-03-04 05:08:38

回答

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)) 
} 

作爲測試,請考慮這個電子表格。它有兩個連續的範圍,都有不規則的邊緣。 Spreadsheet screenshot

下面是我們的測試功能:

function testRanges() { 
    var range1 = getContiguousRange("C3").getA1Notation(); 
    var range2 = getContiguousRange("B8").getA1Notation(); 
    debugger; // Pause if running in debugger 
} 

而這就是我們得到:

Debugger Results

我希望幫助!

+0

非常聰明!感謝分享+1。 – 2013-03-05 12:51:57

2

我嘗試了以上方法,但由於缺乏聲譽,我無法在包含多張工作表的工作表中發表評論,並發現如果更改該行,效果會更好: var contiguousRange = SpreadsheetApp.getActiveSheet() .getRange(cellA1); 與 var contiguousRange = sheet.getRange(cellA1);

相關問題