---
description: 簡化選取 Range
---

# RangeRect

[GAS](..) ⟩ [custom objects]() ⟩ RangeRect

{% tabs %}
{% tab title="🔴 主題" %}
* instance methods
  * inset()
  * toArray()
  * nthRow(i)
  * nthColumn(j)
  * cell(i, j)
* static methods
  * [rangerect.bydata](rangerect/rangerect.bydata "mention")
{% endtab %}

{% tab title="💾 程式" %}
```javascript
/**
 * @example:
 * const rect = new RangeRect(1, 1, 2, 3);
 * sheet.rangeByRect(rect1).setBorder(true, ...);
 * @required by:
 * app.sheet.prototype.rangeByRect()
 */
class RangeRect {

  // init
  constructor(row, col, rows, cols){
    this.row = row;
    this.col = col;
    this.rows = rows;
    this.cols = cols;
  }

  // 🔸 rect.lastRowIndex
  get lastRowIndex(){
    return this.row + this.rows - 1;
  }

  // 🔸 rect.lastColumnIndex
  get lastColumnIndex(){
    return this.col + this.cols - 1;
  }

  // 🔸 rect.toArray()
  toArray(){
    return [this.row, this.col, this.rows, this.cols];
  }

  // 🔸 rect.inset()
  inset({top=0, bottom=0, left=0, right=0}={}){

    let row = this.row + top;
    let col = this.col + left;
    let rows = this.rows - top - bottom;
    let cols = this.cols - left - right;

    if (row < 0) throw new Error(`⛔ RangeRect.inset: new rect can't start from row = ${row}`);
    if (col < 0) throw new Error(`⛔ RangeRect.inset: new rect can't start from col = ${col}`);
    if (rows < 1) throw new Error(`⛔ RangeRect.inset: new rect can't have ${rows} rows`);
    if (cols < 1) throw new Error(`⛔ RangeRect.inset: new rect can't have ${cols} cols`);

    return new RangeRect(row, col, rows, cols);
  }

  // 🔸 rect.nthRow(i)
  nthRow(i){
    let row = this.row + i - 1;
    if (row < 0) throw new Error(`⛔ RangeRect.nthRow(): new rect can't start from row = ${row}`);
    return new RangeRect(row, this.col, 1, this.cols);
  }
  
  // 🔸 rect.nthColumn(j)
  nthColumn(j){
    let col = this.col + j - 1;
    if (col < 0) throw new Error(`⛔ RangeRect.nthColumn(): new rect can't start from col = ${col}`);
    return new RangeRect(this.row, col, this.rows, 1);
  }

  // 🔸 rect.cell(i, j)
  // similar to range.getCell(row, col)
  cell(i, j){
    let row = this.row + i - 1;
    let col = this.col + j - 1;
    if (row < 0) throw new Error(`⛔ RangeRect.cell(): new rect can't start from row = ${row}`);
    if (col < 0) throw new Error(`⛔ RangeRect.cell(): new rect can't start from col = ${col}`);
    return new RangeRect(row, col, 1, 1);
  }
  
}

// ┌─────────────────┐
// │ static methods  │
// └─────────────────┘

// 🔸 RangeRect.byData()
RangeRect.byData = function(data, {row=1, col=1}={}){
  return new RangeRect(row, col, data.length, data[0].length);
};
```
{% endtab %}

{% tab title="📘 手冊" %}
* Swift ⟩ [CGRect](https://developer.apple.com/documentation/corefoundation/cgrect)
* [range](../classes/range "mention") ⟩
  * [getCell(row, column)](https://developers.google.com/apps-script/reference/spreadsheet/range#getcellrow,-column) - cell (Range) relative to range.
  * [getRow()](https://developers.google.com/apps-script/reference/spreadsheet/range#getrow) - <mark style="color:yellow;">**starting row index**</mark> of range.
  * [getColumn()](https://developers.google.com/apps-script/reference/spreadsheet/range#getcolumn) - <mark style="color:yellow;">**starting column index**</mark> of range.
{% endtab %}

{% tab title="⬇️ 應用" %}
* [class-average](../projects/class-average "mention")
* &#x20;[app.sheet.prototype](../app/prototypes/app.sheet.prototype "mention") (used by [sheet.rangebyrect](../app/prototypes/app.sheet.prototype/sheet.rangebyrect "mention"))
{% endtab %}

{% tab title="👥 相關" %}
* [range](../classes/range "mention")
{% endtab %}
{% endtabs %}
