Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion module/move/gspread/src/actions/gspread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,29 @@ mod private
google_sheets4::Error
),

#[ error( "Invalid URL format: {0}" ) ]
#[ error( "Invalid URL format:\n{0}" ) ]
InvalidUrl
(
String
),

#[ error( "Cell error:\n{0}" ) ]
CellError
(
String
),

#[ error( "Invalid JSON format:\n{0}" ) ]
InvalidJSON
(
String
),

#[ error( "Parse error:\n{0}" ) ]
ParseError
(
String
)
}

pub fn get_spreadsheet_id_from_url
Expand Down Expand Up @@ -60,6 +78,7 @@ crate::mod_interface!
{
own use
{
Error,
Result,
get_spreadsheet_id_from_url,
};
Expand Down
25 changes: 16 additions & 9 deletions module/move/gspread/src/actions/gspread_cell_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@

mod private
{


use crate::*;
use actions::gspread::Result;
use actions::gspread::
{
Error,
Result
};
use client::SheetsType;
use ser::JsonValue;

Expand All @@ -19,18 +25,19 @@ mod private
cell_id : &str,
) -> Result< JsonValue >
{
let result = hub
match hub
.spreadsheets()
.values_get( spreadsheet_id, format!( "{}!{}", table_name, cell_id ).as_str() )
.doit()
.await?
.1
.values;

match result
.await
{
Some( values ) => Ok( values.get( 0 ).unwrap().get( 0 ).unwrap().clone() ),
None => Ok( JsonValue::Null.clone() )
Ok( (_, response ) ) =>
match response.values
{
Some( values ) => Ok( values.get( 0 ).unwrap().get( 0 ).unwrap().clone() ),
None => Ok( JsonValue::Null.clone() )
}
Err( error ) => Err( Error::ApiError( error ) )
}

}
Expand Down
27 changes: 19 additions & 8 deletions module/move/gspread/src/actions/gspread_cell_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ mod private
{
use google_sheets4::api::ValueRange;
use crate::*;
use actions::gspread::Result;
use actions::gspread::
{
Result,
Error
};
use client::SheetsType;
use ser::JsonValue;

Expand All @@ -20,7 +24,7 @@ mod private
table_name : &str,
cell_id : &str,
value : &str
) -> Result< i32 >
) -> Result< String >
{

let value = JsonValue::String( value.to_string() );
Expand All @@ -30,17 +34,24 @@ mod private
..ValueRange::default()
};

let result = hub
match hub
.spreadsheets()
.values_update( value_range, spreadsheet_id, format!( "{}!{}", table_name, cell_id ).as_str() )
.value_input_option( "USER_ENTERED" )
.doit()
.await?
.1
.updated_cells
.unwrap();
.await
{
Ok( ( _, response) ) =>
{
match response.updated_cells
{
Some( number ) => Ok( format!( "You successfully update {} cell!", number ) ),
None => Err( Error::CellError( "Some problem with cell updating".to_string() ) )
}
}
Err( error) => Err( Error::ApiError( error ) )
}

Ok( result )
}
}

Expand Down
37 changes: 24 additions & 13 deletions module/move/gspread/src/actions/gspread_cells_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
mod private
{
use crate::*;
use actions::gspread::
{
Error,
Result
};
use google_sheets4::api::
{
BatchUpdateValuesRequest,
Expand All @@ -29,11 +34,11 @@ mod private
fn parse_json
(
json_str : &str
) -> Result< ParsedJson, String >
) -> Result< ParsedJson >
{
serde_json::from_str::< ParsedJson >( json_str ).map_err
(
| err | format!( "Failed to parse JSON: {}", err )
| error | Error::InvalidJSON( format!( "Failed to parse JSON: {}", error ) )
)
}

Expand All @@ -42,7 +47,7 @@ mod private
fn check_select_row_by_key
(
key : &str
) -> Result< (), String >
) -> Result< () >
{
let keys = vec![ "id" ];
if keys.contains( &key )
Expand All @@ -51,22 +56,28 @@ mod private
}
else
{
Err( format!( "Invalid select_row_by_key: '{}'. Allowed keys: {:?}", key, keys ) )
Err
(
Error::ParseError( format!( "Invalid select_row_by_key: '{}'. Allowed keys: {:?}", key, keys ) )
)
}
}

fn is_all_uppercase_letters
(
s : &str
) -> Result< (), String >
) -> Result< () >
{
if s.chars().all( | c | c.is_ascii_uppercase() )
{
Ok( () )
}
else
{
Err( format!( "The string '{}' contains invalid characters. Only uppercase letters (A-Z) are allowed.", s ) )
Err
(
Error::ParseError( format!( "The string '{}' contains invalid characters. Only uppercase letters (A-Z) are allowed.", s ) )
)
}
}

