![](/style/images/good.png)
![](/style/images/bad.png)
GitHub - tatsuya-midorikawa/Fxcel: Excel COM Wrapper Library for F#
source link: https://github.com/tatsuya-midorikawa/Fxcel
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Fxcel - Excel operations library
What's this?
- Fxcel は F# で簡単に Excel の COM 操作をするためのライブラリです。
- C# 向けの Excel COM 操作ライブラリである Midoliy.Office.Interop.Excel のラッパーライブラリとなります。
- .NET 5.0 以上の環境をサポートしています。
- 主に F# Script や F# Interactive での利用を想定して設計をしていますが、Console アプリや Desktop アプリでも問題なく利用可能です。
- COM を利用するため Excel のインストールが必要です。
Get started
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
F# Interactiveで利用する
1. FSIを起動する
dotnet fsi
2. Fxcel を読み込む
Fxcel を nuget から読み込みます。
#r "nuget: Fxcel, 0.0.20";; open Fxcel;;
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
F# Scriptで利用する
1. .fsx ファイルを作成する
まずはコーディングを始めるために main.fsx を作成して、VSCode で開きましょう。
mkdir D:/work cd D:/work new-item main.fsx code D:/work
2. Fxcel を読み込む
main.fsx に Fxcel を利用するためのコードを追加します。
#r "nuget: Fxcel" open Fxcel
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
F# プロジェクトで利用する
1. 新規プロジェクトを作成する
mkdir D:/work cd D:/work dotnet new console -lang=F# -o=FxcelSample
2. Fxcel を読み込む
cd D:/work/FxcelSample dotnet add package Fxcel
Reference for F# Interactive
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
起動中のExcelプロセス一覧をターミナルに表示しつつ取得する
show ()
let processList = show ();;
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
起動中のExcelプロセス一覧を取得する
enumerate ()
let processList = enumerate ();;
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
起動中のExcelプロセスにアタッチする
attach (excel: Handle)
let processList = enumerate ();; let excel = processList.[0] |> attach;;
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
アタッチ済みのExcelプロセスをデタッチする
detach (excel: IExcelApplication)
let processList = enumerate ();; let excel = processList.[0] |> attach;; // do somethings excel |> detach;;
Reference
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
Workbookを新規作成する
create (): IExcelApplication
[<EntryPoint>] let main argv = use excel = create ()
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
既存Workbookをテンプレートとして新規Workbookを作成する
create (template: string): IExcelApplication
[<EntryPoint>] let main argv = use excel = create "C:/work/sample.xlsx"
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
既存Workbookを開く
open' (filepath: string): IExcelApplication
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx"
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
Workbookを名前を付けて保存する
saveAs (filepath: string) (excelObject: ^ExcelObject): unit
[<EntryPoint>] let main argv = // Workbookを新規作成し, ExcelApplicationを取得. use excel = create() // (1) Workbookを利用して, 名前を付けて保存. let book = excel |> workbook(1) // do somethings book |> saveAs "C:/work/sample.xlsx" // (2) Worksheetを利用して, 名前を付けて保存. let sheet = excel |> workbook(1) |> worksheet(1) // do somethings sheet |> saveAs "C:/work/sample.xlsx"
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
Workbookを上書き保存する
save (excelObject: ^ExcelObject): unit
[<EntryPoint>] let main argv = // 既存のExcelワークブックを開く. use excel = open' "C:/work/sample.xlsx" // (1) Workbookを利用して, 上書き保存. let book = excel |> workbook(1) // do somethings book |> save // (2) Worksheetを利用して, 上書き保存. let sheet = excel |> workbook(1) |> worksheet(1) // do somethings sheet |> save
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
Workbookを取得する
workbook (index: int) (excel: IExcelApplication): IWorkbook
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" // indexを指定してWorkbookを取得. // -> index は 1 始まりであることに注意する. let book = excel |> workbook(1)
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
Worksheetを取得する
worksheet (index: int | string) (workbook: IWrokbook): IWorksheet
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" // (1) indexを指定してWorksheetを取得. // -> index は 1 始まりであることに注意する. let sheet = excel |> workbook(1) |> worksheet(1) // (2) sheet nameを指定して取得. let sheet = excel |> workbook(1) |> worksheet("Sheet1")
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
Worksheetを新規追加する
newsheet (book: IWorkbook): IWorksheet
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> newsheet
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
IExcelRangeオブジェクトを取得する
sheet.[address]: IExcelRange
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // (1) アドレス形式で取得. let cell = sheet.["A1"] // (2) R1C1形式で取得. let cell = sheet.[1, 1] // (3) 範囲をアドレス形式で取得. let range = sheet.["A1:B3"] // (4) 範囲を始点セルアドレスと終点セルアドレスを指定して取得. let range = sheet.["A1", "B3"]
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
IWorksheetから行を取得する
get'row (index: int) (sheet: IWorksheet): IExcelRow
get'rows (begin': int, end': int) (sheet: IWorksheet): IExcelRows
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 1行取得. let r = sheet |> get'row(1) // $1:$1 // 複数行取得. let r = sheet |> get'rows(1, 3) // $1:$3
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
IWorksheetから列を取得する
get'column (index: int) (sheet: IWorksheet): IExcelRow
get'columns (begin': int, end': int) (sheet: IWorksheet): IExcelRows
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 1列取得. let c = sheet |> get'column(1) // $A:$A // 複数列取得. let c = sheet |> get'columns(1, 3) // $A:$C
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
IExcelRangeの行全体を取得する
current'rows (range: IExcelRange): IExcelRows
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 行全体を取得. let r = sheet.["A1"] |> current'rows // $1:$1 let r = sheet.["A1:B3"] |> current'rows // $1:$3
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
IExcelRangeの列全体を取得する
current'columns (range: IExcelRange): IExcelColumns
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 列全体を取得. let r = sheet.["A1"] |> current'columns // $A:$A let r = sheet.["A1:B3"] |> current'columns // $A:$B
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
範囲データを行ごとに列挙する
rows (range: IExcelRange): seq<IExcelRow>
rowsi (range: IExcelRange): seq<int * IExcelRow>
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // rows関数を利用して, 1行ずつ取得. for row in sheet.["A1:B3"] |> rows do // 各Cell毎に何か処理. for cell in row do // do somethings // rowsi関数を利用して, インデックス付きで1行ずつ取得. // -> index は 1 始まりであることに注意. for (index, row) in sheet.["A1:B3"] |> rowsi do // 各Cell毎に何か処理. for cell in row do // do somethings
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
範囲データを列ごとに列挙する
columns (range: IExcelRange): seq<IExcelColumn>
columnsi (range: IExcelRange): seq<int * IExcelColumn>
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // columns関数を利用して, 1行ずつ取得. for column in sheet.["A1:B3"] |> columns do // 各Cell毎に何か処理. for cell in column do // do somethings // columnsi関数を利用して, インデックス付きで1行ずつ取得. // -> index は 1 始まりであることに注意. for (index, column) in sheet.["A1:B3"] |> columnsi do // 各Cell毎に何か処理. for cell in column do // do somethings
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
値を取得する
get (cell: IExcelRange): obj
get<'T> (cell: IExcelRange): 'T
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 値を取得. let v: obj = sheet.["A1"] |> get // 値を型付きで取得. // -> 指定した型と互換性がない場合, System.InvalidCastException. let v: int = sheet.["A1"] |> get<int> // 複数要素がある場合は先頭要素の値のみ取得. // -> 以下の場合 sheet.["A1"] の値が得られる. let v: obj = sheet.["A1:B3"] |> get // 複数要素がある場合は先頭要素の型付きの値のみ取得. // -> 以下の場合 sheet.["A1"] の値が得られる. // -> 指定した型と互換性がない場合, System.InvalidCastException. let v: int = sheet.["A1:B3"] |> get<int>
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
値を配列データで取得する
gets (range: IExcelRange): obj [,]
gets<'T> (range: IExcelRange): 'T [,]
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 値を配列データとして取得. let vs: obj [,] = sheet.["A1:A3"] |> gets // 値を型付きの配列データとして取得. // -> 指定した型と互換性がない場合, System.InvalidCastException. let vs: int [,] = sheet.["A1:A3"] |> gets<int> // 取得した配列データから先頭要素の値を取得. // -> 以下の場合 sheet.["A1"] の値が得られる. let v: obj = sheet.["A1:B3"] |> gets |> head // 取得した型付きの配列データから先頭要素の値を取得. // -> 以下の場合 sheet.["A1"] の値が得られる. // -> 指定した型と互換性がない場合, System.InvalidCastException. let v: int = sheet.["A1:B3"] |> gets<int> |> head // 取得した配列データから末尾要素の値を取得. // -> 以下の場合 sheet.["B3"] の値が得られる. let v: obj = sheet.["A1:B3"] |> gets |> last // 取得した型付きの配列データから末尾要素の値を取得. // -> 以下の場合 sheet.["B3"] の値が得られる. // -> 指定した型と互換性がない場合, System.InvalidCastException. let v: int = sheet.["A1:B3"] |> gets<int> |> last
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
関数を取得する
getfx (cell: IExcelRange): string
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 関数を取得. let fn: string = sheet.["A1"] |> getfx
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
関数を配列データで取得する
getsfx (range: IExcelRange): string [,]
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 関数を配列データで取得. let fns: string [,] = sheet.["A1:A3"] |> getsfx
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
値を設定する
set (value: obj) (target: IExcelRange): unit
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 値を設定. sheet.["A1"] |> set 100 sheet.["A1:B3"] |> set 100
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
関数を設定する
fx (func: string) (target: IExcelRange): unit
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 関数を設定. sheet.["A1"] |> fx "SUM(A2:A5)" sheet.["A1:B3"] |> fx "COUNT(A1:B3)"
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
背景色を設定する
bgcolor (color: Color) (target: IExcelRange): unit
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 背景色を設定. sheet.["A1"] |> bgcolor Color.Red sheet.["B1:B3"] |> bgcolor Color.Blue sheet.["C1"] |> bgcolor (rgb(0, 128, 255))
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
背景パターンを設定する
bgpattern (pattern: Pattern) (target: IExcelRange): unit
arg name
values
pattern
pattern'none
pattern'auto
pattern'up
pattern'down
pattern'vertical
pattern'horizontal
pattern'lightUp
pattern'lightDown
pattern'lightVertical
pattern'lightHorizontal
pattern'gray8
pattern'gray16
pattern'gray25
pattern'gray50
pattern'gray75
pattern'semigray75
pattern'solid
pattern'checker
pattern'grid
pattern'crisscross
pattern'linearGradient
pattern'rectangularGradient
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 背景パターンを設定. sheet.["A1"] |> bgpattern pattern'checker sheet.["B1:B3"] |> bgpattern pattern'crisscross
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
罫線を設定する
ruledline (target: IExcelRange): IBorders
コンピュテーション式
![bookmark_tabs](https://github.githubassets.com/images/icons/emoji/unicode/1f4d1.png)
ruledline
で利用できるカスタムオペレーション
operation name
description
top (border)
最上部の横罫線.
bottom (border)
最下部の横罫線.
left (border)
最左部の縦罫線.
right (border)
最右部の縦罫線.
horizontal (border)
中間部の横罫線.
vertical (border)
中間部の縦罫線.
growing (border)
左下から右上に向けての罫線. 色や太さの設定は falling
と共有.
falling (border)
左上から右下に向けての罫線. 色や太さの設定は growing
と共有.
![bookmark_tabs](https://github.githubassets.com/images/icons/emoji/unicode/1f4d1.png)
Border
に設定できる値
operation name
description
values
LineStyle
罫線のスタイル.default:
linestyle'continuous
linestyle'none
linestyle'dot
linestyle'double
linestyle'dash
linestyle'continuous
linestyle'dashdot
linestyle'dashdotdot
linestyle'slant
Weight
罫線の太さ.default:
weight'medium
weight'medium
weight'hairline
weight'thin
weight'thick
Color
罫線の色.default:
Color.Black
Color.Red
Color.Orange
Color.Blue
rgb(r, g, b)
and more...
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 罫線を設定. ruledline sheet.["B2:C5"] { // 各 Border の値は with を利用して指定. top { border with Color= Color.Red } left { border with Color= Color.Orange; Weight= weight'thick } right { border with LineStyle= linestyle'dashdot } bottom { border with Weight= weight'medium } horizontal { border with Color= Color.Blue; Weight= weight'medium } vertical { border with Color= rgb (0, 128, 255); Weight= weight'thin } // growing と falling は値がExcel内部で共有されているため、設定値は後勝ちする. growing { border with Weight= weight'hairline } falling { border with Weight= weight'thick } } |> ignore
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
フォントを設定する / font (target: IExcelRange): IRangeFont
コンピュテーション式
![bookmark_tabs](https://github.githubassets.com/images/icons/emoji/unicode/1f4d1.png)
font
で利用できるカスタムオペレーション
operation name
description
values
set (fontName: string)
name (fontName: string)
フォント名.
游ゴシック
メイリオ
consolas
and more...
set (size: float)
size (size: float)
フォントサイズ.
8.0
10.5
24.0
and more...
set (style: FontStyle)
フォントスタイル. Flags
なので複数まとめて指定可能.
style'normal
style'bold
style'italic'
style'shadow
style'strikethrough
style'subscript
style'superscript
style'singleUnderline
style'doubleUnderline
set (value: Color)
フォント色.
Color.Red
Color.Orange
Color.Blue
and more...
set (value: RGB)
フォント色.
rgb(r: int, g: int, b: int)
{ r= 0; g= 128; b= 255; }
set (style: Underline)
下線.
underline'none
underline'double
underline'doubleAccounting
underline'single
underline'singleAccounting
bold (on: bool)
太字.
true
or false
italic (on: bool)
イタリック体.
true
or false
shadow (on: bool)
フォント影.
true
or false
outline (on: bool)
アウトラインフォント.
true
or false
strikethrough (on: bool)
打ち消し線.
true
or false
subscript (on: bool)
下付き文字にする.
true
or false
superscript (on: bool)
上付き文字にする.
true
or false
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // フォントを設定. font sheet.["A1:A3"] { // フォントの指定. set "メイリオ" // or name "メイリオ" // フォントサイズの設定. set 16.0 // or size 16.0 // 下線の設定. set underline'double // フォント色の設定. set Color.Orange // or set ( rgb(0, 128, 255) ) // or set { r= 0; g= 128; b= 255; } // フォントスタイルの設定. set style'normal // スタイルを複数選択する場合は以下のように指定. set (style'normal ||| style'strikethrough ||| style'shadow) // style を直接指定しなくとも各種スタイルをひとつずつ ON/OFF 可能. bold true italic true shadow true outline true strikethrough true subscript true superscript true } |> ignore
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
IExcelApplicationオブジェクトを操作する
excel'op (excel: IExcelApplication)
コンピュテーション式
![bookmark_tabs](https://github.githubassets.com/images/icons/emoji/unicode/1f4d1.png)
excel'op
で利用できるカスタムオペレーション
operation name
description
values
set (mode: Calculation)
Excelの再計算制御を設定する.default:
calc'manual
calc'auto
calc'manual
calc'semiauto
set (visibility: AppVisibility)
Excelの表示状態を設定する.default:
visibility'hidden
visibility'visible
visibility'hidden
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" excel'op excel { set calc'semiauto set visibility'visible }
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
IExcelRangeオブジェクトを操作する(コピー・ペースト・挿入・削除など)
cell'op
コンピュテーション式
![bookmark_tabs](https://github.githubassets.com/images/icons/emoji/unicode/1f4d1.png)
cell'op
で利用できるカスタムオペレーション
operation name
description
values
copy (target: IExcelRange)
対象をクリップボードにコピーする.
-
paste (target: IExcelRange, pasteMode: PasteMode)
対象にクリップボードの値を貼り付ける.
-
insert (target: IExcelRange, insertMode: InsertMode)
対象にクリップボードの値を挿入する.
-
delete (target: IExcelRange, deleteMode: DeleteMode)
対象を削除する.
-
set (target: IExcelRange, value: obj)
対象に値を設定する.
-
set (target: IExcelRange, color: Color)
対象の背景色を設定する.
Color.Red
Color.Orange
Color.Blue
and more...
set (target: IExcelRange, theme: ThemeColor)
対象の背景色をテーマカラーで設定する.
theme'background1
theme'background2
theme'foreground1
theme'foreground2
theme'accent1
theme'accent2
theme'accent3
theme'accent4
theme'accent5
theme'accent6
set (target: IExcelRange, pattern: Pattern)
対象の背景パターンを設定する.
pattern'none
pattern'auto
pattern'up
pattern'down
pattern'vertical
pattern'horizontal
pattern'lightUp
pattern'lightDown
pattern'lightVertical
pattern'lightHorizontal
pattern'gray8
pattern'gray16
pattern'gray25
pattern'gray50
pattern'gray75
pattern'semigray75
pattern'solid
pattern'checker
pattern'grid
pattern'crisscross
pattern'linearGradient
pattern'rectangularGradient
set (target: IExcelRange, halign: HorizontalAlignment)
文字の水平位置を設定する.
h'right
h'left
h'center
h'justify
h'distributed
h'general
h'fill
h'centerAcrossSelection
set (target: IExcelRange, valign: VerticalAlignment)
文字の垂直位置を設定する.
v'top
v'bottom
v'center
v'justify
v'distributed
fx (target: IExcelRange, formula: string)
対象に関数を設定する.
-
width (target: IExcelRange, length: int)
対象の列幅をpt単位で設定する.
-
height (target: IExcelRange, length: int)
対象の行高をpt単位で設定する.
-
fit'width (target: IExcelRange)
対象の列幅を自動調整する.
-
fit'height (target: IExcelRange)
対象の行高を自動調整する.
-
merge (target: IExcelRange, across: bool)
セルを結合する.
true
: 範囲内のセルを行ごとに結合.false
: 範囲内すべてのセルを1つに結合.
unmerge (target: IExcelRange)
セルの結合を解除する.
-
wrap (target: IExcelRange)
折り返して全体を表示する.
-
unwrap (target: IExcelRange)
折り返して全体を表示を解除する.
-
shrink (target: IExcelRange)
縮小して全体を表示する.
-
unshrink (target: IExcelRange)
縮小して全体を表示を解除する.
-
orientation (target: IExcelRange, angle: int)
文字の方向を設定する.
-90° ~ 90°
format (target: IExcelRange, format: string)
セルの値の表示形式を設定する.
-
![bookmark_tabs](https://github.githubassets.com/images/icons/emoji/unicode/1f4d1.png)
PasteMode
の要素
name
description
values
Paste
貼り付け方式.default:
paste'all
paste'values
paste'comments
paste'formulas
paste'formats
paste'all
paste'validation
paste'exceptBorders
paste'colmnWidths
paste'formulasAndNumberFormats
paste'valuesAndNumberFormats
paste'allUsingSourceTheme
paste'allMergingConditionalFormats
Op
貼り付け時の演算方法.default:
op'none
op'none
op'add
op'sub
op'mul
op'div
SkipBlanks
空白セルを無視するか.default:
false
true
or false
Transpose
行列を入れ替えるか.default:
false
true
or false
![bookmark_tabs](https://github.githubassets.com/images/icons/emoji/unicode/1f4d1.png)
InsertMode
の要素
name
description
values
Shift
挿入後に他のセルをどうシフト移動するか.default:
shift'down
shift'right
shift'down
Origin
書式をコピーしてくる方向.default:
origin'right
, origin'below
origin'left
origin'above
origin'right
origin'below
![bookmark_tabs](https://github.githubassets.com/images/icons/emoji/unicode/1f4d1.png)
DeleteMode
の要素
name
description
values
Shift
削除後に他のセルをどうシフト移動するか.
shift'left
shift'up
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) cell'op { // A1 をクリップボードにコピー. copy sheet.["A1"] // 範囲コピーも可能. copy sheet.["A1:A3"] // クリップボードのデータを B1 に貼り付け. paste sheet.["B1"] paste'mode paste sheet.["B1"] { paste'mode with Paste= paste'values } paste sheet.["B1"] { paste'mode with Op= op'add } paste sheet.["B1"] { paste'mode with SkipBlanks= true } paste sheet.["B1"] { paste'mode with Transpose= true } paste sheet.["B1"] { paste'mode with Paste= paste'values; SkipBlanks= true } paste sheet.["B1"] { paste'mode with Paste= paste'values; Transpose= true } // 範囲貼り付けも可能. paste sheet.["B1:B3"] paste'mode // クリップボードのデータを C1 に挿入. insert sheet.["C1"] insert'mode insert sheet.["C1"] { insert'mode with Shift= shift'down } insert sheet.["C1"] { insert'mode with Origin= origin'below } insert sheet.["C1"] { insert'mode with Shift= shift'right; Origin= origin'below } // 範囲挿入も可能. insert sheet.["C1:C3"] insert'mode // A1 のデータを削除する. delete sheet.["A1"] delete'mode delete sheet.["A1"] { delete'mode with Shift= shift'up } // 範囲削除も可能. delete sheet.["A1:A3"] delete'mode // A1 に値を設定. set sheet.["A1"] 100 set sheet.["A1"] sheet.["B1"] // A1 に関数を設定. fx sheet.["A1"] "SUM(A1:B3)" fx sheet.["A1"] sheet.["B1"] // 列幅を 100 に設定. width sheet.["A1"] 100 width sheet.["A1:B3"] 100 // 行高を 100 に設定. height sheet.["A1"] 100 height sheet.["A1:B3"] 100 // 列幅を自動調整. fit'width sheet.["A1"] fit'width sheet.["A1:B3"] // 行高を自動調整. fit'height sheet.["A1"] fit'height sheet.["A1:B3"] // 背景色を設定. set sheet.["A1"] Color.Blue // 背景色をテーマカラーで設定. set sheet.["A1"] theme'accent1 // 背景パターンを設定. set sheet.["A1"] pattern'horizontal // セルを結合. merge sheet.["C1:D3"] true merge sheet.["E1:F3"] false // セルの結合を解除. unmerge sheet.["C1:D3"] unmerge sheet.["E1:F3"] // 折り返して全体を表示. wrap sheet.["A1"] // 折り返して全体を表示を解除. unwrap sheet.["A1"] // 縮小して全体を表示. shrink sheet.["A1"] // 縮小して全体を表示を解除. unshrink sheet.["A1"] // 文字の方向を設定. orientation sheet.["A1"] -90 orientation sheet.["A1"] 0 orientation sheet.["A1"] 90 // 表示形式を設定. format sheet.["A1"] "(日付)yyyy-MM-dd" }
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
IExcelRangeを削除する
delete (direction: DeleteShiftDirection) (target: IExcelRange): unit
![bookmark_tabs](https://github.githubassets.com/images/icons/emoji/unicode/1f4d1.png)
DeleteShiftDirection
value
description
shift'left
削除後, 左方向へシフト.
shift'up
削除後, 上方向へシフト.
[<EntryPoint>] let main argv = use excel = open' "C:/work/sample.xlsx" let sheet = excel |> workbook(1) |> worksheet(1) // 対象を削除. sheet.["A1"] |> delete shift'up sheet.["A1:A3"] |> delete shift'left
Utility
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
数値をカラム名に変換する
column'name (index: int): string
let name = 1 |> column'name // A let name = 10 |> column'name // J let name = 128 |> column'name // DX
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
カラム名をインデックスに変換する
column'number (column: string): int
let number = "A" |> column'number // 1 let number = "J" |> column'number // 10 let number = "DX" |> column'number // 128
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
IExcelRangeからアドレスを取得する
address (target: IExcelRange): string
let adds = sheet.["A1"] |> address // $A$1 let adds = sheet.["A1:B3"] |> address // $A$1:$B$3
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
ExcelObjectを選択する
activate (target: ^T): unit
// Workbookを選択状態にする. excel |> workbook(1) |> activate // Worksheetを選択状態にする. excel |> workbook(1) |> worksheet(1) |> activate // Cellを選択状態にする. sheet.["B1"] |> activate sheet.["A1:B3"] |> activate
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
ExcelObjectを選択する
select (target: ^T): unit
// Worksheet(1)を選択状態にする. excel |> workbook(1) |> worksheet(1) |> select // Cellを選択状態にする. sheet.["B1"] |> select sheet.["D1:E3"] |> select
TIPS
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
![white_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fb.png)
try-with
の利用
例外処理を施していない場合 Excel COM オブジェクトが適切に解放されず, プロセス上に残ってしまう恐れがあります.try-with
(または try-with-finally
) と use
を併用することで Excel COM オブジェクトの解放漏れを防げます.
try // use を利用する. use excel = create () // do somethings with _ -> ()
また, F# Interactive で利用する場合, attach
したあとは必ず detach
する必要があります.
let ps = enumerate ();; let excel = ps.[0] |> attach;; # do somethings excel |> detach;;
![black_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fc.png)
![white_medium_square](https://github.githubassets.com/images/icons/emoji/unicode/25fb.png)
LangVersion
の指定
.NET 5 を利用している場合, うまくコンピュテーション式が動作しない可能性があります.
その場合は LangVersion
に preview を指定するようにしてください.
<!-- .fsproj を書き換える場合 --> <PropertyGroup> <LangVersion>preview</LangVersion> </PropertyGroup>
# dotnet fsi コマンドにオプションを指定する場合 dotnet fsi --langversion:preview
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK