Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemsakurai/f1956843fcbdec37d52bb8605278a953 to your computer and use it in GitHub Desktop.
Save kemsakurai/f1956843fcbdec37d52bb8605278a953 to your computer and use it in GitHub Desktop.
スプレッドシート の表形式のデータをmarkdwonに変換するTampermonkey のUserScript
javascript: (function(e)%7Bvar t%3D%7Beval:%27"!function()%7Bvar n%3Dwindow.prompt(%5C%5C"SpreadSheet の値を markdownのtableに変換%5C%5C")%3Bnull!%3Dn%26%26%5C%5C"%5C%5C"!%3Dn%26%26(!function(n)%7Bvar e%3Ddocument.createElement(%5C%5C"div%5C%5C")%3Be.appendChild(document.createElement(%5C%5C"pre%5C%5C")).textContent%3Dn%3Bvar t%3De.style%3Bt.position%3D%5C%5C"fixed%5C%5C",t.left%3D%5C%5C"-100%25%5C%5C",document.body.appendChild(e),document.getSelection().selectAllChildren(e),document.execCommand(%5C%5C"copy%5C%5C"),document.body.removeChild(e)%7D(function(n)%7Bvar e%3Dn.split(/%5B%5C%5C%5C%5Cn%5C%5C%5C%5Cu0085%5C%5C%5C%5Cu2028%5C%5C%5C%5Cu2029%5D%7C%5C%5C%5C%5Cr%5C%5C%5C%5Cn%3F/g).map((function(n)%7Breturn console.log(n),n.split(%5C%5C"%5C%5C%5C%5Ct%5C%5C")%7D)),t%3De%5B0%5D.map((function(n,t)%7Breturn function(n,e)%7Breturn Math.max.apply(null,n.map((function(n)%7Breturn n%5Be%5D.length%7D)))%7D(e,t)%7D))%3Bconsole.log(t)%3Bvar o%3De.map((function(n,e)%7Breturn%5C%5C"%7C %5C%5C"%2Bn.map((function(n,e)%7Breturn n%2BArray(t%5Be%5D-n.length%2B1).join(%5C%5C" %5C%5C")%7D)).join(%5C%5C" %7C %5C%5C")%2B%5C%5C" %7C%5C%5C"%7D))%3Breturn o.splice(1,0,%5C%5C"%7C%5C%5C"%2Bt.map((function(n,e)%7Breturn Array(t%5Be%5D%2B3).join(%5C%5C"-%5C%5C")%7D)).join(%5C%5C"%7C%5C%5C")%2B%5C%5C"%7C%5C%5C"),o.join(%5C%5C"%5C%5C%5C%5Cn%5C%5C")%7D(n)),alert(%5C%5C"コピー終了%5C%5C"))%7D()%3B"%27%7D,n%3D!0%3Bif("object"%3D%3Dtypeof this.artoo%26%26(artoo.settings.reload%7C%7C(artoo.log.verbose("artoo already exists within this page. No need to inject him again."),artoo.loadSettings(t),artoo.exec(),n%3D!1)),n)%7Bvar o%3Ddocument.getElementsByTagName("body")%5B0%5D%3Bo%7C%7C(o%3Ddocument.createElement("body"),document.firstChild.appendChild(o))%3Bvar i%3Ddocument.createElement("script")%3Bconsole.log("artoo.js is loading..."),i.src%3D"//medialab.github.io/artoo/public/dist/artoo-latest.min.js",i.type%3D"text/javascript",i.id%3D"artoo_injected_script",i.setAttribute("settings",JSON.stringify(t)),o.appendChild(i)%7D%7D).call(this)%3B
// ==UserScript==
// @name Convert spreadsheet value to markdown table
// @namespace http://tampermonkey.net/
// @version 0.1
// @description スプレッドシートの表形式のデータをmarkdown のtable フォーマットに変換するTampermonkeyスクリプト
// @author K.Sakurai
// @resource CSS1 https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css
// @require https://cdn.jsdelivr.net/npm/toastify-js
// @match http*://*/*
// @run-at context-menu
// @grant GM_addStyle
// @grant GM_getResourceText
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
// CSS読み込み
GM_addStyle(GM_getResourceText('CSS1'));
var value = window.prompt("SpreadSheet の値を markdownのtableに変換");
if (value == null || value == "") {
return;
}
var result = convertSheetValueToMarkdownTable(value);
copyToClipBoardFrom(result);
noticeMessage("texttile形式に変換しました");
// クリップボードにコピーする
function copyToClipBoardFrom(text) {
GM_setClipboard(text);
}
// メッセージ通知
function noticeMessage(text) {
Toastify({
text: text,
duration: 3000,
gravity: "bottom", // `top` or `bottom`
position: 'right', // `left`, `center` or `right`
}).showToast();
}
function convertSheetValueToMarkdownTable(str) {
var rows = str.split((/[\n\u0085\u2028\u2029]|\r\n?/g)).map(function(row) {
console.log(row)
return row.split("\t")
})
var columnWidths = rows[0].map(function(column, columnIndex) {
return columnWidth(rows, columnIndex)
})
console.log(columnWidths);
var markdownRows = rows.map(function(row, rowIndex) {
// | Name | Title | Email Address |
// |--------------|-------|----------------|
// | Jane Atler | CEO | jane@acme.com |
// | John Doherty | CTO | john@acme.com |
// | Sally Smith | CFO | sally@acme.com |
return "| " + row.map(function(column, index) {
return column + Array(columnWidths[index] - column.length + 1).join(" ")
}).join(" | ") + " |"
row.map
})
markdownRows.splice(1, 0, "|" + columnWidths.map(function(width, index) {
return Array(columnWidths[index] + 3).join("-")
}).join("|") + "|")
// https://www.w3.org/TR/clipboard-apis/#the-paste-action
// When pasting, the drag data store mode flag is read-only, hence calling
// setData() from a paste event handler will not modify the data that is
// inserted, and not modify the data on the clipboard.
return markdownRows.join("\n")
}
function columnWidth(rows, columnIndex) {
return Math.max.apply(null, rows.map(function(row) {
return row[columnIndex].length
}))
}
})();
@kemsakurai
Copy link
Author

補足

https://github.com/jonmagic/copy-excel-paste-markdown
上記を参考に作成。
右クリックで、プロンプトが立ち上がるので、スプレッドシート のタブ区切りデータを貼り付けると、
クリップボードにmarkdwon形式のデータがコピーできる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment