@@ -1931,16 +1931,32 @@ impl Model {
1931
1931
}
1932
1932
1933
1933
/// 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
+ }
1937
1952
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 ] ;
1939
1955
1940
- for row in 1 ..( dimension . max_row + 1 ) {
1956
+ for row in start_row ..( start_row + height + 1 ) {
1941
1957
let mut row_markup: Vec < String > = Vec :: new ( ) ;
1942
1958
1943
- for column in 1 ..( dimension . max_column + 1 ) {
1959
+ for column in start_column ..( start_column + width + 1 ) {
1944
1960
let mut cell_markup = match self . get_cell_formula ( sheet, row, column) ? {
1945
1961
Some ( formula) => formula,
1946
1962
None => self . get_formatted_cell_value ( sheet, row, column) ?,
@@ -1949,12 +1965,34 @@ impl Model {
1949
1965
if style. font . b {
1950
1966
cell_markup = format ! ( "**{cell_markup}**" )
1951
1967
}
1968
+ column_widths[ ( column - start_column) as usize ] =
1969
+ column_widths[ ( column - start_column) as usize ] . max ( cell_markup. len ( ) as f64 ) ;
1952
1970
row_markup. push ( cell_markup) ;
1953
1971
}
1954
1972
1955
- rows . push ( row_markup. join ( "|" ) ) ;
1973
+ table . push ( row_markup) ;
1956
1974
}
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 ( ) ;
1957
1987
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
+ }
1958
1996
Ok ( rows. join ( "\n " ) )
1959
1997
}
1960
1998
0 commit comments