如何仅将* selected *文本更改为大写,而不更改所选内容所在的整个段落 [英] How to change only the *selected* text to uppercase, not the entire paragraph in which the selection exists

查看:37
本文介绍了如何仅将* selected *文本更改为大写,而不更改所选内容所在的整个段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google文档中,此功能将所选文本更改为黑色

In Google docs, this function changes the selected text to black

function selectedFontColorBlack() {
//   DocumentApp.getUi().alert('selectedFontColorBlack');
   var sel = DocumentApp.getActiveDocument().getSelection();
   var elements = sel.getRangeElements();
   for (var i = 0; i < elements.length; i++) {
      var element = elements[i];

      // Only modify elements that can be edited as text; skip images and other non-text elements.
      if(element.getElement().editAsText) {
         var text = element.getElement().editAsText();

       // Bold the selected part of the element, or the full element if it's completely selected.
       if (element.isPartial()) {
         text.setForegroundColor(element.getStartOffset(), element.getEndOffsetInclusive(), "#000000");
       } else {
         text.setForegroundColor("#000000");
       }
     }
   }
}

此函数将光标(或所选内容)所在的整个段落更改为大写:

This function changes the entire paragraph in which the cursor (or selection) exists to uppercase:

function uppercaseSelected() {
//  DocumentApp.getUi().alert('uppercaseSelected');
   var sel = DocumentApp.getActiveDocument().getSelection();
   var elements = sel.getRangeElements();
   for (var i = 0; i < elements.length; i++) {
      var element = elements[i];

      // Only modify elements that can be edited as text; skip images and other non-text elements.
      if(element.getElement().editAsText) {
         var text = element.getElement().editAsText();
         text.setText(text.getText().toUpperCase());
       }
   }
}

我没有看到任何相应的 setText 函数,该函数可用于选择的偏移", Text .)

I don't see any corresponding setText function that works on the selection's "offset", as does the setForegroundColor(Integer,Integer,String). (Both of these functions are in class Text.)

如何将实际选择的文本更改为大写字母,而不是大写的整个段落?

How can I change the actually selected text to uppercase, and not the entire paragraph in which the selection exists?

谢谢.

推荐答案

尝试使用 setAttributes(startOffset,endOffsetInclusive,attributes)方法.查看文档
@Mogsdad所指的帖子中隐藏的宝石是: var selectedText = elementText.substring(startOffset,endOffset + 1); .
稍​​微详细些:如何在诸如 DocumentApp.getActiveDocument().getSelection().getSelectedElements()[]等对象上使用字符串方法 substring i] .getElement().editAsText().getText()因此,从本质上讲,抓住该子字符串,将其转换为大写,删除(selectedElement.getstartOffset,selectedElement.endOffsetInclusive)范围内的文本,然后将粗体文本插入 selectedElement.getstartOffset >

Try using the setAttributes(startOffset, endOffsetInclusive, attributes) method. Check out the documentation
The gem hidden in the post that @Mogsdad is referring to is this: var selectedText = elementText.substring(startOffset,endOffset+1);.
to be little more verbose on how this is used: you can use the string method substring on objects such as DocumentApp.getActiveDocument().getSelection().getSelectedElements()[i].getElement().editAsText().getText() so, essentially, grab that substring, convert it to uppercase, delete the text in the range (selectedElement.getstartOffset,selectedElement.endOffsetInclusive) and insert the bolded text at selectedElement.getstartOffset

多田!检查一下:

function uppercaseSelected() {
   // Try to get the current selection in the document. If this fails (e.g.,
   // because nothing is selected), show an alert and exit the function.
   var selection = DocumentApp.getActiveDocument().getSelection();
   if (!selection) {
      DocumentApp.getUi().alert('Cannot find a selection in the document.');
      return;
   }

   var selectedElements = selection.getSelectedElements();
   for (var i = 0; i < selectedElements.length; ++i) {
      var selectedElement = selectedElements[i];

      // Only modify elements that can be edited as text; skip images and other
      // non-text elements.
      var text = selectedElement.getElement().editAsText();

      // Change the background color of the selected part of the element, or the
      // full element if it's completely selected.
      if (selectedElement.isPartial()) {
        var bitoftext = text.getText().substring(selectedElement.getStartOffset(), selectedElement.getEndOffsetInclusive() + 1);
        text.deleteText(selectedElement.getStartOffset(), selectedElement.getEndOffsetInclusive());
        text.insertText(selectedElement.getStartOffset(), bitoftext.toUpperCase());
      }  else  {
        text.setText(text.getText().toUpperCase());
      }
   }
}

这篇关于如何仅将* selected *文本更改为大写,而不更改所选内容所在的整个段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