From e6a9c2e8c1efa25b6bb8824a524ebad31343d728 Mon Sep 17 00:00:00 2001 From: Syres916 <46537884+Syres916@users.noreply.github.com> Date: Sat, 7 Sep 2024 15:20:04 +0100 Subject: [PATCH] [Spreadsheet] Fix CSV import by insert --- src/Mod/Spreadsheet/Gui/AppSpreadsheetGui.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Mod/Spreadsheet/Gui/AppSpreadsheetGui.cpp b/src/Mod/Spreadsheet/Gui/AppSpreadsheetGui.cpp index bde374d128..fb6f21c701 100644 --- a/src/Mod/Spreadsheet/Gui/AppSpreadsheetGui.cpp +++ b/src/Mod/Spreadsheet/Gui/AppSpreadsheetGui.cpp @@ -60,6 +60,7 @@ public: : Py::ExtensionModule("SpreadsheetGui") { add_varargs_method("open", &Module::open); + add_varargs_method("insert", &Module::insert); initialize("This module is the SpreadsheetGui module."); // register with Python } @@ -90,6 +91,32 @@ private: return Py::None(); } + + Py::Object insert(const Py::Tuple& args) + { + char* Name; + const char* DocName = nullptr; + if (!PyArg_ParseTuple(args.ptr(), "et|s", "utf-8", &Name, &DocName)) { + throw Py::Exception(); + } + std::string EncodedName = std::string(Name); + PyMem_Free(Name); + + try { + Base::FileInfo file(EncodedName); + App::Document* pcDoc = Gui::Application::Instance->activeDocument()->getDocument(); + Spreadsheet::Sheet* pcSheet = static_cast( + pcDoc->addObject("Spreadsheet::Sheet", file.fileNamePure().c_str())); + + pcSheet->importFromFile(EncodedName, '\t', '"', '\\'); + pcSheet->execute(); + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + + return Py::None(); + } }; PyObject* initModule()