--- a/sw/source/uibase/uno/loktxdoc.cxx 2026-10-04 10:51:12 +++ b/sw/source/uibase/uno/loktxdoc.cxx 2026-10-04 10:51:12 @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -1110,6 +1111,26 @@ rJsonWriter.put("formula", xSet->getPropertyValue(u"Formula"_ustr).get()); } +/// Implements getCommandValues(".uno:WordCount"). +void GetWordCount(tools::JsonWriter& rJsonWriter, SwDocShell* pDocShell) +{ + SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); + if (!pWrtShell) + return; + SwDocStat aSelection; + pWrtShell->CountWords(aSelection); + const SwDocStat aDocument = pWrtShell->GetUpdatedDocStat(); + for (const auto& [pName, rStat] : + { std::pair("document", aDocument), + std::pair("selection", aSelection) }) + { + auto aNode = rJsonWriter.startNode(pName); + rJsonWriter.put("words", static_cast(rStat.nWord)); + rJsonWriter.put("characters", static_cast(rStat.nChar)); + rJsonWriter.put("charactersNoSpaces", static_cast(rStat.nCharExcludingSpaces)); + } +} + /// Implements getCommandValues(".uno:Sections"). /// /// Parameters: @@ -1148,7 +1169,7 @@ = { u"TextFormFields", u"TextFormField", u"SetDocumentProperties", u"Bookmarks", u"Fields", u"Sections", u"Bookmark", u"Field", u"Layout", - u"Formula" }; + u"Formula", u"WordCount" }; return std::find(vForward.begin(), vForward.end(), rCommand) != vForward.end(); } @@ -1235,6 +1256,10 @@ { GetDocStructure(rJsonWriter, m_pDocShell, aMap); } + else if (o3tl::starts_with(rCommand, ".uno:WordCount"sv)) + { + GetWordCount(rJsonWriter, m_pDocShell); + } else if (o3tl::starts_with(rCommand, ".uno:Formula"sv)) { GetFormula(rJsonWriter, m_pDocShell);