有关提取单词文本和处理单元格中断字符的建议

发布于 2024-07-10 08:17:53 字数 729 浏览 5 评论 0原文

寻求建议(也许是最佳实践)。

我们有一个 MS Word 文档 (Office 2007),我们正在从单元格中提取文本。

我们可以使用以下内容:

string text = wordTable.cell(tablerow.index, 1).Range.Text;

提取文本; 然而我们似乎得到了额外的字符尾随,例如 \r\a

现在我们可以添加以下内容:

.... wordTable.cell(tablerow.index, 1).Range.Text.Replace("\r\a,"");

但这似乎有点太懒了,而且几乎是浪费时间,很可能会导致以后出现问题。

我们还可以有一个方法来接收要清理的字符串:

private string cleanTextWordCellBreak(string wordTextToClean)
{
    // Clean the text here
    return cleanstring;
}

然后我们可以使用它:

cleanTextWordCellBreak(wordTable.cell(tablerow.index, 1).Range.Text;
);

这似乎更接近于处理该问题的更好方法。 你会怎么办?

Looking for advice (perhaps best practice).

We have a MS Word document (Office 2007) that we are extracting text from a cell.

We can use the following:

string text = wordTable.cell(tablerow.index, 1).Range.Text;

The text is extracted; however we seem to get extra characters trailing, for example \r\a.

Now we could add the following:

.... wordTable.cell(tablerow.index, 1).Range.Text.Replace("\r\a,"");

But this seems a little too lazy, and pretty much a waste of time that would most likely lead to problems down the road.

We could also have a method that receives the string to clean:

private string cleanTextWordCellBreak(string wordTextToClean)
{
    // Clean the text here
    return cleanstring;
}

then we could use it:

cleanTextWordCellBreak(wordTable.cell(tablerow.index, 1).Range.Text;
);

This seems closer to a better way of handling the issue. What would you do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-07-17 08:17:53

我会将其分解为一个单独的方法,但使用替换实现,因为它是最简单的解决方案。 如果遇到问题,您可以随时更改实现(例如文本包含多个 \r\a 并且需要保留),

因此:

private string stripCellText(string text)
{
      return text.Replace("\r\a", "");
}

string text = stripCellText(wordTable.cell(tablerow.index, 1).Range.Text);

I would break it out into a separate method but use the replace implementation since it's the simplest solution. You could always change the implementation later if you run into problem (like the text contains more than one \r\a and needs to be preserved)

So:

private string stripCellText(string text)
{
      return text.Replace("\r\a", "");
}

string text = stripCellText(wordTable.cell(tablerow.index, 1).Range.Text);
一生独一 2024-07-17 08:17:53

我个人肯定会选择将其分解为一个单独的方法。 它有助于提高代码的可读性,并使将来需要时更容易更改。

I would definitely opt for breaking it out into a separate method personally. it helps with code readability and makes it a lot easier to change if needed in the future.

昨迟人 2024-07-17 08:17:53

获取它的另一种方法是获取字符和字符的长度。 提取文本达到该长度。

<代码> <代码>

dim range as Range
dim text as string
dim length as Integer

range = ActiveDocument.Tables(1).Cell(1,1).Range
text = range.Text
length = range.Characters.Count

Debug.Print Mid(text, 1, length - 1)

>

Another way of getting it would be get the length of Characters & extracting text upto that length.

dim range as Range
dim text as string
dim length as Integer

range = ActiveDocument.Tables(1).Cell(1,1).Range
text = range.Text
length = range.Characters.Count

Debug.Print Mid(text, 1, length - 1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文