You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用docx的addSection方法报错:该属性为私有属性,求解决方案

问题解决:Property 'addSection' is private 报错

报错信息

Property 'addSection' is private and only accessible within class 'File'

问题代码

const versionList = Object.keys(releaseNote)
for (const key of versionList) {
  // Skip the version if none of its release notes are selected for cherry-picking.
  const shouldInclude = cherryPick[key].some(Boolean)
  if (!shouldInclude) {
    continue
  }

  // Add version title
  doc.addSection({
    properties: {},
    children: [
      new Paragraph({
        children: [
          new TextRun({
            text: key,
            bold: true,
            size: 16,
          }),
        ],
      }),
    ],
  })

  // Iterate over each release note in the current version.
  for (let i = 0; i < releaseNote[key].length; i++) {
    // Check if that release note is selected or not
    if (cherryPick[key][i]) {
      // Add a bullet point before each release note.
      doc.addSection({
        properties: {},
        children: [
          new Paragraph({
            children: [
              new TextRun({
                text: '•',
                bold: true,
              }),
              new TextRun({
                text: releaseNote[key][i],
              }),
            ],
          }),
        ],
      })
    }
  }
}

解决方案

报错原因是你调用了docx库中File类的私有方法addSection,该方法不允许外部直接访问。以下是两种可行的修复方案:

方案1:使用公开的章节创建方法(适配docx.js v8+)

最新版docx库中,创建章节需要调用公开的createSection方法,再向章节内添加内容:

const versionList = Object.keys(releaseNote)
for (const key of versionList) {
  const shouldInclude = cherryPick[key].some(Boolean)
  if (!shouldInclude) {
    continue
  }

  // 创建新章节
  const section = doc.createSection({ properties: {} })

  // 添加版本标题段落
  section.addParagraph(
    new Paragraph({
      children: [
        new TextRun({
          text: key,
          bold: true,
          size: 16,
        }),
      ],
    })
  )

  // 添加选中的发布说明列表项
  for (let i = 0; i < releaseNote[key].length; i++) {
    if (cherryPick[key][i]) {
      section.addParagraph(
        new Paragraph({
          children: [
            new TextRun({
              text: '•',
              bold: true,
            }),
            new TextRun({
              text: ' ' + releaseNote[key][i], // 加空格分隔符号与文本
            }),
          ],
        })
      )
    }
  }
}

方案2:无需多章节时,直接在默认章节添加内容

如果你的文档不需要拆分多个独立章节,仅需标题和列表内容,直接使用addParagraph即可:

const versionList = Object.keys(releaseNote)
for (const key of versionList) {
  const shouldInclude = cherryPick[key].some(Boolean)
  if (!shouldInclude) {
    continue
  }

  // 添加版本标题
  doc.addParagraph(
    new Paragraph({
      children: [
        new TextRun({
          text: key,
          bold: true,
          size: 16,
        }),
      ],
    })
  )

  // 添加列表项
  for (let i = 0; i < releaseNote[key].length; i++) {
    if (cherryPick[key][i]) {
      doc.addParagraph(
        new Paragraph({
          children: [
            new TextRun({
              text: '•',
              bold: true,
            }),
            new TextRun({
              text: ' ' + releaseNote[key][i],
            }),
          ],
        })
      )
    }
  }
}

关键说明

  • 私有方法addSection仅能在File类内部调用,外部必须使用公开的createSection方法创建章节。
  • 若无需拆分章节,直接用addParagraph添加内容更符合docx文档的常规结构,代码也更简洁。

内容的提问来源于stack exchange,提问作者KoyOutkarm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 04:48:25