简体   繁体   English

如何通过Google附加工具栏设置“文档”属性?

[英]How do I set Docs attributes from a Google add-on sidebar?

I am trying to create an add-on for Docs that will allow my students to highlight selections of a text using certain pre-defined highlighter tools in the add-on sidebar (similar to this add-on , but with predefined highlighter tools). 我正在尝试为Google文档创建一个插件,该插件允许我的学生使用插件侧边栏中的某些预定义的荧光笔工具来突出显示所选的文本(类似于该插件 ,但带有预定义的荧光笔工具)。

I've been able to use Google scripts to interact with my drive files, create folders, rename files, etc., and I know how to use .onclick functions within HTML. 我已经能够使用Google脚本与驱动器文件进行交互,创建文件夹,重命名文件等,而且我知道如何在HTML中使用.onclick函数。 Where I am stuck is getting the buttons in my sidebar to change the attributes of selected text within the doc when the button is clicked. 卡住的地方是单击边栏上的按钮以更改文档中所选文本的属性。

Based on my reading in the Developers documentation, my .gs script looks like this: 根据我在开发人员文档中的阅读,我的.gs脚本如下所示:

 function highlightStyleGreen() {
      var selection = DocumentApp.getActiveDocument().getSelection();
      if (selection) {
        var elements = selection.getRangeElements();
        for (var i = 0; i < elements.length; i++) {
        var element = elements[i];}
        var highlightStyleGreen = {};
         highlightStyle[DocumentApp.Attribute.BACKGROUND_COLOR] =    '#7CCD7C';

       selection.setAttributes(highlightStyleGreen);
      }
    }

In the HTML, the button is assigned like this: 在HTML中,按钮的分配方式如下:

< button onclick="highlightStyleGreen()">Main Idea < /button> <button onclick =“ highlightStyleGreen()”>主要思想</ button>

Clearly this isn't working, but I'm not sure what to try next. 显然这是行不通的,但是我不确定下一步该怎么做。

Help? 救命?

if you are calling a google-apps-script function (including custom functions) from a custom dialog or sidebar, the syntax is: 如果您要从自定义对话框或边栏中调用google-apps-script函数(包括自定义函数),则语法为:

<button onclick="google.script.run.highlightStyleGreen()">Main Idea</button>

documentation: https://developers.google.com/apps-script/guides/html/communication 文档: https : //developers.google.com/apps-script/guides/html/communication


And here is the google-apps-script code. 这是google-apps-script代码。 This function will highlight the user's selection in green. 此功能将以绿色突出显示用户的选择。

function highlightStyleGreen() {

  //App>Doc>Body>Paragraph>Text
  //selection>rangeElements>RangeElement>Element>Text

  var selection = DocumentApp.getActiveDocument().getSelection();
  if (selection) {

    //declare variables once before loop
    var elements = selection.getRangeElements();    
    var rngEelement;
    var element;
    var text;
    var startOffset;
    var endOffsetInclusive;

    //loop through selection
    for (var i=0; i < elements.length; i++){
      rngElement = elements[i];
      element = rngElement.getElement();
      if (element) {
        text = element.asText();  
        if (text) {
          //if we are on a 'partial element' we need to only grab the selected part of the text
          if (rngElement.isPartial()) {
            startOffset = rngElement.getStartOffset();
            endOffsetInclusive = rngElement.getEndOffsetInclusive();
            text.setBackgroundColor(startOffset, endOffsetInclusive, '#00FF00');
          } else {
            text.setBackgroundColor('#00FF00');
          }
        }
      }
    }
  }
}

documentation: https://developers.google.com/apps-script/guides/docs 文档: https : //developers.google.com/apps-script/guides/docs

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM