Skip to content

Commit 6740a43

Browse files
committed
UPDATE: Add "Export Area to markdown"
1 parent 2b3ae8e commit 6740a43

File tree

9 files changed

+117
-8
lines changed

9 files changed

+117
-8
lines changed

base/src/model.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,16 +1931,32 @@ impl Model {
19311931
}
19321932

19331933
/// Returns markup representation of the given `sheet`.
1934-
pub fn get_sheet_markup(&self, sheet: u32) -> Result<String, String> {
1935-
let worksheet = self.workbook.worksheet(sheet)?;
1936-
let dimension = worksheet.dimension();
1934+
pub fn get_sheet_markup(
1935+
&self,
1936+
sheet: u32,
1937+
start_row: i32,
1938+
start_column: i32,
1939+
width: i32,
1940+
height: i32,
1941+
) -> Result<String, String> {
1942+
let mut table: Vec<Vec<String>> = Vec::new();
1943+
if start_row < 1 || start_column < 1 {
1944+
return Err("Start row and column must be positive".to_string());
1945+
}
1946+
if start_row + height >= LAST_ROW || start_column + width >= LAST_COLUMN {
1947+
return Err("Start row and column exceed the maximum allowed".to_string());
1948+
}
1949+
if height <= 0 || width <= 0 {
1950+
return Err("Height must be positive and width must be positive".to_string());
1951+
}
19371952

1938-
let mut rows = Vec::new();
1953+
// a mutable vector to store the column widths of length `width + 1`
1954+
let mut column_widths: Vec<f64> = vec![0.0; (width + 1) as usize];
19391955

1940-
for row in 1..(dimension.max_row + 1) {
1956+
for row in start_row..(start_row + height + 1) {
19411957
let mut row_markup: Vec<String> = Vec::new();
19421958

1943-
for column in 1..(dimension.max_column + 1) {
1959+
for column in start_column..(start_column + width + 1) {
19441960
let mut cell_markup = match self.get_cell_formula(sheet, row, column)? {
19451961
Some(formula) => formula,
19461962
None => self.get_formatted_cell_value(sheet, row, column)?,
@@ -1949,12 +1965,34 @@ impl Model {
19491965
if style.font.b {
19501966
cell_markup = format!("**{cell_markup}**")
19511967
}
1968+
column_widths[(column - start_column) as usize] =
1969+
column_widths[(column - start_column) as usize].max(cell_markup.len() as f64);
19521970
row_markup.push(cell_markup);
19531971
}
19541972

1955-
rows.push(row_markup.join("|"));
1973+
table.push(row_markup);
19561974
}
1975+
let mut rows = Vec::new();
1976+
for (j, row) in table.iter().enumerate() {
1977+
if j == 1 {
1978+
let mut row_markup = String::new();
1979+
for i in 0..(width + 1) {
1980+
row_markup.push('|');
1981+
let wide = column_widths[i as usize] as usize;
1982+
row_markup.push_str(&"-".repeat(wide));
1983+
}
1984+
rows.push(row_markup);
1985+
}
1986+
let mut row_markup = String::new();
19571987

1988+
for (i, cell) in row.iter().enumerate() {
1989+
row_markup.push('|');
1990+
let wide = column_widths[i] as usize;
1991+
// Add padding to the cell content
1992+
row_markup.push_str(&format!("{:<wide$}", cell, wide = wide));
1993+
}
1994+
rows.push(row_markup);
1995+
}
19581996
Ok(rows.join("\n"))
19591997
}
19601998

base/src/test/test_sheet_markup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn test_sheet_markup() {
2121
model.set_cell_style(0, 4, 1, &style).unwrap();
2222

2323
assert_eq!(
24-
model.get_sheet_markup(0),
24+
model.get_sheet_markup(0, 1, 1, 4, 2),
2525
Ok("**Item**|**Cost**\nRent|$600\nElectricity|$200\n**Total**|=SUM(B2:B3)".to_string()),
2626
)
2727
}

base/src/user_model/common.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,19 @@ impl UserModel {
293293
self.model.workbook.name = name.to_string();
294294
}
295295

296+
/// Get area markdown
297+
pub fn get_sheet_markup(
298+
&self,
299+
sheet: u32,
300+
row_start: i32,
301+
column_start: i32,
302+
row_end: i32,
303+
column_end: i32,
304+
) -> Result<String, String> {
305+
self.model
306+
.get_sheet_markup(sheet, row_start, column_start, row_end, column_end)
307+
}
308+
296309
/// Undoes last change if any, places the change in the redo list and evaluates the model if needed
297310
///
298311
/// See also:

bindings/wasm/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,4 +672,18 @@ impl Model {
672672
.delete_defined_name(name, scope)
673673
.map_err(|e| to_js_error(e.to_string()))
674674
}
675+
676+
#[wasm_bindgen(js_name = "getSheetMarkup")]
677+
pub fn get_sheet_markup(
678+
&self,
679+
sheet: u32,
680+
start_row: i32,
681+
start_column: i32,
682+
end_row: i32,
683+
end_column: i32,
684+
) -> Result<String, JsError> {
685+
self.model
686+
.get_sheet_markup(sheet, start_row, start_column, end_row, end_column)
687+
.map_err(to_js_error)
688+
}
675689
}

webapp/IronCalc/src/components/Toolbar/Toolbar.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
ArrowMiddleFromLine,
4141
DecimalPlacesDecreaseIcon,
4242
DecimalPlacesIncreaseIcon,
43+
Markdown,
4344
} from "../../icons";
4445
import { theme } from "../../theme";
4546
import BorderPicker from "../BorderPicker/BorderPicker";
@@ -74,6 +75,7 @@ type ToolbarProperties = {
7475
onClearFormatting: () => void;
7576
onIncreaseFontSize: (delta: number) => void;
7677
onDownloadPNG: () => void;
78+
onCopyMarkdown: () => void;
7779
fillColor: string;
7880
fontColor: string;
7981
fontSize: number;
@@ -429,6 +431,17 @@ function Toolbar(properties: ToolbarProperties) {
429431
>
430432
<ImageDown />
431433
</StyledButton>
434+
<StyledButton
435+
type="button"
436+
$pressed={false}
437+
onClick={() => {
438+
properties.onCopyMarkdown();
439+
}}
440+
disabled={!canEdit}
441+
title={t("toolbar.selected_markdown")}
442+
>
443+
<Markdown />
444+
</StyledButton>
432445

433446
<ColorPicker
434447
color={properties.fontColor}

webapp/IronCalc/src/components/Workbook/Workbook.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,26 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => {
558558
onIncreaseFontSize={(delta: number) => {
559559
onIncreaseFontSize(delta);
560560
}}
561+
onCopyMarkdown={async () => {
562+
const {
563+
sheet,
564+
range: [rowStart, columnStart, rowEnd, columnEnd],
565+
} = model.getSelectedView();
566+
const row = Math.min(rowStart, rowEnd);
567+
const column = Math.min(columnStart, columnEnd);
568+
const width = Math.abs(columnEnd - columnStart) + 1;
569+
const height = Math.abs(rowEnd - rowStart) + 1;
570+
const markdown = model.getSheetMarkup(
571+
sheet,
572+
row,
573+
column,
574+
width,
575+
height,
576+
);
577+
// Copy to clipboard
578+
// NB: This will not work in non secure contexts or in iframes (i.e storybook)
579+
await navigator.clipboard.writeText(markdown);
580+
}}
561581
onDownloadPNG={() => {
562582
// creates a new canvas element in the visible part of the the selected area
563583
const worksheetCanvas = worksheetRef.current?.getCanvas();

webapp/IronCalc/src/icons/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import InsertColumnLeftIcon from "./insert-column-left.svg?react";
1919
import InsertColumnRightIcon from "./insert-column-right.svg?react";
2020
import InsertRowAboveIcon from "./insert-row-above.svg?react";
2121
import InsertRowBelow from "./insert-row-below.svg?react";
22+
import Markdown from "./markdown.svg?react";
2223

2324
import IronCalcIcon from "./ironcalc_icon.svg?react";
2425
import IronCalcLogo from "./orange+black.svg?react";
@@ -48,4 +49,5 @@ export {
4849
IronCalcIcon,
4950
IronCalcLogo,
5051
Fx,
52+
Markdown,
5153
};

webapp/IronCalc/src/icons/markdown.svg

Lines changed: 8 additions & 0 deletions
Loading

webapp/IronCalc/src/locale/en_us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"vertical_align_middle": " Align middle",
2727
"vertical_align_top": "Align top",
2828
"selected_png": "Export Selected area as PNG",
29+
"selected_markdown": "Export Selected area as Markdown",
2930
"wrap_text": "Wrap text",
3031
"format_menu": {
3132
"auto": "Auto",

0 commit comments

Comments
 (0)