基本可用。
This commit is contained in:
69
main.cpp
69
main.cpp
@@ -2,10 +2,10 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <xlnt/xlnt.hpp>
|
||||
|
||||
#include "bf.request.h"
|
||||
#include "bf.util.h"
|
||||
#include "bf.xlnt.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "Windows.h"
|
||||
@@ -17,6 +17,7 @@ int main(int argc, char** argv)
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
#endif
|
||||
CLI::App app{"百度翻译xlsx批量工具。"};
|
||||
argv = app.ensure_utf8(argv);
|
||||
|
||||
std::string workbook;
|
||||
std::string sheetName;
|
||||
@@ -25,11 +26,11 @@ int main(int argc, char** argv)
|
||||
bool testRequest = false;
|
||||
std::vector<std::string> tasks; // 存储多个字符串
|
||||
|
||||
app.add_option("-t,--task", tasks, "要处理的行列范围(起始行,起始列,结束行,结束列),如:1,1,10,10)")->multi_option_policy();
|
||||
app.add_option("-r,--task", tasks, "要处理的行列范围(起始行,起始列,结束行,结束列),如:1,1,10,10)")->multi_option_policy();
|
||||
app.add_option("-s,--sheet", sheetName, "工作表名称");
|
||||
app.add_option("-w,--workbook", workbook, "工作簿");
|
||||
app.add_option("-w,--workbook", workbook, "工作簿")->check(CLI::ExistingFile);
|
||||
app.add_option("-f,--from", from, "源语言(默认auto)");
|
||||
app.add_option("--to-language,--to", to, "目标语言(默认en)");
|
||||
app.add_option("-p,--to", to, "目标语言(默认en)");
|
||||
app.add_flag("--test-request", testRequest, "测试请求");
|
||||
|
||||
// 添加其他可选参数
|
||||
@@ -51,6 +52,7 @@ int main(int argc, char** argv)
|
||||
// }
|
||||
// return 0;
|
||||
|
||||
gLogger->debug("开始处理xlsx文件:{}", workbook);
|
||||
BF_Interface* request = new BF_Request();
|
||||
std::shared_ptr<void> del(nullptr, [request](void*) { delete request; });
|
||||
|
||||
@@ -72,42 +74,35 @@ int main(int argc, char** argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
xlnt::workbook wb;
|
||||
xlnt::worksheet ws;
|
||||
try {
|
||||
wb.load("");
|
||||
ws = wb.sheet_by_title(sheetName);
|
||||
std::vector<XlsxTask> 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<std::string>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
std::vector<XlsxTask> xlsxTasks;
|
||||
for (const auto& task : tasks) {
|
||||
auto v = BF_Util::split(task, ",");
|
||||
if (v.size() != 4) {
|
||||
gLogger->error("任务格式错误:{}", task);
|
||||
return -1;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
gLogger->error(e.what());
|
||||
XlsxTask xlsxTask;
|
||||
xlsxTask.startRow = std::stoi(v[0]);
|
||||
xlsxTask.startCol = std::stoi(v[1]);
|
||||
xlsxTask.endRow = std::stoi(v[2]);
|
||||
xlsxTask.endCol = std::stoi(v[3]);
|
||||
xlsxTask.from = from;
|
||||
xlsxTask.to = to;
|
||||
xlsxTasks.push_back(xlsxTask);
|
||||
}
|
||||
|
||||
if (xlsxTasks.empty()) {
|
||||
gLogger->error("没有指定任务!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::shared_ptr<BF_Xlnt> xlnt = std::make_shared<BF_Xlnt>();
|
||||
xlnt->Set(request, bc);
|
||||
if (!xlnt->parseXlsx(workbook, xlsxTasks, sheetName)) {
|
||||
gLogger->error("处理xlsx文件失败!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
wb.save(fmt::format("{}_out.xlsx", workbook));
|
||||
gLogger->info("结束!");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user