英文:
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;]'[string cols p_table];
dataRows: raze {.h.htc[`tr;] raze[{.h.htc[`td; $[10h=type x; x; string x]] }'[value x]]}'[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;]'[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;]'[string cols p_table];
dataRows: raze {.h.htc[`tr;] raze[{.h.htc[`td; $[10h=type x; x; string x]] }'[value x]]}'[p_table];
: head , "<table style=\"width:100%\">" , headerRow , dataRows , "</table>";
};
q){show x; show toHtmlTable x;} tab: ([]ColA:1 2 3; ColB:4 5 6; ColC:`a`b`c; ColD:("abc"; "def"; "ghi"))
ColA ColB ColC ColD
--------------------
1 4 a "abc"
2 5 b "def"
3 6 c "ghi"
"<table><tr><th>ColA</th><th>ColB</th><th>ColC</th><th>ColD</th></tr><tr><td>1</td><td>4</td><td>a</td><td>abc</td></tr><tr><td>2</td><td>5</td><td>b</td><td>def</td></tr><tr><td>3</td><td>6</td><td>c</td><td>ghi</td></tr></table>"
q){show x; show 100 cut toFormattedHtmlTable x;} tab: ([]ColA:1 2 3; ColB:4 5 6; ColC:`a`b`c; ColD:("abc"; "def"; "ghi"))
ColA ColB ColC ColD
--------------------
1 4 a "abc"
2 5 b "def"
3 6 c "ghi"
"<head> <style> table, th, td {font-size:13pt; border:1px solid #D6D9DC; border-collapse:collapse; t"
"ext-align:left;} th, td {padding: 5px;} </style> </head><table style=\"width:100%\"><tr><th>ColA</th><"
"th>ColB</th><th>ColC</th><th>ColD</th></tr><tr><td>1</td><td>4</td><td>a</td><td>abc</td></tr><tr><t"
"d>2</td><td>5</td><td>b</td><td>def</td></tr><tr><td>3</td><td>6</td><td>c</td><td>ghi</td></tr></ta"
"ble>"
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"echo \"" , p_body , "\" | mail -s \"" , p_subject , "\" " , p_to;
};
So you could do:
sendMail["my subject"; .h.htc[`html; toHtmlTable tab]; "foo@bar.com"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论