在 Google 文档段落中的粗体文本周围添加 <b> 标签

问题描述 投票:0回答:1

我创建这个的目的是为了将文章转换为 html 格式(你可能已经从我尝试将标题放入标签等的方式中看到了)。但是,我在文档中查找和处理粗体文本时遇到了麻烦。我尝试了多种方法,也请求AI帮助。

function myFunction() {
  var outputHTML = DocumentApp.openByUrl("https://docs.google.com/document/d/1KZcWSbrPg/edit?usp=sharing");
  var body2 = outputHTML.getBody();
  body2.clear(); //here is the cleared HTML output body
  var body = DocumentApp.getActiveDocument().getBody();
  var elements = body.getNumChildren(); //number of lines


  for (var i = 0; i < elements; i++) {
    var line = body.getChild(i); //selects each line
    var type = line.getType().toString(); //type of the line
    
    if (type === "PARAGRAPH") {
      var paragraph = line.asParagraph();
      if (paragraph.getHeading() === DocumentApp.ParagraphHeading.NORMAL) {
        body2.appendParagraph("<p>" + paragraph.getText() + "<p>");

      } else if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING1) {
        body2.appendParagraph("<h1>" + paragraph.getText() + "</h1>");

      } else if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING2) {
        body2.appendParagraph("<h2>" + paragraph.getText() + "</h2>");

      } else{
        console.log("unknown");
      }
    } else {
      console.log(type);
    }
  }
}

我想在段落中的粗体文本周围添加 开始标记和结束标记。当代码逐行读取文档时。

google-apps-script google-docs
1个回答
0
投票

这是文档中的示例:

// Define a style with yellow background.
var highlightStyle = {};
highlightStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#FFFF00';
highlightStyle[DocumentApp.Attribute.BOLD] = true;

// Insert "Hello", highlighted.
DocumentApp.getActiveDocument().editAsText()
  .insertText(0, 'Hello\n')
  .setAttributes(0, 4, highlightStyle);
© www.soinside.com 2019 - 2024. All rights reserved.