mcpField
W
working-with-spreadsheets

working-with-spreadsheets

github.com/mjunaidcaドキュメント28154 回インストール11 週間前に更新
リポジトリ

working-with-spreadsheets について

working-with-spreadsheets は、Python ライブラリで Excel ブックを扱うためのスキルで、表計算ファイルの作成、分析、保守を行う人向けです。数式による計算、財務モデルの書式、数式エラーの確認を重視します。

主な機能

  1. 01pandas による Excel データ分析
  2. 02openpyxl によるブックの作成と編集
  3. 03財務モデル向け色と数値書式の適用
  4. 04数式の再計算とエラー確認
  5. 05よくある数式問題のトラブルシューティング

活用シーン

  1. 01数式を使った財務モデルの作成
  2. 02ブック内の複数シートにまたがるデータ分析
  3. 03数式を保持した既存ブックの編集
  4. 04数式参照と計算エラーの確認

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/mjunaidca/mjs-agent-skills --skill working-with-spreadsheets

インストール後、Claude Code で/working-with-spreadsheetsを入力して読み込まれたか確認します。

同梱ファイルscripts/verify.py

詳細

スプレッドシートの操作

クイックスタート

from openpyxl import Workbook

wb = Workbook()
sheet = wb.active
sheet['A1'] = 'Revenue'
sheet['B1'] = 1000
sheet['B2'] = '=B1*1.1'  # Use formulas, not hardcoded values!
wb.save('output.xlsx')

重要なルール: ハードコードされた値ではなく数式を使用する

Python で計算するのではなく、常に Excel の数式を使用してください。

# WRONG - Hardcoding calculated values
total = df['Sales'].sum()
sheet['B10'] = total  # Hardcodes 5000

# CORRECT - Using Excel formulas
sheet['B10'] = '=SUM(B2:B9)'

財務モデルの色分け標準

色RGB用途
青い文字0,0,255ハードコードされた入力値、シナリオ値
黒い文字0,0,0すべての数式と計算
緑の文字0,128,0他のワークシートからのリンク
赤い文字255,0,0他のファイルへの外部リンク
黄色の背景255,255,0注意が必要な主要な前提条件
from openpyxl.styles import Font

# Input cell (user changeable)
sheet['B5'].font = Font(color='0000FF')  # Blue

# Formula cell
sheet['C5'] = '=B5*1.1'
sheet['C5'].font = Font(color='000000')  # Black

# Cross-sheet link
sheet['D5'] = "=Sheet2!A1"
sheet['D5'].font = Font(color='008000')  # Green

数値書式の標準

# Currency with thousands separator
sheet['B5'].number_format = '$#,##0'

# Zeros display as dash
sheet['B5'].number_format = '$#,##0;($#,##0);-'

# Percentages with one decimal
sheet['C5'].number_format = '0.0%'

# Valuation multiples
sheet['D5'].number_format = '0.0x'

# Years as text (not 2,024)
sheet['A1'] = '2024'  # String, not number

ライブラリの選択

タスクライブラリ例
データ分析pandasdf = pd.read_excel('file.xlsx')
数式と書式設定openpyxlsheet['A1'] = '=SUM(B:B)'
大きなファイル (読み取り)openpyxlload_workbook('file.xlsx', read_only=True)
大きなファイル (書き込み)openpyxlWorkbook(write_only=True)

Excel ファイルの読み取り

import pandas as pd
from openpyxl import load_workbook

# pandas - data analysis
df = pd.read_excel('file.xlsx')
all_sheets = pd.read_excel('file.xlsx', sheet_name=None)  # Dict of DataFrames

# openpyxl - preserve formulas
wb = load_workbook('file.xlsx')
sheet = wb.active
print(sheet['A1'].value)  # Returns formula string

# openpyxl - get calculated values (WARNING: loses formulas on save!)
wb = load_workbook('file.xlsx', data_only=True)

Excel ファイルの作成

from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment

wb = Workbook()
sheet = wb.active
sheet.title = 'Model'

# Headers
sheet['A1'] = 'Metric'
sheet['B1'] = '2024'
sheet['A1'].font = Font(bold=True)

# Data with formulas
sheet['A2'] = 'Revenue'
sheet['B2'] = 1000000
sheet['B2'].font = Font(color='0000FF')  # Blue = input

sheet['A3'] = 'Growth'
sheet['B3'] = '=B2*0.1'
sheet['B3'].font = Font(color='000000')  # Black = formula

# Formatting
sheet['B2'].number_format = '$#,##0'
sheet.column_dimensions['A'].width = 20

wb.save('model.xlsx')

既存ファイルの編集

from openpyxl import load_workbook

wb = load_workbook('existing.xlsx')
sheet = wb['Data']  # Or wb.active

# Modify cells
sheet['A1'] = 'Updated Value'
sheet.insert_rows(2)
sheet.delete_cols(3)

# Add new sheet
new_sheet = wb.create_sheet('Analysis')
new_sheet['A1'] = '=Data!B5'  # Cross-sheet reference

wb.save('modified.xlsx')

数式の再計算

openpyxl は数式を書き込みますが、値を計算しません。 再計算するには LibreOffice を使用してください:

# Recalculate and check for errors
python recalc.py output.xlsx

このスクリプトは JSON を返します:

{
  "status": "success",  // or "errors_found"
  "total_errors": 0,
  "total_formulas": 42,
  "error_summary": {
    "#REF!": {"count": 2, "locations": ["Sheet1!B5", "Sheet1!C10"]}
  }
}

数式検証チェックリスト

構築前
  • まず 2〜3 個のサンプル参照をテストする
  • 列のマッピングを確認する (列 64 = BL、BK ではない)
  • 覚えておく: DataFrame の行 5 = Excel の行 6 (1 から始まるインデックス)
よくある落とし穴
  • 値を使用する前に pd.notna() で NaN をチェックする
  • FY データはしばしば列 50 以降 (右端) にある
  • 最初の一致だけでなく、すべての出現箇所を検索する
  • 除算の前に分母をチェックする (#DIV/0!)
  • シート間参照が正しい形式 (Sheet1!A1) を使用していることを確認する
構築後
  • recalc.py を実行し、エラーを修正する
  • #REF!、#DIV/0!、#VALUE!、#NAME? = 0 を確認する

よくあるエラー

エラー原因修正
#REF!無効なセル参照削除された行/列を確認する
#DIV/0!ゼロによる除算IF チェックを追加する: =IF(B5=0,0,A5/B5)
#VALUE!データ型が正しくないセルに期待される型が含まれているか確認する
#NAME?不明な関数スペル、テキストを囲む引用符を確認する

検証

実行: python scripts/verify.py

関連スキル

  • building-nextjs-apps - スプレッドシートアップロード用のフロントエンド
  • scaffolding-fastapi-dapr - スプレッドシート処理用の API

コメント