google 文档加粗文本直到分号逐行应用程序脚本错误

问题描述 投票:0回答:1
function boldTextInLines() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var text = body.getText();

  var lines = text.split("\n"); 

  for (var i = 0; i < lines.length; i++) {
    var line = lines[i];
    var endPos = line.indexOf(':');
    console.log(endPos);
    
    if (endPos !== -1) {
      
      body.editAsText().setBold(i, 0, endPos, true);
    }else{
      console.log(endPos);
    }
    
  }
}

传递给

setBold
的参数似乎是正确的:

  • i
    :行索引

  • 0
    :行中文本的起始偏移量

  • endPos + 1
    :行中文本的结束偏移量(冒号后一个字符)

  • true
    :将文本设置为粗体

    异常:参数 (number,number,number,(class)) 与 DocumentApp.Text.setBold 的方法签名不匹配。 boldTextInLines @ Kod.gs:14 bu 错误,我该如何纠正它?

javascript google-apps-script google-docs-api method-signature line-by-line
1个回答
0
投票

function boldTextInLines() {
  var doc = DocumentApp.getActiveDocument();

  var body = doc.getBody();

  var text = body.getText();
 

  var lines = text.split("\n"); 
  console.log(lines)
  
  for (var i = 0; i < lines.length; i++) {
    var line = lines[i];
    console.log(line)
    var endPos = line.indexOf(":");
    console.log(endPos);

    
    if (endPos!==-1) {

      var textBeforeColon = line.substring(0, endPos);
      var startIndex = body.getText().indexOf(textBeforeColon);
      var endIndex = startIndex + textBeforeColon.length;
      console.log(textBeforeColon)
      body.editAsText().setBold(startIndex,endIndex, true);
    }else{

    }
    
  }
}

这就是这个问题的答案

© www.soinside.com 2019 - 2024. All rights reserved.