RAS Playground 日本語 GitHub

RAS standard library

Range, sheets and workbooks

These are extension methods added to Excel's Range, Worksheet, Workbook and Workbooks (ras_range / ras_sheet / ras_book of the RAS standard library). You can use them without importing anything. Only the methods you use are emitted into the VBA modules RAS_Range, RAS_Sheet and RAS_Book. Excel's own members (cells, end, currentRegion, etc.) are in the Excel type definitions.

export function salesTotal(): double {
    const ws = Sheet1;
    const last = ws.lastRow();                         // the last row that has a value
    const header = ws.find("Amount");                  // the heading cell (null if not found)
    if (header == null) return 0.0;
    let total = 0.0;
    for (let r = header.row + 1; r <= last; r++) {
        const v = ws.cells(r, header.column).value;
        if (isError(v)) continue;                       // error values such as #N/A
        total += v as double;
    }
    return total;
}

export function copyByHeaders() {
    // pick columns by heading, read them, and write them to another sheet
    const rows = Sheet1.range("A1").readTable(["Product", "Amount"]);
    Sheet2.range("A1").writeTable(rows);
}

export function monthSheet(): Excel.Worksheet {
    const book = thisWorkbook;
    return book.sheet(/^Month \d+$/);                   // find a sheet by name or regular expression
}

A loop that reads and writes cells one by one is converted into a bulk transfer to and from an array if you simply write it (specification §9). The range extended up, down, left or right range.extend(…), the range given by numbers ws.range(top, left, bottom, right) and the bottom-right cell range.lastCell are compiler built-ins (§9.4).

Reference

Extension method (Excel.Range) Range.findAll

function findAll(this: Excel.Range, what: any, match: string = "whole", strict: boolean = false, visibleOnly: boolean = false, order: string = "rows"): Excel.Range[]

Every cell in the range that matches what (an empty array if none). Only cells with values are searched, and a merged cell is found once, as its top-left cell.

  • what is a string: compared with the cell's text. By default (strict: false), full-width/half-width, case and whitespace (including line break) differences are ignored. Number and date cells match both the text shown ("1,200") and the value ("1200")
  • what is a regular expression (/^Total/): tried on the cell's text (with strict: false, also on the text with full-width characters made half-width)
  • what is a number, date or boolean: compared with the cell's value
  • match: "whole" (default; the whole cell) or "partial" (part of the cell)
  • visibleOnly: cells in hidden rows and columns (including rows hidden by a filter) are not searched
  • order: "rows" (default; row by row from the left) or "columns" (column by column from the top)

Example: Sheet1.columns(1).findAll("Total"), ws.range("A1:F20").findAll(/^Q\d$/, visibleOnly: true)

ras_range.ras

Extension method (Excel.Range) Range.findByHeaders

function findByHeaders(this: Excel.Range, column: any, row: any, strict: boolean = false, visibleOnly: boolean = false): Excel.Range

The cell where the column headed column meets the row headed row (null if either heading is not found). Headings are searched in the range with the same matching as findAll, and the first found is used.

Example: ws.usedRange.findByHeaders("April", "Sales") → the cell in the column headed "April" and the row headed "Sales"

ras_range.ras

Extension method (Excel.Range) Range.stepOffset

function stepOffset(this: Excel.Range, rows: int = 0, columns: int = 0, visibleOnly: boolean = true): Excel.Range