Expand All @@ -77,7 +88,7 @@ mod private
json_str : &str,
spreadsheet_id : &str,
table_name : &str
) -> Result< String, String >
) -> Result< String >
{
check_select_row_by_key( select_row_by_key )?;

Expand All @@ -86,7 +97,8 @@ mod private
let row_id = pairs
.columns
.remove( select_row_by_key )
.ok_or_else( || format!( "Key '{}' not found in JSON", select_row_by_key ) )?;
.ok_or_else( || Error::ParseError( format!( "Key '{}' not found in JSON", select_row_by_key ) ) )?;


let mut value_ranges= Vec::new();

Expand All @@ -112,18 +124,17 @@ mod private
..Default::default()
};

let result = hub
match hub
.spreadsheets()
.values_batch_update( req, spreadsheet_id )
.doit()
.await;

match result
.await
{
Ok( _ ) => Ok( format!( "Cells were sucsessfully updated!" ) ),
Err( error ) => Err( format!( "{}", error ) )
Err( error ) => Err( Error::ApiError( error ) )
}
}

}

crate::mod_interface!
Expand Down
30 changes: 21 additions & 9 deletions module/move/gspread/src/actions/gspread_get_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ mod private
use std::fmt;
use crate::*;
use client::SheetsType;
use actions::gspread::Result;
use actions::gspread::
{
Error,
Result
};
use format_tools::AsTable;
use util::display_table::display_header;
use ser::JsonValue;
Expand All @@ -37,19 +41,27 @@ mod private
(
hub : &SheetsType,
spreadsheet_id : &str,
table_name: &str) -> Result< Vec< Vec< JsonValue > > >
table_name : &str
) -> Result< Vec< Vec< JsonValue > > >
{
let result = hub
match hub
.spreadsheets()
.values_get( spreadsheet_id, format!( "{}!A1:Z1", table_name ).as_str() )
.doit()
.await?
.1
.values
.unwrap_or_else( | | Vec::new() );

Ok( result )
.await
{
Ok( ( _, response ) ) =>
{
match response.values
{
Some( values ) => Ok( values ),
None => Ok( Vec::new() )
}
},
Err( error ) => Err( Error::ApiError( error ) )
}
}

}

crate::mod_interface!
Expand Down
26 changes: 18 additions & 8 deletions module/move/gspread/src/actions/gspread_get_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ mod private
{
use crate::*;
use client::SheetsType;
use actions::gspread::Result;
use actions::gspread::
{
Error,
Result
};
use ser::JsonValue;

pub async fn action
Expand All @@ -19,16 +23,22 @@ mod private
table_name : &str
) -> Result< Vec< Vec < JsonValue > > >
{
let result = hub
match hub
.spreadsheets()
.values_get( spreadsheet_id, format!( "{}!A2:Z", table_name ).as_str() )
.doit()
.await?
.1
.values
.unwrap_or_else( | | Vec::new() );

Ok( result )
.await
{
Ok( ( _, response ) ) =>
{
match response.values
{
Some( values ) => Ok( values ),
None => Ok( Vec::new() )
}
},
Err( error ) => Err( Error::ApiError( error ) )
}
}
}

Expand Down
12 changes: 9 additions & 3 deletions module/move/gspread/src/commands/gspread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,41 @@ mod private
#[ derive( Debug, Parser ) ]
pub struct CommonArgs
{
#[ arg( long ) ]
#[ arg( long, help = "Full URL of Google Sheet.\n\
It has to be inside of '' to avoid parse errors.\n\
Example: 'https://docs.google.com/spreadsheets/d/your_spreadsheet_id/edit?gid=0#gid=0'" ) ]
pub url : String,

#[ arg( long ) ]
#[ arg( long, help = "Sheet name.\nExample: List1" ) ]
pub tab : String
}

#[ derive( Debug, Subcommand ) ]
pub enum Command
{


/// Command to get header of a sheet. Header is a first raw.
#[ command ( name = "header" ) ]
Header
(
CommonArgs
),

/// Command to get all raws of a sheet but not header.
#[ command( name = "rows" ) ]
Rows
(
CommonArgs
),

/// Command to get or update a cell from a sheet.
#[ command ( subcommand, name = "cell" ) ]
Cell
(
gspread_cell::Commands
),

/// Commands to set a new value to a cell or get a value from a cell.
#[ command ( subcommand, name = "cells" ) ]
Cells
(
Expand Down
Loading
Loading