增加range方法支持

This commit is contained in:
csh
2024-01-19 17:37:26 +08:00
parent b4649653d9
commit 1ac447c94e
10 changed files with 152 additions and 64 deletions
+84 -10
View File
@@ -13,7 +13,6 @@ public
Function SplitRun(index);overload;
Function Clear(deleteAll);
Function Size();
Function Root();
Function Print();
Begin
@@ -27,6 +26,7 @@ private
Function AddRunInfo(runNode, tParagraph);
Function AddRunInfo2(runNode, tParagraph);
Function ParagraphRange(node);
Function CalcPosition(startNode);
public
data_; // 管理range结构的数组,字段如下
@@ -106,7 +106,7 @@ Function TSWdRange.Create(rootNode, startP, endP);overload;
Begin
root_node_ := rootNode;
start_position_ := startP;
end_position_ := endP;
end_position_ := endP = -1 ? INF : endP;
target_node_ := nil;
self.Init();
End;
@@ -153,21 +153,15 @@ Begin
return length(data_);
End;
Function TSWdRange.Root();
Begin
node := root_node_;
return node;
End;
Function TSWdRange.GetStart();
Begin
if ifnil(start_position_) then return 0;
if ifnil(start_position_) then self.CalcPosition(root_node_);
return start_position_;
End;
Function TSWdRange.GetEnd();
Begin
if ifnil(end_position_) then return 0;
if ifnil(end_position_) then self.CalcPosition(root_node_);
return end_position_;
End;
@@ -372,6 +366,86 @@ Begin
end;
End;
Function TSWdRange.CalcPosition(startNode);
Begin
node := startNode.FirstChildElement();
while ifObj(node) do
begin
if node.Eq(target_node_) then
begin
eq_flag := true;
start_position_ := cur_position_;
end
case node.GetName() of
"w:p":
begin
paragraph_obj := TOfficeObj("TParagraph");
paragraph_obj.Init(node);
if paragraph_obj.Empty() then // 空段落
begin
cur_position_ += 1;
end
else begin
sub_node := node.FirstChildElement();
while ifObj(sub_node) do
begin
case sub_node.GetName() of
"w:r":
begin
run_obj := TOfficeObj("TRun");
run_obj.Init(sub_node);
text := run_obj.Text();
if class(TSXml).IsUtf8() then text := Utf8ToAnsi(text);
cur_position_ += lengthw(text);
end
"w:ins", "w:del":
begin
run_node := sub_node.FirstChildElement("w:r");
while ifObj(run_node) do
begin
run_obj := TOfficeObj("TRun");
run_obj.Init(run_node);
text := run_obj.Text();
if class(TSXml).IsUtf8() then text := Utf8ToAnsi(text);
cur_position_ += lengthw(text);
run_node := run_node.NextElement("w:r");
end
end
end;
sub_node := sub_node.NextElement();
end
// 完整段落
cur_position_ += 1;
end
end
"w:tbl":
begin
tr := node.FirstChildElement("w:tr");
while ifObj(tr) do
begin
tc := tr.FirstChildElement("w:tc");
while ifObj(tc) do
begin
flag := self.CalcPosition(tc);
if flag then return 1;
tc := tc.NextElement("w:tc");
end
cur_position_ += 1;
tr := tr.NextElement("w:tr");
end
end
end;
if eq_flag then
begin
end_position_ := cur_position_;
return 1;
end
node := node.NextElement();
end
End;
Function TSWdRange.ParagraphRange(node);
Begin
paragraph_obj := TOfficeObj("TParagraph");