The cell reached by moving cell by cell rows rows down and columns columns right (negative numbers for up and left). A merged cell counts as one cell (if A1:B1 is merged, one column right of A1 is C1), and the top-left cell of the merged cell reached is returned. Passing through a merged cell keeps the row or column moved along (down from column B through merged A2:B2 to B3, not to column A of the merged cell's top-left). With visibleOnly (default true), hidden rows and columns (including rows hidden by a filter) are skipped without counting. For a range of several cells, the union of each cell moved. A run-time error 1004 when it goes off the sheet.

Example: ws.findAll("Name")[0].stepOffset(0, 1).value → the value of the (merged) cell to the right of the heading "Name"

ras_range.ras

Extension method (Excel.Range) Range.stepResize

function stepResize(this: Excel.Range, rows: int = 1, columns: int = 1, visibleOnly: boolean = true, withinBorders: boolean = false): Excel.Range

The range of rows rows and columns columns counted cell by cell from the top-left cell of the range (a merged cell counts as one cell and its top-left cell is included). With visibleOnly (default true), hidden rows and columns are not counted (the range returned has several areas, without the skipped rows and columns). With withinBorders, the range extends only as far as the border frame continues (to the right while the top and bottom borders are the same and the left border equals the right border of the first cell; downward while the left and right borders are the same and the top border equals the bottom border of the first row).

Example: ws.findAll("Details")[0].stepOffset(1, 0).stepResize(5, 3) → the 5 rows and 3 columns below the heading (a merged cell counts as one)

ras_range.ras

Extension method (Excel.Range) Range.lastRow

function lastRow(this: Excel.Range, skipBlank: boolean = false): int

Number of the last row in the range that has a value or formula (0 if none). ws.columns(1).lastRow() gives the last row of column A (unlike VBA's Cells(Rows.Count, 1).End(xlUp).Row, hidden and filtered-out rows are counted, and an empty column gives 0). A merged cell with a value counts up to the last row of the merge. With skipBlank, cells that are empty or only whitespace (including formulas whose result is "") are not counted.

Example: ws.columns(3).lastRow(skipBlank: true) → rows where a filled-down =IF(A2="","",…) gives "" are not counted

ras_range.ras

Extension method (Excel.Range) Range.lastColumn

function lastColumn(this: Excel.Range, skipBlank: boolean = false): int

Number of the last column in the range that has a value or formula (0 if none). ws.rows(1).lastColumn() gives the last column of row 1. Counted as in lastRow.

ras_range.ras

Extension method (Excel.Range) Range.readTable

function readTable(this: Excel.Range, pick: any[] = [], untilBlank: boolean = false): cellValue[][]

Reads the data of the table starting at the top-left cell of the heading row (this cell) into a 2-D array (without the heading row; indexes from 0). The table's columns extend right from this cell as far as the headings continue (up to the first empty cell). The rows go from below the headings to the last row of the table's columns.

  • pick: the columns to read and their order. Heading strings (ignoring full-width/half-width, case and whitespace differences), regular expressions (/Amount|Sales/), positions from the left edge of the table (int, from 0), null (an empty column). When omitted, every column of the table. A heading that is not found raises an exception (listing the headings)
  • untilBlank: a row whose table columns are all empty ends the table (totals and notes below are not read)

Example: ws.findAll("Code")[0].readTable(["Code", /Amount/], untilBlank: true)

ras_range.ras

Extension method (Excel.Range) Range.writeTable

function writeTable<T>(this: Excel.Range, values: T[][], pick: any[] = []): void

Writes values into the table starting at the top-left cell of the heading row (this cell), from right below the headings. With pick, the columns of values are written in order to the columns of pick (given as in readTable; null columns are not written), leaving the other columns (formula columns and so on) as they are. When omitted, values is written as is from the left edge of the table.

Example: ws.range("A1").writeTable(rows, ["Date", "Amount"])

ras_range.ras

Extension method (Excel.Worksheet) Worksheet.find

function find(this: Excel.Worksheet, what: any, match: string = "whole", strict: boolean = false, visibleOnly: boolean = false, order: string = "rows"): Excel.Range

The first cell in the sheet that matches what (searched among cells with values; null if none). Matching and arguments are as in range.findAll (strings ignore full-width/half-width, case and whitespace differences; regular expressions like /^Total/; numbers, dates and booleans).

Example: Sheet1.find("Name").stepOffset(0, 1).value → the value of the cell to the right of the heading "Name"

ras_sheet.ras

Extension method (Excel.Worksheet) Worksheet.findAll

function findAll(this: Excel.Worksheet, what: any, match: string = "whole", strict: boolean = false, visibleOnly: boolean = false, order: string = "rows"): Excel.Range[]

Every cell in the sheet that matches what (in order; by default row by row from the left). Matching and arguments are as in range.findAll.

ras_sheet.ras

Extension method (Excel.Worksheet) Worksheet.findByHeaders

function findByHeaders(this: Excel.Worksheet, column: any, row: any, strict: boolean = false, visibleOnly: boolean = false): Excel.Range

The cell where the column headed column meets the row headed row (null if either is not found). range.findByHeaders over the whole sheet.

Example: Sheet1.findByHeaders("April", "Sales").value

ras_sheet.ras

Extension method (Excel.Worksheet) Worksheet.lastRow

function lastRow(this: Excel.Worksheet, skipBlank: boolean = false): int

Number of the last row in the sheet that has a value or formula (0 for an empty sheet). Hidden and filtered-out rows are counted. For the last row of a column, use ws.columns(1).lastRow(). With skipBlank, cells that are empty or only whitespace (including formulas whose result is "") are not counted.

ras_sheet.ras

Extension method (Excel.Worksheet) Worksheet.lastColumn

function lastColumn(this: Excel.Worksheet, skipBlank: boolean = false): int

Number of the last column in the sheet that has a value or formula (0 for an empty sheet). For the last column of a row, use ws.rows(1).lastColumn(). Counted as in lastRow.

ras_sheet.ras

Extension method (Excel.Workbook) Workbook.sheet

function sheet(this: Excel.Workbook, name: any): Excel.Worksheet

Returns a worksheet by name or regular expression (null if none).

  • String: the sheet with that name. If there is none, names are compared ignoring full-width/half-width, case and whitespace differences, and the sheet is returned if exactly one matches
  • Regular expression: the first sheet whose name matches (also tried on the name with full-width characters made half-width)

Example: ThisWorkbook.sheet(/^\d{4}-\d{2}$/) → the first sheet named like "2026-04"

ras_book.ras

Extension method (Excel.Workbook) Workbook.copySheet

function copySheet(this: Excel.Workbook, sheet: Excel.Worksheet, name: string = "", before: Excel.Worksheet = null, after: Excel.Worksheet = null): Excel.Worksheet

Copies the sheet and returns the copy (Excel's "Move or Copy" with "Create a copy"). The copy is placed before before or after after. When both are omitted, it goes right after the original in the same workbook, or at the end of another workbook. When name is omitted, the copy keeps the name Excel gives it ("Sales (2)"). When the name cannot be set (a sheet with that name exists, for example), the copy is deleted and an error is raised.

Example: const next = ThisWorkbook.copySheet(Sheet1, "May");

ras_book.ras

Extension method (Excel.Workbooks) Workbooks.opened

function opened(this: Excel.Workbooks, path: string): Excel.Workbook

The open workbook whose file name equals the last part of path ("sales.xlsx" for "C:\\data\\sales.xlsx"), or null if none. Excel cannot open two workbooks with the same name, so the name identifies it.

Example: const book = workbooks.opened("sales.xlsx"); if (book != null) book.close(false);

ras_book.ras

Extension method (Excel.Workbooks) Workbooks.ensureOpen

function ensureOpen(this: Excel.Workbooks, path: string, readOnly: boolean = false, updateLinks: int = 0): Excel.Workbook

Returns the workbook at path: the open one if it is already open (opened), otherwise opens it (workbooks.open, using readOnly and updateLinks).

Example: const src = workbooks.ensureOpen(path, readOnly: true);

ras_book.ras