将KDB q查询结果表转换为HTML表格以用于电子邮件

huangapple go评论62阅读模式
英文:

Convert a KDB q query result table to a html table for email

问题

我有一个通过kdb q查询得到的表格,我想将它转换成HTML表格并将其发送到电子邮件的正文中。
tab:([]A:1;B:2;C:3)

上述kdb q表格,类型为98h,需要转换成HTML表格,如下所示:

A B C
1 2 3

然后,可以使用sendmail功能将上述表格发送到电子邮件正文中。

英文:

I have a table resulting from a kdb q query and i want to convert it into a html table and send it the body of the email.
tab:([]A:1;B:2;C:3)

the above kdb q table of type 98h has to be converted into html table like this

A B C
1 2 3

and send the above table in a email body using sendmail functionality

答案1

得分: 2

这部分代码是用于创建HTML表格的。请注意,这些代码的目的是生成HTML表格,所以不需要翻译其中的代码部分。以下是这些代码的翻译结果:

toHtmlTable: { [p_table]
    headerRow: .h.htc[`tr] raze .h.htc[`th;]'[string cols p_table];
    dataRows: raze {.h.htc[`tr;] raze[{.h.htc[`td; $[10h=type x; x; string x]] }'[value x]]}'[p_table];
    : .h.htc[`table; headerRow , dataRows];
};

toFormattedHtmlTable: { [p_table]
    head: "<head> <style>  table, th, td {font-size:13pt; border:1px solid #D6D9DC; border-collapse:collapse; text-align:left;} th, td {padding: 5px;} </style> </head>";
    headerRow: .h.htc[`tr] raze .h.htc[`th;]&#39;[string cols p_table];
    dataRows: raze {.h.htc[`tr;] raze[{.h.htc[`td; $[10h=type x; x; string x]] }&#39;[value x]]}&#39;[p_table];
    : head ,  "<table style=\"width:100%\">" , headerRow , dataRows , "</table>";
};

这些函数用于将Q表格转换为HTML表格,并包含了一些格式化样式。

还有一个用于发送邮件的函数:

sendMail: { [p_subject; p_body; p_to]
    system"echo \"" , p_body , "\" | mail -s \"" , p_subject , "\" " , p_to;
};

这个函数用于发送邮件,并可以将HTML表格作为邮件正文发送。

如果您需要更多信息或有其他问题,请随时提出。

英文:

This will work to create a string with an HTML table from a Q table, but it isn't particularly performant (it loops through each column in each row). Additional handling would also be needed for non-string list columns.

toHtmlTable: { [p_table]
    headerRow: .h.htc[`tr] raze .h.htc[`th;]&#39;[string cols p_table];
	dataRows: raze {.h.htc[`tr;] raze[{.h.htc[`td; $[10h=type x; x; string x]] }&#39;[value x]]}&#39;[p_table];
    : .h.htc[`table; headerRow , dataRows];
 };

toFormattedHtmlTable: { [p_table]
    head: &quot;&lt;head&gt; &lt;style&gt;  table, th, td {font-size:13pt; border:1px solid #D6D9DC; border-collapse:collapse; text-align:left;} th, td {padding: 5px;} &lt;/style&gt; &lt;/head&gt;&quot;;
    headerRow: .h.htc[`tr] raze .h.htc[`th;]&#39;[string cols p_table];
    dataRows: raze {.h.htc[`tr;] raze[{.h.htc[`td; $[10h=type x; x; string x]] }&#39;[value x]]}&#39;[p_table];	
    : head ,  &quot;&lt;table style=\&quot;width:100%\&quot;&gt;&quot; , headerRow , dataRows , &quot;&lt;/table&gt;&quot;;
 };


q){show x; show toHtmlTable x;} tab: ([]ColA:1 2 3; ColB:4 5 6; ColC:`a`b`c; ColD:(&quot;abc&quot;; &quot;def&quot;; &quot;ghi&quot;))
ColA ColB ColC ColD
--------------------
1    4    a    &quot;abc&quot;
2    5    b    &quot;def&quot;
3    6    c    &quot;ghi&quot;
&quot;&lt;table&gt;&lt;tr&gt;&lt;th&gt;ColA&lt;/th&gt;&lt;th&gt;ColB&lt;/th&gt;&lt;th&gt;ColC&lt;/th&gt;&lt;th&gt;ColD&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;a&lt;/td&gt;&lt;td&gt;abc&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;b&lt;/td&gt;&lt;td&gt;def&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;c&lt;/td&gt;&lt;td&gt;ghi&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quot;

q){show x; show 100 cut toFormattedHtmlTable x;} tab: ([]ColA:1 2 3; ColB:4 5 6; ColC:`a`b`c; ColD:(&quot;abc&quot;; &quot;def&quot;; &quot;ghi&quot;))
ColA ColB ColC ColD
--------------------
1    4    a    &quot;abc&quot;
2    5    b    &quot;def&quot;
3    6    c    &quot;ghi&quot;
&quot;&lt;head&gt; &lt;style&gt;  table, th, td {font-size:13pt; border:1px solid #D6D9DC; border-collapse:collapse; t&quot;
&quot;ext-align:left;} th, td {padding: 5px;} &lt;/style&gt; &lt;/head&gt;&lt;table style=\&quot;width:100%\&quot;&gt;&lt;tr&gt;&lt;th&gt;ColA&lt;/th&gt;&lt;&quot;
&quot;th&gt;ColB&lt;/th&gt;&lt;th&gt;ColC&lt;/th&gt;&lt;th&gt;ColD&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;a&lt;/td&gt;&lt;td&gt;abc&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;t&quot;
&quot;d&gt;2&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;b&lt;/td&gt;&lt;td&gt;def&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;c&lt;/td&gt;&lt;td&gt;ghi&lt;/td&gt;&lt;/tr&gt;&lt;/ta&quot;
&quot;ble&gt;&quot;

A function to send mail could also be created like (note I don't have access to a box to test this right now...):

sendMail: { [p_subject; p_body; p_to]
    system&quot;echo \&quot;&quot; , p_body , &quot;\&quot; | mail -s \&quot;&quot; , p_subject , &quot;\&quot; &quot; , p_to;
	};

So you could do:

sendMail[&quot;my subject&quot;; .h.htc[`html; toHtmlTable tab]; &quot;foo@bar.com&quot;]

huangapple
  • 本文由 发表于 2023年7月18日 15:45:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定