{"id":5291,"date":"2021-09-20T15:33:44","date_gmt":"2021-09-20T14:33:44","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=5291"},"modified":"2022-07-25T08:35:41","modified_gmt":"2022-07-25T07:35:41","slug":"javascript-get-data-from-positioned-table","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-get-data-from-positioned-table\/","title":{"rendered":"Javascript: Get data from positioned table"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(() => {\n    function* getDataFromPositionedTable(tableSource) {\n        let lines = String(tableSource).matchAll(\/^.*$\/gm); \/\/ split('\\n');\n        const headerLine = String(lines.next().value[0]);\n        const columnsRegex = \/([^\\s]+[\\s]+)\/g;\n        const columns = Array.from(headerLine.matchAll(columnsRegex)).map(m => ({ name: m[1].trim(), start: m.index, end: m.index + m[1].length }));\n\n        for (const [line] of lines) {\n            if (line.length &lt; headerLine.length) continue;\n            const item = {};\n            for (const c of columns) {\n                item[c.name] = line.substring(c.start, c.end).trim();\n            }\n            yield item;\n        }\n    }\n\n    const text = document.getElementById(\"t1\").value;\n    const data2 = getDataFromPositionedTable(text);\n    console.log(Array.from(data2));\n})();<\/pre>\n\n\n\n<p>To get example data from Windows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">wmic printer list > printers.txt\n:: OR\nwmic startup list > list.txt<\/pre>\n\n\n\n<p>Example data:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Caption                   Command                                                                                                                                                       Description               Location                                                                                        SettingID  \nLibreOffice 6.4           LibreOffice 6.4.lnk                                                                                                                                           LibreOffice 6.4           Startup                                                                                                    \nLocate32 Autorun          Locate32 Autorun.lnk                                                                                                                                          Locate32 Autorun          Startup                                                                                                    \nVerzenden naar OneNote    Verzenden naar OneNote.lnk                                                                                                                                    Verzenden naar OneNote    Startup                                                                                                    <\/pre>\n\n\n\n<p>Example result<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[\n    {\n        \"Caption\": \"LibreOffice 6.4\",\n        \"Command\": \"LibreOffice 6.4.lnk\",\n        \"Description\": \"LibreOffice 6.4\",\n        \"Location\": \"Startup\",\n        \"SettingID\": \"\"\n    },\n    {\n        \"Caption\": \"Locate32 Autorun\",\n        \"Command\": \"Locate32 Autorun.lnk\",\n        \"Description\": \"Locate32 Autorun\",\n        \"Location\": \"Startup\",\n        \"SettingID\": \"\"\n    },\n    {\n        \"Caption\": \"Verzenden naar OneNote\",\n        \"Command\": \"Verzenden naar OneNote.lnk\",\n        \"Description\": \"Verzenden naar OneNote\",\n        \"Location\": \"Startup\",\n        \"SettingID\": \"\"\n    }\n]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To get example data from Windows: Example data: Example result<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4,1],"tags":[],"class_list":["post-5291","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=5291"}],"version-history":[{"count":5,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5291\/revisions"}],"predecessor-version":[{"id":5299,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5291\/revisions\/5299"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=5291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=5291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=5291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}