npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@szk302/excel-cell-fitter

v0.0.0

Published

Excel の列幅を、指定フォント(既定 Meiryo UI)で各列の全セルが途切れず表示されるよう自動調整するツール

Readme

excel-cell-fitter

Excel(.xlsx)の列幅を、各列の全セルが途切れず表示されるよう自動調整する TypeScript 製ツールです。表示に使うフォントを指定でき、既定は Meiryo UI です。

列に入る値は ASCII・マルチバイト・環境依存文字・日付/日時など様々で、それぞれ 表示幅が異なります。本ツールは各列について「値のあるセルの最大表示幅」を求め、 それを列幅に設定します。

しくみ

フォント

  • 対象セルのフォントを --font(既定 Meiryo UI)に書き換えたうえで、その フォントでの表示幅に合わせて列幅を決めます。MS ゴシックのブックをそのまま かけると、Meiryo UI で表示され幅も合った状態で出力されます。
  • サイズ・太字・色などフォント名以外の書式は維持します。フォントは値のあるセル だけでなく列のスタイルにも設定するので、データ範囲内の空白セルや、その列に 後から入力するセルも同じフォントで表示されます(データ範囲より右の未使用列は 対象外です)。結合セルは幅の計算には使いませんが、フォントは適用します。
  • 幅の計算だけ行いフォントを変えたくない場合は --keep-font を使います。
  • 幅はフォントごとの字送り幅で求めます。プロポーショナルフォントでは iW の幅の差が反映され、MS ゴシックのような等幅和文フォントでは ASCII が すべて同じ幅になります。フォントサイズに比例させ、太字は少し広く見積もります。
  • 収録していないフォント名を指定した場合は、プロポーショナル和文フォント相当の 近似で測ります(フォント名自体は指定どおりに設定されます)。

収録フォント: Meiryo UI / Meiryo / Yu Gothic / MS PGothic / MS Gothic / Calibri / Arial / Segoe UI / Consolas / Courier New(MS ゴシック のような全角表記や 別名も解決します)

幅の値について: 各フォントの字送り幅は設計値からの近似で、グリフ実測では ありません(Excel の表示は環境のフォント置換に左右されるため、実測しても近似に しかならないという判断です)。文字ごとの相対的な広さは Helvetica/Arial の AFM 実測値を基準テーブルとして流用し、フォント固有の数字幅でスケールしています。 精度を上げたい場合は src/fonts.ts の表だけを差し替えれば済みます。

測定の割り切り

文字ごとの幅を区別するのは ASCII(U+0020〜U+007E)だけです。以下は意図的に 一律とし、いずれも「狭くして切れる」より「広くして余る」側に倒しています。

  • 全角はすべて 1em。漢字は設計上ちょうど 1em なので正確ですが、Meiryo UI・ MS Pゴシック・MS UI Gothic・Yu Gothic UI はかなと約物を詰めるため、かな中心の 列は実際の表示より少し広くなります。
  • East Asian Width が曖昧(Ambiguous)な文字は全角。丸数字①や環境依存文字が 切れないための既定ですが、合成済み(NFC)の é のようなラテン文字も全角として 数えます。欧文中心のブックでは --ambiguous-narrow を使ってください。
  • キリル文字・ギリシャ文字など ASCII 外の半角文字は一律 0.5em。フォントごとの 差も文字ごとの差もつきません。

幅の単位

Excel の列幅は「Normal スタイルフォントの最大数字幅(MDW)の何文字ぶんか」という 単位です。ExcelJS は出力時に Normal スタイルを必ず Calibri 11(MDW = 7px)で 書き出すため、本ツールもセルのフォントで測ったピクセル数を 7px で割って列幅に 換算します。セルのフォントを Meiryo UI に変えても単位は変わらないため、この換算を 挟まないと列が狭くなり文字が切れます。

なお ExcelJS は入力ファイルの Normal スタイルを保持しません。元ファイルが既定 スタイル任せでMS ゴシックだった場合、本ツールが各セルへ明示的にフォントを 設定することで、出力でも意図したフォントが保たれます。

