use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required,
};
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> = api_endpoint
.to_request()
.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> = api_endpoint
.to_request()
.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> = api_endpoint
.to_request()
.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> = api_endpoint
.to_request()
.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> = api_endpoint
.to_request()
.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> = api_endpoint
.to_request()
.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> = api_endpoint
.to_request()
.body(serialize_params(¶ms, "取消合并单元格")?);
let response = Transport::request(api_request, config, Some(option)).await?;
extract_response_data(response, "取消合并单元格")
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
#[tokio::test]
async fn test_delete_range_returns_data_on_success() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path(
"/open-apis/sheets/v3/spreadsheets/token001/dimensionRange/delete",
))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0, "msg": "success", "data": {}
})))
.mount(&server)
.await;
let config = Config::builder()
.app_id("ci_app_id")
.app_secret("ci_app_secret")
.base_url(server.uri())
.enable_token_cache(false)
.build();
let resp = delete_range(
&config,
"token001",
DeleteRangeParams {
range: "Sheet1!A1:A5".into(),
dimension: "ROWS".into(),
},
)
.await
.expect("删除范围应成功");
assert!(resp.data.is_none());
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(
received[0].url.path(),
"/open-apis/sheets/v3/spreadsheets/token001/dimensionRange/delete"
);
}
}