// 使用法: // このファイルを Range.cms という名前で Cassava Editor の Macro/lib フォルダに置いてください。 // // SpreadsheetApp.getActiveRange() で、選択範囲を表す Range オブジェクトを取得できます。 // SpreadsheetApp.getCurrentCell() で、カーソルのある単一セルを表す Range オブジェクトを取得できます。 // SpreadsheetApp.getActiveSheet().getDataRange() で、データのある範囲を表す Range オブジェクトを取得できます。 // SpreadsheetApp.getActiveSheet().getRange(row, column, numRows, numColumns) で、 // 指定した範囲を表す Range オブジェクトを取得できます。 // // Range クラスは、Google Apps Script の Range クラスの一部のメソッドを実装しています。 // https://developers.google.com/apps-script/reference/spreadsheet/range import { Array } from "lib/Array.cms"; class Range { constructor(startRow, startColumn, numRows, numColumns) { this.startRow = startRow; this.startColumn = startColumn; this.numRows = numRows; this.numColumns = numColumns; } activate() { Select(this.startColumn, this.startRow, this.startColumn + this.numColumns - 1, this.startRow + this.numRows - 1); return this; } activateAsCurrentCell() { Col = this.startColumn; Row = this.startRow; return this; } clear() { for (y = this.startRow; y < this.startRow + this.numRows; y++) { for (x = this.startColumn; x < this.startColumn + this.numColumns; x++) { [x,y] = ""; } } return this; } clearContent() { return this.clear(); } copyTo(destination) { new Range(destination.startRow, destination.startColumn, this.numRows, this.numColumns).setValues(this.getValues()); } getCell(row, column) { return new Range(this.startRow + row - 1, this.startColumn + column - 1, 1, 1); } getColumn() { return this.startColumn; } getHeight() { return this.numRows; } getLastColumn() { return this.startColumn + this.numColumns - 1; } getLastRow() { return this.startRow + this.numRows - 1; } getRow() { return this.startRow; } getRowIndex() { return this.startRow; } getValue() { return [this.startColumn, this.startRow]; } getValues() { result = new Array(); for (y = this.startRow; y < this.startRow + this.numRows; y++) { row = new Array(); for (x = this.startColumn; x < this.startColumn + this.numColumns; x++) { row.push([x,y]); } result.push(row); } return result; } getWidth() { return this.numColumns; } isBlank() { for (y = this.startRow; y < this.startRow + this.numRows; y++) { for (x = this.startColumn; x < this.startColumn + this.numColumns; x++) { if ([x,y] != "") { return false; } } } return true; } moveTo(destination) { values = this.getValues(); this.clear(); new Range(destination.startRow, destination.startColumn, this.numRows, this.numColumns).setValues(values); } offset(rowOffset, columnOffset, ...numbers) { if (numbers.length >= 2) { return new Range(this.startRow + rowOffset, this.startColumn + columnOffset, numbers[0], numbers[1]); } else if (numbers.length == 1) { return new Range(this.startRow + rowOffset, this.startColumn + columnOffset, numbers[0], this.numColumns); } else { return new Range(this.startRow + rowOffset, this.startColumn + columnOffset, this.numRows, this.numColumns); } } setValue(value) { [this.startColumn, this.startRow] = value; } setValues(values) { for (y = 0; y < values.length; y++) { row = values[y]; for (x = 0; x < row.length; x++) { [this.startColumn + x, this.startRow + y] = row[x]; } } return this; } } function getActiveRange() { return new Range(SelTop, SelLeft, SelBottom - SelTop + 1, SelRight - SelLeft + 1); } function getCurrentCell() { return new Range(Row, Col, 1, 1); } function getDataRange() { return new Range(1, 1, Bottom, Right); }