そのほか

  • 数値・日付/日時はセルの表示形式(numFmt)を numfmt で適用してから測るため、実際に画面へ出る文字列の幅になります。
  • 文字の全角/半角の判定には East Asian Width を使います。丸数字や環境依存文字 などの「曖昧(Ambiguous)」幅は既定で全角として数え、切れないよう安全側に倒します。
  • セル内改行は最長行を採用します。結合セルは幅の計算対象外です。
  • 列幅は Excel 仕様の上限 255 でクランプします。

対象シート

--sheets で対象シートを絞り込めます。既定は全シートです。対象外のシートは 列幅もフォントも一切変更しません。 指定した名前がブックに無い場合は、書き出さずに エラーで終了します(指定ミスに気づけるようブック内のシート名も表示します)。

インストール

npm install -g @szk302/excel-cell-fitter

開発

make setup   # 依存インストール+ビルド(npm ci && npm run build)
make test    # テスト(vitest)

PR と main への push では GitHub Actions(.github/workflows/ci.yml)が ビルド・テスト・テストコードの型チェック(npx tsc -p test)・公開物の内容確認を 実行します。リリースは docs/RELEASE.md を参照してください。

CLI

excel-cell-fitter <input.xlsx> [options]

| オプション | 説明 | | --- | --- | | -o, --output <path> | 出力先(省略時は <name>.fitted.xlsx) | | --in-place | 入力ファイルを上書き | | --font <name> | 表示に使うフォント(既定 Meiryo UI) | | --keep-font | セルのフォントを書き換えず、幅の計算だけ --font で行う | | --padding <n> | 余白(列幅単位、既定 0.71) | | --min-width <n> | 列幅の下限 | | --max-width <n> | 列幅の上限(既定 255) | | --sheets <a,b> | 対象シート名(カンマ区切り、既定は全シート) | | --ambiguous-narrow | 曖昧幅の文字(①§等)を半角として数える(既定は全角=安全側) | | -h, --help | ヘルプ表示 |

例:

# MS ゴシックのブックを Meiryo UI 表示に合わせて調整し、book.fitted.xlsx に出力
excel-cell-fitter book.xlsx

# 特定シートだけを対象にする(他のシートは崩さない)
excel-cell-fitter book.xlsx --sheets 明細,集計

# フォントを指定して上書き
excel-cell-fitter book.xlsx --in-place --font 游ゴシック

# フォントは元のまま、Meiryo UI で表示した場合の幅に合わせる
excel-cell-fitter book.xlsx --keep-font

ライブラリ

import { fitFile, fitWorkbook, fitColumns } from '@szk302/excel-cell-fitter';
import ExcelJS from 'exceljs';

// ファイル単位(調整したシート名を返す)
const fitted = await fitFile('in.xlsx', 'out.xlsx', { padding: 1 });

// ExcelJS のワークブック/ワークシートを直接調整
const wb = new ExcelJS.Workbook();
await wb.xlsx.readFile('in.xlsx');
fitWorkbook(wb, { sheets: ['Sheet1'] });
// もしくは 1 シートだけ:
fitColumns(wb.worksheets[0], { font: 'MS Pゴシック', maxWidth: 40 });
await wb.xlsx.writeFile('out.xlsx');

オプション(FitOptions

| キー | 既定 | 説明 | | --- | --- | --- | | font | Meiryo UI | 表示に使うフォント名 | | applyFont | true | セルのフォントを font に書き換えるか(false なら測定のみ) | | padding | 0.71 | 内容幅に加える余白(列幅単位) | | minWidth | なし | 列幅の下限 | | maxWidth | 255 | 列幅の上限 | | sheets | 全シート | 対象シート名または 1 始まりの id。対象外は変更しない | | dateFormatDefault | yyyy/mm/dd | numFmt の無い日付セルの表示形式 | | ambiguousAsWide | true | 曖昧幅の文字を全角として数える |