getBody 仍然会抛出 "can't call getBody out of null" 错误,即使它在 Apps 脚本中不为 null
getBody still chucks out a "can't call getBody out of null" error even though it isn't null in Apps Script
getBody 在 Apps 脚本中仍然 returns null,尽管我已经四处寻找错误并进行了大部分调试。另一位用户(向我指出了大部分错误)说这段代码中的 getBody 不为空。这是我的代码还是 Apps 脚本的错误?
function myFunction() {
var searchResult = DocumentApp.getActiveDocument().getBody().findText("very",searchResult)
Logger.log(searchResult)
while (searchResult !== null) {
searchResult.getElement().asText().setAttributes(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),"#FF000")
searchResult = DocumentApp.getActiveDocument().getBody().findText("very",searchResult)
}
function highlightProblem() {
var words = ["very","so","totally","really"]
words.forEach(findText)
}
function onOpen(){
DocumentApp.getUi().createMenu('everythingisnotfine.avi').addItem('Higlight Words That Make You Sound Like a Dandy', 'higlightProblem').addToUi()
}}
您的主要问题是您甚至在定义 searchResult 之前就在使用它。
var searchResult = DocumentApp.getActiveDocument().getBody().findText("very",searchResult)
因为您还没有定义 searchResult
,所以您不能通过 findText()
和 searchResult
作为起点。一旦定义了第一个 searchResult 变量,就可以在 while 循环中使用它来查找匹配项
while (searchResult !== null) {
searchResult = DocumentApp.getActiveDocument().getBody().findText(very,searchResult)
}
其次,您在 myFunction()
的上下文中定义函数 onOpen()
和 highlightProblem()
,就像这样
function myFunction(){ // <--- Start of my function
function highlightProblem(){
}
function onOpen(){
}
} //<-- End of my function
您需要单独定义每个函数,如下所示。
您的最终代码:
function findText(searchString) {
Logger.log(searchString)
var doc = DocumentApp.getActiveDocument()
//var doc = DocumentApp.openByUrl("Copy and paste URL of your doc here")
var searchResult = doc.getBody().findText(searchString)
Logger.log(searchResult)
var style = {};
style[DocumentApp.Attribute.BACKGROUND_COLOR] = '#FFFF00';
while (searchResult !== null) {
searchResult.getElement().asText().setAttributes(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),style)
searchResult = DocumentApp.getActiveDocument().getBody().findText(searchString,searchResult)
}
}
function highlightProblem() {
var words = ["very","so","totally","really"]
words.forEach(findText)
}
function onOpen(){
DocumentApp.getUi().createMenu('everythingisnotfine.avi').addItem('Higlight Words That Make You Sound Like a Dandy', 'highlightProblem').addToUi()
}
运行代码的步骤
1) 按原样复制并粘贴到脚本编辑器中。然后 运行 函数 onOpen() 将自定义菜单添加到您的文档
2) 您可以从脚本编辑器或自定义菜单中运行高亮功能
3) 如果你仍然无法 运行 它取消注释(删除 //)这一行并复制并粘贴 URL 如前所述
//var doc = DocumentApp.openByUrl("Copy and paste URL of your doc here")
getBody 在 Apps 脚本中仍然 returns null,尽管我已经四处寻找错误并进行了大部分调试。另一位用户(向我指出了大部分错误)说这段代码中的 getBody 不为空。这是我的代码还是 Apps 脚本的错误?
function myFunction() {
var searchResult = DocumentApp.getActiveDocument().getBody().findText("very",searchResult)
Logger.log(searchResult)
while (searchResult !== null) {
searchResult.getElement().asText().setAttributes(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),"#FF000")
searchResult = DocumentApp.getActiveDocument().getBody().findText("very",searchResult)
}
function highlightProblem() {
var words = ["very","so","totally","really"]
words.forEach(findText)
}
function onOpen(){
DocumentApp.getUi().createMenu('everythingisnotfine.avi').addItem('Higlight Words That Make You Sound Like a Dandy', 'higlightProblem').addToUi()
}}
您的主要问题是您甚至在定义 searchResult 之前就在使用它。
var searchResult = DocumentApp.getActiveDocument().getBody().findText("very",searchResult)
因为您还没有定义 searchResult
,所以您不能通过 findText()
和 searchResult
作为起点。一旦定义了第一个 searchResult 变量,就可以在 while 循环中使用它来查找匹配项
while (searchResult !== null) {
searchResult = DocumentApp.getActiveDocument().getBody().findText(very,searchResult)
}
其次,您在 myFunction()
的上下文中定义函数 onOpen()
和 highlightProblem()
,就像这样
function myFunction(){ // <--- Start of my function
function highlightProblem(){
}
function onOpen(){
}
} //<-- End of my function
您需要单独定义每个函数,如下所示。
您的最终代码:
function findText(searchString) {
Logger.log(searchString)
var doc = DocumentApp.getActiveDocument()
//var doc = DocumentApp.openByUrl("Copy and paste URL of your doc here")
var searchResult = doc.getBody().findText(searchString)
Logger.log(searchResult)
var style = {};
style[DocumentApp.Attribute.BACKGROUND_COLOR] = '#FFFF00';
while (searchResult !== null) {
searchResult.getElement().asText().setAttributes(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),style)
searchResult = DocumentApp.getActiveDocument().getBody().findText(searchString,searchResult)
}
}
function highlightProblem() {
var words = ["very","so","totally","really"]
words.forEach(findText)
}
function onOpen(){
DocumentApp.getUi().createMenu('everythingisnotfine.avi').addItem('Higlight Words That Make You Sound Like a Dandy', 'highlightProblem').addToUi()
}
运行代码的步骤
1) 按原样复制并粘贴到脚本编辑器中。然后 运行 函数 onOpen() 将自定义菜单添加到您的文档
2) 您可以从脚本编辑器或自定义菜单中运行高亮功能
3) 如果你仍然无法 运行 它取消注释(删除 //)这一行并复制并粘贴 URL 如前所述
//var doc = DocumentApp.openByUrl("Copy and paste URL of your doc here")