简体   繁体   English

如何使用 Google Apps 脚本删除 Google 文档中的选定文本

[英]How to delete selected text in a Google doc using Google Apps Script

In a Google document is there a way to delete selected text with Google Apps Script?在 Google 文档中是否可以使用 Google Apps 脚本删除选定的文本? The find criterion for the text to delete is not a string, but instead is relative to a bookmark.要删除的文本的查找标准不是字符串,而是相对于书签。 This question is related to a workaround for my open question at https://webapps.stackexchange.com/questions/166391/how-to-move-cursor-to-a-named-bookmark-using-google-apps-script) .这个问题与我在https://webapps.stackexchange.com/questions/166391/how-to-move-cursor-to-a-named-bookmark-using-google-apps-script的未解决问题的解决方法有关.

Here is code I wish worked.这是我希望工作的代码。

function UpdateBookmarkedText() {
  var doc = DocumentApp.getActiveDocument();
  var bookmarks = doc.getBookmarks();   
  for (var i = 0; i < bookmarks.length; i++){
    // Delete the old text, one step in a longer process.
    var text = bookmarks[i].getPosition().getElement().asText().editAsText();
    var range = doc.newRange().addElementsBetween(text, 5, text, 7).build(); // arbitrary offsets for testing
    doc.setSelection(range);  // The selected range is successfully highlighted in the document.
    doc.deleteSelection();  // This command does not exist.
} }

This documentation seems relevant but is over my head: https://developers.google.com/docs/api/how-tos/move-text该文档似乎相关,但在我头上: https://developers.google.com/docs/api/how-tos/move-text

I'm not sure what exactly you're trying to do, so here is a guess.我不确定你到底想做什么,所以这里是一个猜测。 If you able to select something you can remove the selected text about this way:如果您能够 select 某些内容,您可以通过以下方式删除所选文本:

function UpdateBookmarkedText() {
  var doc = DocumentApp.getActiveDocument();
  var bookmarks = doc.getBookmarks();   
  for (var i = 0; i < bookmarks.length; i++){
    // Delete the old text, one step in a longer process.
    var text = bookmarks[i].getPosition().getElement().asText().editAsText();
    var range = doc.newRange().addElementsBetween(text, 5, text, 7).build(); // arbitrary offsets for testing
    doc.setSelection(range);  // The selected range is successfully highlighted in the document.

    // the way to handle a selection
    // from the official documentation
    // https://developers.google.com/apps-script/reference/document/range
    var selection = DocumentApp.getActiveDocument().getSelection();
    if (selection) {
      var elements = selection.getRangeElements();
      for (let element of elements) {
        if (element.getElement().editAsText) {
          var text = element.getElement().editAsText();
          if (element.isPartial()) {
            text.deleteText(element.getStartOffset(), element.getEndOffsetInclusive());
          } else {
            text.setText(''); // not sure about this line
          }
        }
      }
    }

  }
}

Use deleteText()使用 deleteText()

You may use the following script as the basis for your script:您可以使用以下脚本作为脚本的基础:

function deleteSelectedText() {
  var selection = DocumentApp.getActiveDocument().getSelection();
  if (selection) {
    var elements = selection.getRangeElements();
    if (elements[0].getElement().editAsText) {
      var text = elements[0].getElement().editAsText();
      if (elements[0].isPartial()) {
        text.deleteText(elements[0].getStartOffset(), elements[0].getEndOffsetInclusive());
      }
    }
  }
}

This is a modified version of the script featured in the Class Range guide.这是Class 范围指南中的脚本的修改版本。 This modification works for selected sentences within a paragraph.此修改适用于段落中的选定句子。 Thus, the use of the for loop (in the sample script) is not anymore necessary since the script operates within a single element/paragraph.因此,不再需要使用 for 循环(在示例脚本中),因为脚本在单个元素/段落中运行。

Optimized Script:优化脚本:

function test() {
  var selection = DocumentApp.getActiveDocument().getSelection();
  var elements = selection.getRangeElements();
  var text = elements[0].getElement().editAsText();
  (selection && elements[0].getElement().editAsText && elements[0].isPartial()) ? text.deleteText(elements[0].getStartOffset(), elements[0].getEndOffsetInclusive()):null;
}

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Google Doc Apps 脚本缓存选定的文本元素 - Caching Selected Text Element with Google Doc Apps Script 使用谷歌应用程序脚本从谷歌文档中提取文本 - Extract Text from google doc using google apps script 如何使用谷歌应用脚本将谷歌文档标题下的特定文本/行导入谷歌表格? - How to import specific text/line under headings in Google doc to Google sheet using google apps script? 如何使用Google Apps脚本将Google文档文本导入电子邮件草稿 - How to Import Google doc text into Email draft using Google Apps Script 如何使用谷歌应用脚本在谷歌文档中的指定 position 处插入文本? - How to insert text at specified position in google doc using google apps script? 如何使用 Apps 脚本查找和替换 Google Doc 中的特殊字符? - How to find and replace special characters in Google Doc using Apps Script? 使用 Apps Script 在 Google Doc 中格式化表格 - Formatting a table in Google Doc using Apps Script 使用应用程序脚本在谷歌文档中插入脚注 - Inserting footnote in google doc using apps script 在 Google Apps Script 的 Google 文档中插入模板文本 - Inserting template text in a Google doc in Google Apps Script Google Apps Script:如何在不改变文本格式的情况下更改 Google 文档中的文本? - Google Apps Script: How do I change text in a Google Doc without altering the formatting of that text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM