use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required, SDKResult,
};
use crate::common::{api_endpoints::CcmSheetApiOld, api_utils::*};
#[derive(Debug, Clone)]
pub struct SheetOperationsApi {
config: Config,
}
impl SheetOperationsApi {
pub fn new(config: Config) -> Self {
Self { config }
}
pub fn config(&self) -> &Config {
&self.config
}
}
pub mod models;
pub use models::{
DeleteRangeParams, DeleteRangeResponse, DeleteRangeResult, FindReplaceParams,
FindReplaceResponse, FindReplaceResult, InsertDimensionParams, InsertDimensionResponse,
InsertDimensionResult, MergeCellsParams, MergeCellsResponse, MergeCellsResult,
MoveDimensionParams, MoveDimensionResponse, MoveDimensionResult, ReplaceRangeParams,
ReplaceRangeResponse, ReplaceRangeResult, UnmergeCellsParams, UnmergeCellsResponse,
UnmergeCellsResult,
};
impl ApiResponseTrait for DeleteRangeResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for InsertDimensionResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for MoveDimensionResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for ReplaceRangeResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for FindReplaceResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for MergeCellsResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ApiResponseTrait for UnmergeCellsResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn delete_range(
config: &Config,
spreadsheet_token: &str,
params: DeleteRangeParams,
) -> SDKResult<DeleteRangeResponse> {
delete_range_with_options(config, spreadsheet_token, params, RequestOption::default()).await
}
pub async fn delete_range_with_options(
config: &Config,
spreadsheet_token: &str,
params: DeleteRangeParams,
option: RequestOption,
) -> SDKResult<DeleteRangeResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.range, "删除范围不能为空");
let api_endpoint = CcmSheetApiOld::DeleteRange(spreadsheet_token.to_string());
let api_request: ApiRequest<DeleteRangeResponse> =
ApiRequest::post(&api_endpoint.to_url()).body(serialize_params(¶ms, "删除范围")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "删除范围")
}
pub async fn insert_dimension(
config: &Config,
spreadsheet_token: &str,
params: InsertDimensionParams,
) -> SDKResult<InsertDimensionResponse> {
insert_dimension_with_options(config, spreadsheet_token, params, RequestOption::default()).await
}
pub async fn insert_dimension_with_options(
config: &Config,
spreadsheet_token: &str,
params: InsertDimensionParams,
option: RequestOption,
) -> SDKResult<InsertDimensionResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.range, "插入范围不能为空");
let api_endpoint = CcmSheetApiOld::InsertDimension(spreadsheet_token.to_string());
let api_request: ApiRequest<InsertDimensionResponse> =
ApiRequest::post(&api_endpoint.to_url()).body(serialize_params(¶ms, "插入行列")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "插入行列")
}
pub async fn move_dimension(
config: &Config,
spreadsheet_token: &str,
params: MoveDimensionParams,
) -> SDKResult<MoveDimensionResponse> {
move_dimension_with_options(config, spreadsheet_token, params, RequestOption::default()).await
}
pub async fn move_dimension_with_options(
config: &Config,
spreadsheet_token: &str,
params: MoveDimensionParams,
option: RequestOption,
) -> SDKResult<MoveDimensionResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.source_range, "源范围不能为空");
let api_endpoint = CcmSheetApiOld::MoveDimension(spreadsheet_token.to_string());
let api_request: ApiRequest<MoveDimensionResponse> =
ApiRequest::post(&api_endpoint.to_url()).body(serialize_params(¶ms, "移动行列")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "移动行列")
}
pub async fn replace_range(
config: &Config,
spreadsheet_token: &str,
params: ReplaceRangeParams,
) -> SDKResult<ReplaceRangeResponse> {
replace_range_with_options(config, spreadsheet_token, params, RequestOption::default()).await
}
pub async fn replace_range_with_options(
config: &Config,
spreadsheet_token: &str,
params: ReplaceRangeParams,
option: RequestOption,
) -> SDKResult<ReplaceRangeResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.range, "替换范围不能为空");
let api_endpoint = CcmSheetApiOld::ReplaceRange(spreadsheet_token.to_string());
let api_request: ApiRequest<ReplaceRangeResponse> =
ApiRequest::post(&api_endpoint.to_url()).body(serialize_params(¶ms, "替换范围")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "替换范围")
}
pub async fn find_replace(
config: &Config,
spreadsheet_token: &str,
params: FindReplaceParams,
) -> SDKResult<FindReplaceResponse> {
find_replace_with_options(config, spreadsheet_token, params, RequestOption::default()).await
}
pub async fn find_replace_with_options(
config: &Config,
spreadsheet_token: &str,
params: FindReplaceParams,
option: RequestOption,
) -> SDKResult<FindReplaceResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.range, "查找范围不能为空");
validate_required!(params.find, "查找内容不能为空");
let api_endpoint = CcmSheetApiOld::FindReplace(spreadsheet_token.to_string());
let api_request: ApiRequest<FindReplaceResponse> =
ApiRequest::post(&api_endpoint.to_url()).body(serialize_params(¶ms, "查找替换")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "查找替换")
}
pub async fn merge_cells(
config: &Config,
spreadsheet_token: &str,
params: MergeCellsParams,
) -> SDKResult<MergeCellsResponse> {
merge_cells_with_options(config, spreadsheet_token, params, RequestOption::default()).await
}
pub async fn merge_cells_with_options(
config: &Config,
spreadsheet_token: &str,
params: MergeCellsParams,
option: RequestOption,
) -> SDKResult<MergeCellsResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.range, "合并范围不能为空");
let api_endpoint = CcmSheetApiOld::MergeCells(spreadsheet_token.to_string());
let api_request: ApiRequest<MergeCellsResponse> =
ApiRequest::post(&api_endpoint.to_url()).body(serialize_params(¶ms, "合并单元格")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "合并单元格")
}
pub async fn unmerge_cells(
config: &Config,
spreadsheet_token: &str,
params: UnmergeCellsParams,
) -> SDKResult<UnmergeCellsResponse> {
unmerge_cells_with_options(config, spreadsheet_token, params, RequestOption::default()).await
}
pub async fn unmerge_cells_with_options(
config: &Config,
spreadsheet_token: &str,
params: UnmergeCellsParams,
option: RequestOption,
) -> SDKResult<UnmergeCellsResponse> {
validate_required!(spreadsheet_token.trim(), "表格Token不能为空");
validate_required!(params.range, "取消合并范围不能为空");
let api_endpoint = CcmSheetApiOld::UnmergeCells(spreadsheet_token.to_string());
let api_request: ApiRequest<UnmergeCellsResponse> =
ApiRequest::post(&api_endpoint.to_url()).body(serialize_params(¶ms, "取消合并单元格")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "取消合并单元格")
}
#[cfg(test)]
mod tests {
use serde_json;
#[test]
fn test_serialization_roundtrip() {
let json = r#"{"test": "value"}"#;
assert!(serde_json::from_str::<serde_json::Value>(json).is_ok());
}
#[test]
fn test_deserialization_from_json() {
let json = r#"{"field": "data"}"#;
let value: serde_json::Value = serde_json::from_str(json).unwrap();
assert_eq!(value["field"], "data");
}
}