Skip to content

Commit eed9368

Browse files
authored
0.2.0: integrating with editor-paste event
1 parent 0d467cc commit eed9368

File tree

6 files changed

+55
-10
lines changed

6 files changed

+55
-10
lines changed

README.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
# Obsidian Excel to Markdown Table
22

33
[![Tag 0.1.0](https://img.shields.io/badge/tag-0.1.0-blue)](https://github.com/ganesshkumar/obsidian-excel-to-markdown-table/releases/tag/0.1.0)
4+
[![MIT License](https://img.shields.io/github/license/ganesshkumar/obsidian-excel-to-markdown-table)](LICENSE)
45

56
An Obsidian plugin to paste Excel tables as Markdown tables in Obsidian editor.
67

78
## Demo
89

9-
You can paste the copied Excel date from the clipboard by using either
10+
You can paste the copied Excel data from the clipboard by using either
1011

11-
1. Hotkey - `Crtl/Cmd + Alt + V` (reassign it from settings if needed) or
12-
2. Command Palette: `Ctrl/Cmd + P` > `Excel to Markdown` command
13-
14-
![Obsidian Excel to Markdown Table](https://user-images.githubusercontent.com/2135089/151919147-8315155a-0972-4152-a8cc-70f835b6dece.gif)
15-
**Note**: The hotkeys are `Ctrl/Cmd + Alt + V`
12+
| # | Name | Method | Notes |
13+
|---|------|--------|-------|
14+
|1| Pasting |`Ctrl/Cmd + V` | will paste only table with two or more columns |
15+
|2| Hotkey |`Crtl/Cmd + Alt + V` | will paste table with one column as well <br/> reassign the Hotkey from settings if needed |
16+
|3| Command Palette |`Ctrl/Cmd + P` > `Excel to Markdown` | will paste table with one column as well |
1617

18+
![Obsidian Excel to Markdown Table](https://user-images.githubusercontent.com/2135089/153027044-d1b91515-d5ea-4624-ace1-654c4ceccdc1.gif)
1719

1820
## Installation
1921

@@ -45,3 +47,10 @@ You can paste the copied Excel date from the clipboard by using either
4547
- [vscode-excel-to-markdown-table](https://github.com/csholmq/vscode-excel-to-markdown-table)
4648
- [copy-excel-paste-markdown](https://github.com/thisdavej/copy-excel-paste-markdown)
4749

50+
51+
---
52+
53+
54+
If you like my work, you could consider buying me a coffee. It is unnecessary, but appreciated 🙂
55+
56+
<a href="https://www.buymeacoffee.com/ganesshkumar" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-excel-to-markdown-table",
33
"name": "Excel to Markdown Table",
4-
"version": "0.1.1",
4+
"version": "0.2.0",
55
"minAppVersion": "0.12.0",
66
"description": "An Obsidian plugin to paste Excel tables as Markdown tables in Obsidian editor.",
77
"author": "Ganessh Kumar R P <rpganesshkumar@gmail.com>",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-excel-to-markdown-table",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "An Obsidian plugin to paste Excel tables as Markdown tables in Obsidian editor.",
55
"main": "main.js",
66
"scripts": {

src/excel-markdown-tables.ts

+17
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,20 @@ export function excelToMarkdown(rawData: string): string {
1515

1616
return helper.addAlignmentSyntax(markdownRows, columnWidths, colAlignments).join(LINE_ENDING);
1717
}
18+
19+
export function getExcelRows(rawData: string): string[][] {
20+
let data = rawData.trim();
21+
var intraCellNewlineReplacedData = helper.replaceIntraCellNewline(data)
22+
return helper.splitIntoRowsAndColumns(intraCellNewlineReplacedData);
23+
}
24+
25+
export function excelRowsToMarkdown(rows: string[][]): string {
26+
var {columnWidths, colAlignments } = helper.getColumnWidthsAndAlignments(rows);
27+
const markdownRows = helper.addMarkdownSyntax(rows, columnWidths);
28+
29+
return helper.addAlignmentSyntax(markdownRows, columnWidths, colAlignments).join(LINE_ENDING);
30+
}
31+
32+
export function isExcelData(rows: string[][]): boolean {
33+
return rows && rows[0] && rows[0].length > 1 ? true : false
34+
}

src/main.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import { Editor, MarkdownView, Plugin } from 'obsidian';
2-
import {excelToMarkdown} from './excel-markdown-tables';
2+
import { excelToMarkdown, getExcelRows, isExcelData, excelRowsToMarkdown } from './excel-markdown-tables';
33

44
export default class ExcelToMarkdownTablePlugin extends Plugin {
5+
pasteHandler = (evt: ClipboardEvent, editor: Editor) => {
6+
if (evt.clipboardData === null) {
7+
return;
8+
}
9+
10+
const rawData = evt.clipboardData.getData("text");
11+
const rows = getExcelRows(rawData);
12+
13+
if (isExcelData(rows)) {
14+
const markdownData = excelRowsToMarkdown(rows);
15+
editor.replaceSelection(markdownData + '\n');
16+
evt.preventDefault();
17+
}
18+
}
19+
520
async onload() {
621
this.addCommand({
722
id: 'excel-to-markdown-table',
@@ -17,8 +32,11 @@ export default class ExcelToMarkdownTablePlugin extends Plugin {
1732
editor.replaceSelection(excelToMarkdown(text))
1833
}
1934
});
35+
36+
this.app.workspace.on("editor-paste", this.pasteHandler);
2037
}
2138

2239
onunload() {
40+
this.app.workspace.off("editor-paste", this.pasteHandler);
2341
}
2442
}

versions.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"0.1.0": "0.12.0",
3-
"0.1.1": "0.12.0"
3+
"0.1.1": "0.12.0",
4+
"0.2.0": "0.12.0"
45
}

0 commit comments

Comments
 (0)