如何制作基于单词首字符为单词着色的Google App脚本?

问题描述

我知道如何使用Google Apps脚本(最主要是使用Google表格)的非常基本的知识,但是我是Google文档脚本的新手。

我正在寻找一个检查google docs文档中每个单词的函数。当单词以特定字符开头时,整个单词都会变色。该函数应将color属性和第一个字符作为参数,以便我可以切换以某些字符开头的单词会获得什么颜色。

有人可以帮助我吗?

这就是我所知道的。

我知道如何获取文档的文本

  var document = DocumentApp.openById(documentId);
  var text = document.getBody().getText();

我知道如何计算字数(很难弄清楚)

function countWords(text) {
    var s = text;
    //this function kept returning "1" when the doc was blank 
    //so this is how I stopped having it return 1.       
  
    if (s.length === 0) 
        return 0;
    //A simple \n replacement didn't work,neither did \s not sure why
    s = s.replace(/\r\n|\r|\n/g," ");
    //In cases where you have "...last word.First word..." 
    //it doesn't count the two words around the period.
    //so I replace all punctuation with a space
    var punctuationless = s.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()"?“”]/g," ");
    //Finally,trim it down to single spaces (not sure this even matters)
    var finalString = punctuationless.replace(/\s{2,}/g," ");
    //Actually count it
    var count = finalString.trim().split(/\s+/).length; 
    return count;
}

现在,我需要编写某种循环,从i = 0i = count逐字逐句地检查第一个字符是否为选中的那个,然后将其更改为指定的颜色

例如,如果我想使以字符“#”开头的文档中的所有单词都被涂成蓝色字体,那么我要做的就是运行带有文档ID,字符#的函数。然后将编辑蓝色和整个文档的代码,使每个以#开头的单词变为蓝色。

有帮助吗?

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)