如何用Google脚本将字符串替换为Google文档中的多线文本

发布于 2025-01-23 08:41:45 字数 824 浏览 0 评论 0原文

我遇到了麻烦,使用应用程序脚本在Google文档中替换多线文本。
我花了一些时间搜索和测试类似的问题,但我无法学习很多。

我在Google文档中有几集文字。我正在使用标签标签来查找需要更换哪些文本集团。我将文本块从“ || tag_label ”中替换为“ ||

|| tag_label
lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do ut ut labore et dolore magna aliqua。
ut enim ad enim ad ad ad Minim 静脉 ea commodo eatsion。||


从我学到的东西中,我必须将字符串传递给替换。
我已经检查了控制台,而文本托图特(Texttoreplace)是一个字符串,但文本未替换

  function regexTestFunction(){

  const document = DocumentApp.getActiveDocument();
  const documentBody = document.getBody();

  const keyWord = "TAG_LABEL";
  const textToReplace = "\\|{2} (.*?)" + keyWord + "[\\s\\S]*?\\|{2}";

  const deleteText = documentBody.replaceText(textToReplace,"");

}

感谢您的帮助

I'm having trouble, replacing multi-lines text in a Google Doc, using App Script.
I've took the time to search and test similar questions I've learn a lot but I can't go through.

I have several blocs of text like below in my google Doc. I'm using tag labels to find what text bloc needs to be replaced. I replace the text block from "||TAG_LABEL" to "||"

||TAG_LABEL
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ut labore et dolore magna aliqua.
Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat.||

From what I've learned I have to pass a string to replaceText.
I've checked the console and textToReplace is a string but the text is not replaced

  function regexTestFunction(){

  const document = DocumentApp.getActiveDocument();
  const documentBody = document.getBody();

  const keyWord = "TAG_LABEL";
  const textToReplace = "\\|{2} (.*?)" + keyWord + "[\\s\\S]*?\\|{2}";

  const deleteText = documentBody.replaceText(textToReplace,"");

}

Thank you for your help

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

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

发布评论

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

评论(3

鱼窥荷 2025-01-30 08:41:45

从您的显示样本值如下,

    ||TAG_LABEL
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ut labore et dolore magna aliqua.
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.||

似乎您要替换的值具有多个段落。在这种情况下,似乎不能直接使用替换。因此,在这种情况下,以下示例脚本怎么样?

示例脚本:

在此示例脚本中,使用Google Docs API。因此,请在高级Google Services 上启用Google docs api 。

function regexTestFunction() {
  const document = DocumentApp.getActiveDocument();
  const documentBody = document.getBody();
  const keyWord = "TAG_LABEL";
  const textToReplace = "\\|{2}.*?" + keyWord + "[\\s\\S]*?\\|{2}";
  const r = new RegExp(textToReplace, "g");
  const matches = documentBody.getText().match(r);
  if (!matches || matches.length == 0) return;
  const requests = matches.map(text => ({ replaceAllText: { containsText: { matchCase: false, text }, replaceText: "" } }));
  Docs.Documents.batchUpdate({ requests }, document.getId());
}
  • 运行此脚本时,您的示例值将替换为“”是空的。

  • 在此示例脚本中,使用以下流程。

    1. 从文档中检索所有文本。
    2. 使用texttoreplace的正则从文本中检索值。
    3. 使用Google Docs API替换匹配的文本。

注意:

  • 我不确定您的实际文档。因此,当上述示例脚本没有用时,您可以提供示例文档吗?这样,我想确认。

参考:

  • a href =“ https://developer.mozilla.org/en-us/docs/web/javascript/Reference/global_objects/array/array/map/map”
  • < a href =“ https://developers.google.com/docs/api/reference/rest/rest/v1/documents/batchupdate” rel =“ nofollow noreferrer”> document

From your showing sample value as follows,

    ||TAG_LABEL
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ut labore et dolore magna aliqua.
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.||

it seems that the value you want to replace has multiple paragraphs. In this case, it seems that replaceText cannot be directly used. So, in this case, how about the following sample script?

Sample script:

In this sample script, Google Docs API is used. So, please enable Google Docs API at Advanced Google services.

function regexTestFunction() {
  const document = DocumentApp.getActiveDocument();
  const documentBody = document.getBody();
  const keyWord = "TAG_LABEL";
  const textToReplace = "\\|{2}.*?" + keyWord + "[\\s\\S]*?\\|{2}";
  const r = new RegExp(textToReplace, "g");
  const matches = documentBody.getText().match(r);
  if (!matches || matches.length == 0) return;
  const requests = matches.map(text => ({ replaceAllText: { containsText: { matchCase: false, text }, replaceText: "" } }));
  Docs.Documents.batchUpdate({ requests }, document.getId());
}
  • When you run this script, your sample value is replaced with "" which is empty.

  • In this sample script, the following flow is used.

    1. Retrieve all texts from Document.
    2. Retrieve values using the regex of textToReplace from the texts.
    3. Replace the matched texts using Google Docs API.

Note:

  • I'm not sure about your actual Document. So when the above sample script was not useful, can you provide the sample Document? By this, I would like to confirm it.

References:

白况 2025-01-30 08:41:45

将所有行设置为一行

function setTextToOneSentence() {
  let d = DocumentApp.getActiveDocument();
  let b = d.getBody();
  b.setText("This is one sentence.")
}

Set all lines to one line

function setTextToOneSentence() {
  let d = DocumentApp.getActiveDocument();
  let b = d.getBody();
  b.setText("This is one sentence.")
}
不知在何时 2025-01-30 08:41:45

为了解决我的解决方案,我调整了您使用的正则态度并提出了:

function regexTestFunction(){
  const document = DocumentApp.getActiveDocument();
  const documentBody = document.getBody();
  const keyWord = "TAG_LABEL";
  var replace = `\\|{2}[^\\|]*${keyWord}[^\\|]*\\|{2}`;
  var re = new RegExp(replace, "g");
  documentBody.setText(documentBody.getText().replace(re,""));
}

这是什么是替代“ || tag_label”和“ ||”中的文本。我对您的设置进行了修改测试了您的设置:

应用脚本后,我得到了:

但是,我注意到,当标签之间存在一个管道(|)时,脚本不起作用。因此,如果您没有期望标签之间有任何单个管道,则可以使用此脚本。

For my solution, I adjusted the regex you used and came up with:

function regexTestFunction(){
  const document = DocumentApp.getActiveDocument();
  const documentBody = document.getBody();
  const keyWord = "TAG_LABEL";
  var replace = `\\|{2}[^\\|]*${keyWord}[^\\|]*\\|{2}`;
  var re = new RegExp(replace, "g");
  documentBody.setText(documentBody.getText().replace(re,""));
}

What this does is it replaces the text within the "||TAG_LABEL" and "||". I tested the script on your setup with modifications:
enter image description here

After applying the script, I got:
enter image description here

However, I noticed that when a single pipe (|) is present in between the tags, the script does not work. Hence, you can use this script if you are not expecting any single pipes in between the tags.

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