在 Excel 加载项中覆盖或清除页眉/页脚的内容 [英] Override or clear content of header/footer in Add-in for Excel

查看:104
本文介绍了在 Excel 加载项中覆盖或清除页眉/页脚的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为 Excel 工作簿实现一些自动化.一部分是在加载项按钮被触发时重置带有信息的标题(如版本号和当前周数).

I'm currently trying to implement some automations for an Excel Workbook. Part of it is to reset a header with information (like a version number and the current week number), whenever the add-in button is triggered.

我设置标题的代码:

async function run() {
  await Excel.run(async (context) => {
    const sheet: Excel.Worksheet = context.workbook.worksheets.getActiveWorksheet();
    const headerFooter: Excel.HeaderFooter = sheet.pageLayout.headersFooters.defaultForAllPages;

    headerFooter.load("centerHeader");
    await context.sync();
    console.log(`Header: ${headerFooter.centerHeader}`);

    headerFooter.centerHeader = "Center";

    await context.sync();
  });
}

我还做了一个要点:作为要点的最小示例

我的问题:

  • 如果未设置标题,则一切正常.
  • 但是,如果为左标题设置了某些内容并且 Center-Hedaer 为空,则Center"附加到左标题.Gif 显示效果
  • 如果使用了格式,例如&ZCenter"(&Z 使文本居中),新内容将附加到现有内容,即CenterCenter",而不是覆盖它.这只发生在手动插入内容而不是我的代码 (???) 时.Gif 显示效果
  • If no header is set, everything works fine.
  • However, if something is set for the left header and the Center-Hedaer is empty, "Center" is attached to the left header. Gif to show effect
  • If formatting is used, like "&ZCenter" (&Z to center the text), the new content is attached to the existing content, saying "CenterCenter", instead of overwriting it. This only happens if the content is inserted manually and not with my code (???). Gif to show effect

根据我的理解 headerFooter.centerHeader = "Center"; 应该覆盖现有的标题.为了提示我在设置新字符串之前已经尝试设置 headerFooter.centerHeader = "";headerFooter.centerHeader = null;,但没有效果.

In my understanding headerFooter.centerHeader = "Center"; should overwrite the existent header. To promt that i already tried to set headerFooter.centerHeader = ""; and headerFooter.centerHeader = null;, before setting the new string, but without effect.

我在 Windows 10 64 位、Office 上运行它365 商业版,构建 2104,32 位

I run it on Windows 10 64-bit, Office 365 Business, Build 2104, 32-bit

编辑 18. 2021 年 5 月:

我录制了更长的视频来展示正在发生的事情.

I recorded longer videos to show what is happening.

1. 如果在标题的左边部分写了一些东西,一个像Center"这样的简单字符串以编程方式为中心部分设置,字符串连接到左侧.如果所有部分都为空,则字符串正确设置为中心部分.

1. If something is written in the left section of the header an a plain string like "Center" is programmatically set for the center section, the string is concatenated to the left side. If all sections are empty, the string is set correctly to the center section.

关于上面提到的要点的空白表格中发生的事情的视频:Concat中心";在左侧部分

Video on what is happening in a blank sheet with the gist mentioned above: Concat "Center" in left section

2. 如果标题的字符串中包含格式(如&Z"),则它的位置是正确的 - 即使左侧部分中写有某些内容.但是,如果中间部分已经有一些文本,与要插入的文本有些不同,则新文本覆盖现有文本.如果随后选择了中间部分,则文本将被连接起来.

2. If formatting (like "&Z") is included in the string for the header, it is positioned correctly - even if there is something written in the left section. However, if there is already some text in the center section, which is somehow different from the text to be inserted, the new text overlays the existent text. If the center section is then selected, the texts are concatenated.

我在更改到控制台之前输出标题(参见上面的代码),即使标题中有文本,它也不会打印到控制台,即 makro 假定标题为空.还有一点很奇怪:在某些情况下,当它不能正常工作时,标题的中心部分会延伸到整个页面,甚至覆盖其他标题部分.

I output the header before altering to the console (see code above) and even though there is text in the header, it is not printed to the console, i.e. the makro assumes the header is empty. What is also weird: in the cases, when it does not work properly the center section of the header stretches across the whole page, overlaying even the other header sections.

这是一个视频:在中心部分叠加文字

3. 与 1. 类似,但在右侧:如果标题的右侧部分有文本,并且中间部分的文本不包含格式,则中心部分连接到右侧部分的文本.如果包含格式,则插入工作正常.

3. Similar to 1. but on the right: If there is text in the right section of the header, and if the text for the center section does not contain formatting, the text for the center section is concatenated to the text in the right section. If formatting is included, the insertion works correctly.

这里还有一个视频:Concat Center"在右侧

我在这里错过了什么?以及如何覆盖现有的标题内容?

What am I missing here? And how can I overwrite the existent header content?

谢谢!

推荐答案

遗憾的是仍然无法重现您的更多信息.那你能帮忙验证其他方式吗?1. 将locale改成en-us [https://www.tenforums.com/tutorials/132050-change-system-locale-windows-10-a.html] 看看能不能repro?我的窗户是英文的,我试图将语言环境设置为德国仍然无法重现.2. 可以在线试一下excel,是不是windows下才出现这个问题?如果可以的话,能否再给我分享两个excel文件?一个是excel文件,你可以用左边的标题集来重现,但中间和右边是空的,但是用api设置中心可以重现这个问题.另一个excel文件是left header由UI设置,center由api设置,然后center与UI中的left相连.将确认文件是正确保存还是只是 UI 渲染问题.感谢您的支持.

unfortunately still cannot repro with your more info. So can you help verify other ways? 1. Change the locale to en-us [https://www.tenforums.com/tutorials/132050-change-system-locale-windows-10-a.html] and check whether it can repro? My windows is English, and I tried to set locale as Germany still cannot repro. 2. Can you try excel online, whether the issue only repro in windows? If it is possible, can you share me two more excel files? One is the excel file you can repro with left header set, but middle and right are empty, but setting center with api can repro the issue. The other excel file is that left header is set by UI, and center is set by api and then center is connected with left in UI. Will confirm whether the file was saved right or just UI render issue. Thanks for your support.

这篇关于在 Excel 加载项中覆盖或清除页眉/页脚的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