google-apps-script - 在 Google Docs 中查找并格式化所有以集合字符串开头的单词
问题描述
我在 Google Apps Script 中创建了一个函数,用于搜索 Google Docs 中的所有单词并将它们的颜色更改为所需的颜色。它需要输入:文档 ID、所需颜色和要查找的单词。
但是,我真正需要的是一个函数,它可以找到所有以特定字符串开头的单词。例如,“将所有以 # 开头的单词更改为蓝色”。我试着弄乱,findText()
但没有运气。关于如何修复下面的功能来做我需要的任何想法?谢谢!
目前,我的功能如下所示:
function colorTheWords(findMe,color,documentID) {
//color input must be formatted in CSS notation like '#ffffff'
//documentID must be formated as text in between ''
//findMe word must be formatted as ''
//open doc
var document = DocumentApp.openById(documentID);
var body = document.getBody();
var foundElement = body.findText(findMe);
while (foundElement != null) {
// Get the text object from the element
var foundText = foundElement.getElement().asText();
// Where in the Element is the found text?
var start = foundElement.getStartOffset();
var end = foundElement.getEndOffsetInclusive();
// Change the current color to the desired color
foundText.setForegroundColor(start, end, color);
// Find the next match
foundElement = body.findText(findMe, foundElement);
}
}
解决方案
推荐阅读
- python - 如何在 Flask 中将列表从一个函数传递到另一个函数?
- python - 使用 spotipy 将曲目添加到播放列表
- ios - Swift:替换 sqlite 文件失败
- c - 将 C 代码中的调试文本打印到 Lauterbach TRACE32
- python - 我想用 python pandas 显示 excel 文件中的所有数据/过滤数据
- sql - 如何在 SQL Server 中使用函数?
- python - Pyinstaller - Matplotlib 安装已损坏 - 无法执行 scrypt
- python - 如何管理跨编辑器的“缩进中制表符和空格的不一致使用”
- java - 我们可以使用 Java selenium 代码测试 .net Web 应用程序吗?
- javascript - 用于 Jest 的 React Package.json 文件 setupFiles 未加载