Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
hirobe
March 08, 2024
Programming
0
3k
PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話
PHPerKaigi2024 LT登壇資料です
hirobe
March 08, 2024
Tweet
Share
More Decks by hirobe
See All by hirobe
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
1.7k
PHP8.1で、リソースがオブジェクトに!? マイナーリリースの変更が レガシープロダクトに与えた影響
hirobe1999
0
1.9k
フレームワークが存在しない時代からのレガシープロダクトを、 Laravelに”載せる”実装戦略
hirobe1999
0
1.8k
フレームワークが存在しない時代からのレガシープロダクトを、 Laravelに”載せる”実装戦略
hirobe1999
0
2.1k
新卒PHPer奮闘記 ~配属されたのは3歳違いのプロダクト!?~ / phperkaigi-2022-lt
hirobe1999
0
1.8k
Other Decks in Programming
See All in Programming
AI駆動開発の本音 〜Claude Code並列開発で見えたエンジニアの新しい役割〜
hisuzuya
4
500
grapheme_strrev関数が採択されました(あと雑感)
youkidearitai
PRO
1
210
maplibre-gl-layers - 地図に移動体たくさん表示したい
kekyo
PRO
0
260
AIコードレビューの導入・運用と AI駆動開発における「AI4QA」の取り組みについて
hagevvashi
0
450
ふつうのRubyist、ちいさなデバイス、大きな一年 / Ordinary Rubyists, Tiny Devices, Big Year
chobishiba
1
440
new(1.26) ← これすき / kamakura.go #8
utgwkk
0
2.3k
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
1.1k
How to stabilize UI tests using XCTest
akkeylab
0
120
モジュラモノリスにおける境界をGoのinternalパッケージで守る
magavel
0
3.5k
Windows on Ryzen and I
seosoft
0
270
GC言語のWasm化とComponent Modelサポートの実践と課題 - Scalaの場合
tanishiking
0
110
技術検証結果の整理と解析をAIに任せよう!
keisukeikeda
0
120
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Scaling GitHub
holman
464
140k
Side Projects
sachag
455
43k
The Cost Of JavaScript in 2023
addyosmani
55
9.8k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
150
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
WENDY [Excerpt]
tessaabrams
9
36k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
320
The SEO Collaboration Effect
kristinabergwall1
0
390
Transcript
© RAKUS Co., Ltd. 1 PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話 #phperkaigi
2024/03/09 廣部知⽣
2 ⾃⼰紹介 21卒で株式会社ラクスに⼊社 PHPでMail Dealerの開発を⾏っている 『良いコード悪いコードで学ぶ設計⼊⾨』や 『ちょうぜつソフトウェア設計⼊⾨』で 設計を学ぶ⽇々…… スト6で戦いの螺旋を登り続けています (Act2はMR1700でフィニッシュ)
© RAKUS Co., Ltd. 3 PHPでOfficeファイル取り扱いたいな〜
© RAKUS Co., Ltd. 4 皆さん、思ったこと、ありますよね?
© RAKUS Co., Ltd. 5 あるある〜!
© RAKUS Co., Ltd. 6 できます!
7 PHPでOfficeを取り扱うためのライブラリ群 主に以下の3つがある • PhpSpreadsheet • PHPWord • PHPPresentation https://github.com/PHPOffice
PHPOffice
8 スプレッドシートの新規作成はもちろん PhpSpreadsheet $spreadsheet = new Spreadsheet(); $activeWorksheet = $spreadsheet->getActiveSheet();
$activeWorksheet->setCellValue('A1', 'Hello World !'); $writer = new Xlsx($spreadsheet); $writer->save('hello world.xlsx'); 参考:https://phpspreadsheet.readthedocs.io/en/latest/
9 スプレッドシートの読み込みもできる PhpSpreadsheet $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx"); $spreadsheet = $reader->load("05featuredemo.xlsx"); 参考:https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/
10 Wordファイルの新規作成 PHPWord $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection();
$section->addText('Hello World!'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('helloWorld.docx'); 参考:https://phpoffice.github.io/PHPWord/usage/introduction.html
11 Wordファイルの読み込み PHPWord $reader = IOFactory::createReader('Word2007'); $reader->load(__DIR__ . '/sample.docx'); $reader
= IOFactory::createReader('MsDoc'); $reader->load(__DIR__ . '/sample.doc'); 参考:https://phpoffice.github.io/PHPWord/usage/readers.html
12 rtfやodtもサポートしている PHPWord $reader = IOFactory::createReader('RTF'); $reader->load(__DIR__ . '/sample.rtf'); $reader
= IOFactory::createReader('ODText'); $reader->load(__DIR__ . '/sample.odt'); 参考:https://phpoffice.github.io/PHPWord/usage/readers.html
13 書式の指定もできます PHPWord $fontStyleName = 'oneUserDefinedStyle'; $phpWord->addFontStyle( $fontStyleName, array('name' =>
'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true) ); 参考:https://phpoffice.github.io/PHPWord/usage/introduction.html
14 pptxファイルの新規作成 PHPPresentation $presentation = new PhpPresentation(); $writerPPTX = IOFactory::createWriter($presentation,
'PowerPoint2007'); $writerPPTX->save(__DIR__ . '/sample.pptx'); 参考:https://phpoffice.github.io/PHPPresentation/usage/presentation.html
15 pptファイルの読み込み PHPPresentation $reader = IOFactory::createReader('PowerPoint2007'); $reader->load(__DIR__ . '/sample.pptx'); $reader
= IOFactory::createReader('PowerPoint97'); $reader->load(__DIR__ . '/sample.ppt'); 参考:https://phpoffice.github.io/PHPPresentation/usage/readers.html
16 ⽂字や画像の位置指定が、ちょっと⾯倒? PHPPresentation $shape = $currentSlide->createDrawingShape(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo')
->setPath('./resources/logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); 参考:https://phpoffice.github.io/PHPPresentation/usage/presentation.html
© RAKUS Co., Ltd. 17 地球上の誰かがふと思った PHPでOfficeファイルを 扱えるようになったら どんな機能が実装できるだろうか……
18 について メール共有管理システム 15年連続シェアNo.1(2009〜2023)※ 歴史は更に⻑く、2001年4⽉に販売開始 Laravelは2011年リリースなので、10歳年上 ※出典:ITR「ITR Market View:メール∕Web∕SNSマーケティング市場2024」
メール処理市場:ベンダー別売上⾦額推移およびシェア(2009〜2023年度予測)
19 PHPOfficeで読み込んだファイルを⾃然⾔語処理で解析して 宛先を抽出する メール本⽂、添付ファイル名、添付ファイル内の宛先を 突き合わせて、違うものがあったら警告を出す機能 (write系の機能は使わず、readのみの利⽤) 誤添付防⽌機能を実装しました
20 実際の動作はこんな感じ
21 メモリ枯渇との戦いが多かった! 外部ライブラリなので、Officeファイルをどう解析して どういう形でメモリ内に保持するのかブラックボックス →メモリ枯渇のエラーハンドリングを⾏い 発⽣時に随時対応する以外思いつきませんでした 苦労したところ
22 PhpSpreadsheet、超メモリ容量⾷います > PhpSpreadsheet uses an average of about 1k
per cell (1.6k on 64-bit PHP) in your worksheets 1セルごとに1kbのメモリを使⽤する →10000(100x100)セルで10MB使⽤する計算 とりあえず、読み込み範囲の制限をかけて対策 参考:https://phpspreadsheet.readthedocs.io/en/latest/topics/memory_saving/ 苦労したところ
23 社内サーバリリース後、メモリ枯渇発⽣の報告が →添付ファイルを⾒ても、そんなにセル数多くない…… あれこれ検証した結果、vlookupが原因だと判明 ⾮表⽰シートに対し、⾮常に広い範囲で検索をかけていた →関数の解析‧展開で、メモリ枯渇が発⽣していた! 関数を展開しないオプションがあったので そちらを適⽤することで解決 苦労したところ
24 他にも、空⽩のセルは無視するオプションや 1シートずつ読み込んで、適宜メモリ解放を⾏う等の 対策を⾏った 公式ドキュメントには、PSR-16が実装されたライブラリを 利⽤し、代替オブジェクト(APCu、Redist、Memcache) を利⽤する⽅法も紹介されているが、⾮採⽤ 参考:https://phpspreadsheet.readthedocs.io/en/latest/topics/memory_saving/ 苦労したところ
25 PowerPointもメモリ枯渇発⽣するときがある 特にドキュメントに記載はないが、画像ファイルが多い プレゼンテーションはメモリ枯渇が発⽣しやすい こちらは特に対策が記載されていない…… →諦めて、メモリ枯渇時は解析を実⾏しないように 苦労したところ
26 当然メモリ枯渇以外にも苦労はあり…… 神Excelがこの世に存在するように、どんなフォーマットの 添付ファイルが添付されるか予測がし⾟い 宛先を抽出する際「様」や「御中」などの敬称を 参考にするのだが、別セルに書かれていたり、同じセル に書かれていたりと形式が曖昧 セル結合も使われていたりすると、さらに複雑に 😱 苦労したところ(時間があれば)
© RAKUS Co., Ltd. 27 (いろいろ課題はありますが……) PHPで楽しい Officeライフを!