๐ณ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
},
};
app.makeTable() - ่ฃฝ่กจ
๐งจ ้ทๅ๏ผ
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