🔳app.range.prototype
⬆️ 需要: app
/**
* custom prototype for Range
*
* ### methods
*
* - row()
* - col()
* - rows()
* - cols()
* - info()
*
* - inset()
* - nthRow(i)
* - nthColumn(j)
* - firstNRows(n)
* - firstNColumns(n)
* - lastNthRow(i) <-------- #TODO
* - lastNthColumn(j)
* - lastNRows(n) <-------- #TODO
* - lastNColumns(n)
* - cell(i, j)
*
* @example
*
* range.__proto__ = app.range.prototype;
* range.alignCenter();
*/
app.range.prototype = {
// ┌────────────┐
// │ info │
// └────────────┘
// GAS doesn't work well with getters❓
// -------------------------------------
// get row(){ return this.getRow() }, // ⛔ TypeError: this.getRow is not a function
// get col(){ return this.getColumn() },
// get rows(){ return this.numRows() },
// get cols(){ return this.numColumns() },
row(){ return this.getRow() },
col(){ return this.getColumn() },
rows(){ return this.getNumRows() },
cols(){ return this.getNumColumns() },
// 🔸 range.toString()
// ⭐ 注意:
// Range 有自己的 toString() (returns "Range"),所以如果要用 prototype 的版本,必須用下列程式碼:
// • range.__proto__.toString.call(range)
// • use `range.info()` instead
toString(){
return `Range at: (${this.row()}, ${this.col()}), dim: ${this.rows()} ⨉ ${this.cols()}`;
},
// 🔸 range.info()
info(){
return `Range at: (${this.row()}, ${this.col()}), dim: ${this.rows()} ⨉ ${this.cols()}`;
},
// ┌─────────┐
// │ Range │
// └─────────┘
// 🔸 range.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(`⛔ app.range.prototype.inset: new range can't start from row = ${row}`);
if (col < 0) throw new Error(`⛔ app.range.prototype.inset: new range can't start from col = ${col}`);
if (rows < 1) throw new Error(`⛔ app.range.prototype.inset: new range can't have ${rows} rows`);
if (cols < 1) throw new Error(`⛔ app.range.prototype.inset: new range can't have ${cols} cols`);
let range = this.offset(row - this.row(), col - this.col(), rows, cols);
range.__proto__ = app.range.prototype;
return range;
},
// 🔸 range.nthRow(i)
nthRow(i){
let row = this.row() + i - 1;
if (row < 0) throw new Error(`⛔ app.range.prototype.nthRow(): new range can't start from row = ${row}`);
let range = this.offset(row - this.row(), 0, 1, this.cols());
range.__proto__ = app.range.prototype;
return range;
},
// 🔸 range.nthColumn(j)
nthColumn(j){
let col = this.col() + j - 1;
if (col < 0) throw new Error(`⛔ app.range.prototype.nthColumn(): new range can't start from col = ${col}`);
let range = this.offset(0, col - this.col(), this.rows(), 1);
range.__proto__ = app.range.prototype;
return range;
},
// 🔸 range.firstNRows(n)
firstNRows(n){
let range = this.offset(0, 0, n, this.cols());
range.__proto__ = app.range.prototype;
return range;
},
// 🔸 range.firstNColumns(n)
firstNColumns(n){
let range = this.offset(0, 0, this.rows(), n);
range.__proto__ = app.range.prototype;
return range;
},
// 🔸 range.lastNthColumn(j)
lastNthColumn(j){
let range = this.offset(0, this.cols() - j, this.rows(), 1);
range.__proto__ = app.range.prototype;
return range;
},
// 🔸 range.lastNColumns(n)
lastNColumns(n){
let range = this.offset(0, this.cols() - n, this.rows(), n);
range.__proto__ = app.range.prototype;
return range;
},
// 🔸 range.cell(i, j)
cell(i, j){
let row = this.row() + i - 1;
let col = this.col() + j - 1;
if (row < 0) throw new Error(`⛔ app.range.prototype.cell(): new range can't start from row = ${row}`);
if (col < 0) throw new Error(`⛔ app.range.prototype.cell(): new range can't start from col = ${col}`);
let range = this.offset(row - this.row(), col - this.col(), 1, 1);
range.__proto__ = app.range.prototype;
return range;
},
// ┌─────────────┐
// │ formatting │
// └─────────────┘
// 🔸 range.alignCenter()
alignCenter(){
this
.setVerticalAlignment('middle')
.setHorizontalAlignment('center');
return this; // for chaining
},
};
🧨 雷區:
Google Apps Script 似乎無法處理在 💡 custom prototypes 中的 getters❗️
難道 GAS 也有 ⛔️ Object.assign causing TypeError 的問題❓
app.range.prototype = {
// ❓ GAS doesn't work well with getters
// ⛔ TypeError: this.getRow is not a function
get row(){ return this.getRow() },
};
Last updated