【问题标题】:Is content control object in header or footer countable?页眉或页脚中的内容控件对象是否可数?
【发布时间】:2018-06-16 18:35:01
【问题描述】:

我在所有页面的word文档标题中插入了内容控件,但后来我注意到“contentcontrol.items.length”结果始终为1。它不应该等于页码吗?为什么总是一个?谁能解释一下?代码:

await Word.run(async (context) => {  

        var mySections = context.document.sections;        
        context.load(mySections, 'body/style');
        await context.sync().then(function () {

          var myHeader = mySections.items[0].getHeader("primary");
          myHeader.clear();

          myHeader.insertHtml('<a href="https://www.cgi.se/">CGI</a>',"Start");

          var myContentControl = myHeader.insertContentControl();
          myContentControl.tag = "header";
          myContentControl.appearance = "Tags"; 

          return context.sync();
        });
      });

然后来统计内容控件:

  async function ContentControlCount() {
    // Run a batch operation against the Word object model.
    await Word.run(function (context) {

      // Create a proxy sectionsCollection object.
      var mySections = context.document.sections;

      // Queue a commmand to load the sections.
      context.load(mySections, 'body/style');

      return context.sync().then(function () {

        var myHeader = mySections.items[0].getHeader('primary');
        return context.sync().then(function () {

          var contentControl = myHeader.contentControls;

          // Queue a command to load the text property for a content control.
          context.load(contentControl, 'text, tag');

          // Synchronize the document state by executing the queued commands, and return a promise to indicate task completion.
          return context.sync().then(function () {
            console.log(contentControl.items.length);
            console.log(contentControl.items[0].text);

          });
        });
      });

    })
      .catch(function (e) { console.log(e.message); })

  }

【问题讨论】:

  • 如果这是一个编码问题,那么添加您到目前为止的尝试。否则,它应该移动到superuser.com
  • 代码已添加,谢谢。
  • 谢谢。现在,您的问题是对我们知识库的宝贵贡献。
  • 任何人都可以分享一些光?谢谢!

标签: wordpress office-js office-addins word-addins


【解决方案1】:

每个部分只有一个标题。您应该将其视为模板。即使它呈现在每个页面上,它仍然是相同的标题对象。因此,如果您向其中添加内容控件,则无论该部分中有多少页,这只是一个内容控件。

【讨论】:

  • 谢谢瑞克,现在我明白了这个标题是如何工作的。我在计算内容控件实际上是在计算页码,因为当前的办公室 API 不支持页码,我在想也许我可以获取页眉编号然后我知道页码,但显然它不起作用这种方式..
猜你喜欢
  • 1970-01-01
  • 2023-03-28
  • 2017-11-06
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
相关资源
最近更新 更多