为什么谷歌应用程序脚本抛出'引用错误:“;粗体“;未定义'使用.setBold()时

Why does Google Apps Script throw 'ReferenceError: "bold" is not defined' when using .setBold()?

本文关键字:未定义 粗体 使用 setBold 引用 脚本 应用程序 谷歌 错误 为什么      更新时间:2024-04-09

上下文:我需要处理/更正许多包含多个特定文本错误的文本文档,用"粗体"突出显示关键字,然后输出结果。我有一个谷歌电子表格,里面有两个工作表:一个有两列"错误的wordforms"answers"replacement wordforms[/em>"(2d数组),我打算随着时间的推移将其添加到其中,并将其用作"call from;"另一个是单列单词集合(1d数组),我指定"关键字"进行检查,然后在目标文档中突出显示。

我尝试过的行之有效的方法:我使用了初学者视频中的基本数组迭代循环(我还不能添加更多链接,我很抱歉),并成功地将body.replaceText()替换为sendEmail(),以将"数据存储"中的更正处理到目标文档中,这几乎完美。它忽略没有完全相同大小写的文本值。。。但这是另一天的问题。

function fixWords() {
// Document to edit
  var td = DocumentApp.openById('docId1');
// Document holding comparison datastore
  var ss = SpreadsheetApp.openById('docId2');
// Create data objects
  var body = td.getBody();
  var sheet = ss.getSheetByName("Word Replacements");
  var range = sheet.getDataRange();
  var values = range.getValues();
// Create a loop (iterate through the cell data)
  for (i=1;i<values.length;i++) {
    fault = values[i][0];
    solution = values[i][2];
    body.replaceText(fault, solution);
  }
}

我尝试过的失败的事情:然后我尝试用replaceText()代码交换setBold()的值,但我得到的最接近的结果是,数组中关键字的第一个实例的样式会正确,但没有其他实例……与使用fixWords函数从word Replacements数组中更正拼写错误单词的所有实例不同。

我在stackoverflow找到了"highlightTextTwo"示例,它运行得很好,但我不知道如何在外部数据源中交换,也不知道如何强制包含的不同迭代循环对我有利。

我扫描了GAS参考资料,观看了谷歌开发者视频,寻找可能适用的片段。。。但很明显,我错过了一些可能是编程基础的东西。但我真的不知道为什么这不像body.replaceText()功能那么简单。

function boldKeywords() { // https://stackoverflow.com/questions/12064972
// Document to edit
  var doc  = DocumentApp.openById('docId1');
// Access the keyword worksheet, create objects
  var ss = SpreadsheetApp.openById('docId2');
  var sheet = ss.getSheetByName("Keywords");
  var range = sheet.getDataRange();
  var values = range.getValues();
  var highlightStyle = {};
  highlightStyle[DocumentApp.Attribute.BOLD] = 'true';
  for (i=1; i<values.length; ++i) {
    textLocation = values[i];
    if (textLocation != null && textLocation.getStartOffset() != -1) {
      textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);
    }
  }
}

这抛出了'TypeError:在对象DIV.中找不到函数getStartOffset(第15行,文件"boldIt")'我想这意味着通过盲目地交换这个代码,它在寻找错误的对象。。。但我不明白为什么它适用于x.replaceText()而不适用于x.setAttributes()x.setBold.getElement().getText().editAsText()。。。似乎没有一个"学习谷歌应用程序脚本的例子"能处理如此低级的平凡、无趣的用例。。。足够让我弄清楚如何将它指向正确的对象,然后操纵"if语句"参数来获得我需要的行为。

我目前的砖墙:我再次发现了这个例子,谷歌应用程序脚本中谷歌文档中字符串的文本格式,尽管DocsList语法已经被弃用,但它似乎很有前景(我很确定)。但现在我被"大胆是没有定义的"抛向我。大胆。。。未定义::张大嘴巴::

function boldKeywords() {
// Access the keyword worksheet, create objects
  var ss = SpreadsheetApp.openById('docId1');
  var sheet = ss.getSheetByName("Keyterms");
  var range = sheet.getDataRange();
  var values = range.getValues();
// Open target document for editing
  var doc  = DocumentApp.openById('docId2');
  var body = doc.getBody();
  // Loop function: find given keyword value from spreadsheet in target document
  // and then bold it (highlight with style 'bold')
  for (i=1; i<values.length; ++i) {
    keyword = values[i];
    target = body.findText(keyword);
    body.replaceText(target,keyword);
    text = body.editAsText();
    text.setBold(text.startOffset, text.endOffsetInclusive, bold);
  }
}

我乐意牺牲我的长子,好让你的庄稼在来年茁壮成长,以换取一些真知灼见。

我将其用于我的脚本,即setStyleAttribute方法。

文件:https://developers.google.com/apps-script/ui_supportedStyles

示例:

TexBox.setStyleAttribute("fontWeight", "bold");

bold参数是布尔数据类型。您需要使用单词truefalse

将"bold"替换为"true"。

text.setBold(text.startOffset, text.endOffsetInclusive, true);

查看文档中的"类型"列:

谷歌文档-setBold