#include #include #include #include #include #include "bf.request.h" #include "bf.util.h" #ifdef _WIN32 #include "Windows.h" #endif int main(int argc, char** argv) { #ifdef _WIN32 SetConsoleOutputCP(CP_UTF8); #endif CLI::App app{"百度翻译xlsx批量工具。"}; std::string workbook; std::string sheetName; std::string from = "auto"; std::string to = "en"; bool testRequest = false; std::vector tasks; // 存储多个字符串 app.add_option("-t,--task", tasks, "要处理的行列范围(起始行,起始列,结束行,结束列),如:1,1,10,10)")->multi_option_policy(); app.add_option("-s,--sheet", sheetName, "工作表名称"); app.add_option("-w,--workbook", workbook, "工作簿"); app.add_option("-f,--from", from, "源语言(默认auto)"); app.add_option("--to-language,--to", to, "目标语言(默认en)"); app.add_flag("--test-request", testRequest, "测试请求"); // 添加其他可选参数 std::string config = "baidu_fanyi.json"; app.add_option("-c,--config", config, "配置文件(默认baidu_fanyi.json)"); bool verbose = false; app.add_flag("-v,--verbose", verbose, "详细输出"); // 解析命令行参数 CLI11_PARSE(app, argc, argv); BF_Util::initLogger(); BF_Util::setLogLevel(spdlog::level::debug); // 测试参数,这里直接返回。 // for (const auto& task : tasks) { // gLogger->info("任务:{}", task); // } // return 0; BF_Interface* request = new BF_Request(); std::shared_ptr del(nullptr, [request](void*) { delete request; }); std::shared_ptr bc = std::make_shared(); if (!bc->parseConfig(config)) { gLogger->error("解析配置文件失败!"); return -1; } request->setConfig(bc); if (testRequest) { gLogger->info("测试请求开始..."); gLogger->info("测试内容:[你好,世界。] from auto to en."); std::string readyData = request->doReady("你好,世界。", from, to); std::string data = fmt::format("{}{}", bc->baseUrl, readyData); std::string result = request->getResult(data); gLogger->info("测试请求结束!"); gLogger->info("结果:[{}]", result); return 0; } xlnt::workbook wb; xlnt::worksheet ws; try { wb.load(""); ws = wb.sheet_by_title(sheetName); std::vector tasks; for (auto& task : tasks) { for (int i = task.startRow; i <= task.endRow; i++) { for (int j = task.startCol; j <= task.endCol; j++) { xlnt::cell cell = ws.cell(i, j); std::string text = cell.value(); if (text.empty()) { gLogger->error("数据为空,跳过:[{}][{}]", i, j); continue; } auto readyData = request->doReady(text, from, to); if (readyData.empty()) { gLogger->error("doReady数据为空,跳过:[{}][{}][{}]", i, j, text); continue; } auto data = fmt::format("{}{}", bc->baseUrl, readyData); std::string result = request->getResult(data); if (result.empty()) { gLogger->error("getResult数据为空,跳过:[{}][{}][{}]", i, j, text); continue; } gLogger->info("翻译成功:[{}][{}][{}][{}]", i, j, text, result); cell.value(result); } } } } catch (const std::exception& e) { gLogger->error(e.what()); } wb.save(fmt::format("{}_out.xlsx", workbook)); gLogger->info("结束!"); return 0; }