apps-script-utils 2.1.1 Help

isConsistent2DArray

function isConsistent2DArray(arr: unknown): arr is Array<Array<unknown>>;

This is the check to run before Range#setValues, which rejects a ragged matrix with a message that names neither the offending row nor the reason. Everything is2DArray requires holds here as well — a non-empty array of arrays — plus the rule that every row is as wide as the first.

As a TypeScript type predicate it narrows the value to Array<Array<unknown>> inside the branch.

Parameters

Parameter

Type

Description

arr

unknown

The value to check.

Returns

boolean — true if the value is a non-empty array of arrays of equal length; otherwise false.

In TypeScript the guard narrows arr to Array<Array<unknown>> inside the branch.

Examples

Uniform rows, ragged rows, and non-matrices

isConsistent2DArray([ [1, 2], [3, 4] ]); // => true isConsistent2DArray([[1, 2], [3]]); // => false — rows differ in length isConsistent2DArray([1, 2]); // => false — not 2D at all isConsistent2DArray([]); // => false

Before writing to a range

/** * Writes a matrix to a sheet, refusing a ragged one with a message that says * what is wrong — which `setValues` itself does not. */ export function write(sheet: GoogleAppsScript.Spreadsheet.Sheet, rows: unknown): void { if (!isConsistent2DArray(rows)) { throw new Error("Every row must have the same number of columns."); } sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows); }
function write(sheet, rows) { if (!isConsistent2DArray(rows)) { throw new Error("Every row must have the same number of columns."); } sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows); }

See also

Source

src/lang/array/isConsistent2DArray.ts

23 September 2026

This documentation was generated with AI (Claude) from the library's source code.