メインコンテンツまでスキップ

読み取りAPI

既存のExcelファイルを読み込むためのAPIです。

read()

import { read } from "xlmake";

// ファイルパスから読み込み
const workbook = await read("report.xlsx");

// Bufferから読み込み
const workbook = await read(buffer);

WorkbookReader

ワークブック全体を表します。

プロパティ/メソッド戻り値説明
sheetNamesstring[]シート名の一覧
sheetCountnumberシート数
sheet(name)SheetReader名前でシートを取得
sheetAt(index)SheetReaderインデックスでシートを取得
const workbook = await read("report.xlsx");

console.log(workbook.sheetNames); // ["売上", "在庫"]
console.log(workbook.sheetCount); // 2

const sheet = workbook.sheet("売上");
const firstSheet = workbook.sheetAt(0);

SheetReader

シートを表します。

プロパティ/メソッド戻り値説明
namestringシート名
rowCountnumber行数
columnCountnumber列数
mergedCellsstring[]マージ情報("A1:B2"形式)
cell(address)CellReaderA1形式でセルを取得
cellAt(row, col)CellReader行・列番号でセルを取得
const sheet = workbook.sheet("売上");

console.log(sheet.name); // "売上"
console.log(sheet.rowCount); // 100
console.log(sheet.columnCount); // 5
console.log(sheet.mergedCells); // ["A1:B1", "C1:C2"]

// A1形式でアクセス
const cell1 = sheet.cell("A1");

// 行・列番号でアクセス(0-indexed)
const cell2 = sheet.cellAt(0, 0); // A1と同じ

CellReader

セルを表します。

プロパティ戻り値説明
valuestring | number | boolean | nullセルの値
styleCellStyle | undefinedセルのスタイル
borderCellBorder | undefinedセルの罫線情報
const cell = sheet.cell("A1");

console.log(cell.value); // "商品名"

const style = cell.style;
console.log(style?.bold); // true
console.log(style?.fill); // "#4472C4"

const border = cell.border;
console.log(border?.top); // { style: "thin", color: "#000000" }
console.log(border?.bottom); // { style: "thin", color: "#000000" }

使用例

import { read } from "xlmake";

// Excelファイルを読み込み
const workbook = await read("./report.xlsx");

// シート一覧を取得
console.log(workbook.sheetNames); // ["売上", "在庫"]

// シートを取得
const sheet = workbook.sheet("売上");

// セルの値を取得
console.log(sheet.cell("A1").value); // "商品名"
console.log(sheet.cell("B2").value); // 100

// セルのスタイルを取得
const style = sheet.cell("A1").style;
console.log(style?.bold); // true
console.log(style?.fill); // "#4472C4"

// マージ情報を取得
console.log(sheet.mergedCells); // ["A1:B1", "C1:C2"]

// 行・列番号でアクセス(0-indexed)
console.log(sheet.cellAt(0, 0).value); // A1の値
console.log(sheet.cellAt(1, 1).value); // B2の値

関連