LoginSignup
1
2

More than 3 years have passed since last update.

Excelの表をクリップボードにコピーしたときのHTMLの内容を調べてみた

Last updated at Posted at 2021-01-05

まえがき

Excelの表を取り込んで、AsciiDocフォーマットに変換(※)しようと思い、とっかかりとして、Excelの表をクリップボードにコピーして得られるHTMLのフォーマットを調べてみた。

※:できた
ExcelとかWordでクリップボードにコピーしたデータからAsciiDocの表に変換するツールをつくってみた(結合セル対応) - Qiita

1. 実際の例

1.1. コピーした部分

image.png

1.2. 自作ツールでHTMLに変換した結果


Version:1.0
StartHTML:0000000105
EndHTML:0000002603
StartFragment:0000002174
EndFragment:0000002551

<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 15">
<link id=Main-File rel=Main-File
href="file:///C:/Users/XXXXX/AppData/Local/Temp/msohtmlclip1/01/clip.htm">
<link rel=File-List
href="file:///C:/Users/XXXXX/AppData/Local/Temp/msohtmlclip1/01/clip_filelist.xml">
<style>
<!--table
    {mso-displayed-decimal-separator:"\.";
    mso-displayed-thousand-separator:"\,";}
@page
    {margin:.75in .7in .75in .7in;
    mso-header-margin:.3in;
    mso-footer-margin:.3in;}
.font5
    {color:windowtext;
    font-size:6.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:游ゴシック, monospace;
    mso-font-charset:128;}
tr
    {mso-height-source:auto;
    mso-ruby-visibility:none;}
col
    {mso-width-source:auto;
    mso-ruby-visibility:none;}
br
    {mso-data-placement:same-cell;}
td
    {padding-top:1px;
    padding-right:1px;
    padding-left:1px;
    mso-ignore:padding;
    color:black;
    font-size:11.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:游ゴシック, monospace;
    mso-font-charset:128;
    mso-number-format:General;
    text-align:general;
    vertical-align:middle;
    border:none;
    mso-background-source:auto;
    mso-pattern:auto;
    mso-protection:locked visible;
    white-space:nowrap;
    mso-rotate:0;}
.xl65
    {text-align:center;}
.xl66
    {text-align:right;}
ruby
    {ruby-align:left;}
rt
    {color:windowtext;
    font-size:6.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:游ゴシック, monospace;
    mso-font-charset:128;
    mso-char-type:katakana;
    display:none;}
-->
</style>
</head>

<body link="#0563C1" vlink="#954F72">

<table border=0 cellpadding=0 cellspacing=0 width=138 style='border-collapse:
 collapse;width:104pt'>
<!--StartFragment-->
 <col width=69 span=2 style='width:52pt'>
 <tr height=49 style='mso-height-source:userset;height:36.5pt'>
  <td rowspan=2 height=98 class=xl65 width=69 style='height:73.0pt;width:52pt'>a</td>
  <td width=69 style='width:52pt'>b</td>
 </tr>
 <tr height=49 style='mso-height-source:userset;height:36.5pt'>
  <td height=49 class=xl66 style='height:36.5pt'>c</td>
 </tr>
<!--EndFragment-->
</table>

</body>

</html>

2.1. ヘッダ情報

2.1.1. StartHTML と EndHTML

参考サイト1(Microsoft)より:

StartHTML: bytecount from the beginning of the clipboard to the start of the context, or -1 if no context.
EndHTML: bytecount from the beginning of the clipboard to the end of the context, or -1 if no context.

抜粋

StartHTML:0000000105
EndHTML:0000002603

105byteと2603byteは下記の位置にあたる。

image.png

image.png

ちなみに Versionの説明は下記。クリップボード自体のバージョン?

Version: vv version number of the clipboard. Starting version is 0.9.

2.1.2. StartFragment と EndFragment

StartFragment: bytecount from the beginning of the clipboard to the start of the fragment.
EndFragment: bytecount from the beginning of the clipboard to the end of the fragment.
(中略)
The fragment should be preceded and followed by the HTML comments and (no space allowed between the !-- and the text) to conveniently indicate where the fragment starts and ends. Thus the start and end of the fragment are indicated by the presence of these comments and by StartFragment and EndFragment byte counts in the description.

下記のように、byte位置StartFragment~EndFragmentのデータ領域には、コメント部分は含まれないよう。

抜粋

StartFragment:0000002174
EndFragment:0000002551

image.png

image.png

2.2. 文字コード

UTF-8固定のよう。

The only character set supported by the clipboard is Unicode in its UTF-8 encoding.

3. 表の中身

書式情報を拾いたいが、Fragment内で完結しない。。

抜粋

<table border=0 cellpadding=0 cellspacing=0 width=138 style='border-collapse:
 collapse;width:104pt'>
<!--StartFragment-->
 <col width=69 span=2 style='width:52pt'>
 <tr height=49 style='mso-height-source:userset;height:36.5pt'>
  <td rowspan=2 height=98 class=xl65 width=69 style='height:73.0pt;width:52pt'>a</td>
  <td width=69 style='width:52pt'>b</td>
 </tr>
 <tr height=49 style='mso-height-source:userset;height:36.5pt'>
  <td height=49 class=xl66 style='height:36.5pt'>c</td>
 </tr>
<!--EndFragment-->
</table>
  • <table>タグがFragmentの外にあり、罫線情報を拾うのが面倒くさい。
  • アライメント情報(右揃えとか)が、スタイルシートで指定されており、同じくFragmentの外にあり、拾うのが面倒くさい。(<td height=49 class=xl66 style='height:36.5pt'>c</td>の部分)
  • セル結合情報は rowspan=2 とかが含まれているので、想定通り。

ちなみにWordで(表をつくって)試したところ、(Excelとは異なり、)Fragmentのなかに<table>タグがありました。

4. 所感(?)

所感というか、ひとりごとです。

面倒くさそう。
COMでExcel操作するのとどっちが楽か、応用が利くか、ツールとして使いやすいか。。あとはExcelのバージョンが上がったときにどっちが影響を受けにくいか。。
考えても解がなさそうなのでまずは今回調べた内容でやってみるかな。

参考サイト

  1. HTML Clipboard Format - Win32 apps | Microsoft Docs
  2. HTMLクリップボードのデータフォーマット(VC++)
  3. Microsoft TeamsのチャットログのテキストをクリップボードにコピーしてHTML Format形式の文字列として読み取ろうとしたら文字化けした件 - Qiita
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2