👔RangeRect
簡化選取 Range
GAS ⟩ custom objects ⟩ RangeRect
instance methods
inset()
toArray()
nthRow(i)
nthColumn(j)
cell(i, j)
static methods
/**
* @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);
};
Swift ⟩ CGRect
Range ⟩
getCell(row, column) - cell (Range) relative to range.
getRow() - starting row index of range.
getColumn() - starting column index of range.
app.sheet.prototype (used by 💾 sheet.rangeByRect())
Last updated