使用 Google 脚本更改 Google 文档中的段落标题文本

发布于 2025-01-14 00:12:00 字数 983 浏览 2 评论 0原文

我有一个具有以下样式的 Google 文档:

我有什么

实际目录是:

Table Of Contents

您可以看到它有几种样式:

  • 标题(14pt,蓝色,下划线)
  • 副标题(12 点,黑色,下划线)
  • 标题 1(11 点)
  • 标题 2(10 点,粗体)
  • 普通文本(10 点)

我想制作一个在活动文档上运行的脚本并存储每个标题的内容进行以下更改:

  • 标题 1(14pt,蓝色,下划线)
  • 标题 2(11pt)
  • 普通文本(10pt,第一行包含“主题 n°)”,粗体)

运行脚本后的最终结果应该为:

我想要什么

新目录应如下所示:

新目录

所以主要的变化是将HEADER 2内容放入普通文本行,因此它从目录中消失。有没有办法编写一个脚本来解决这个问题?

I have a Google Docs with the following style:

What I have

The actual TOC is:

Table Of Contents

You can see it has a few styles:

  • Title (14pt, in blue, underlined)
  • Subtitle (12pt, in black, underlined)
  • Header 1 (11pt)
  • Header 2 (10pt, in bold)
  • Normal text (10pt)

I would like to make a script that runs on the active document and store the content of each heading to make the following changes:

  • Header 1 (14pt, in blue, underlined)
  • Header 2 (11pt)
  • Normal text (10pt, the first line contains "Topic nº)" in bold)

The final result after running the script should be:

What I want

The new TOC should look like:

New Table Of Contents

So the main change is to put HEADER 2 content into a Normal text line so it dissapears from TOC. Is there any way to code a script that solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2025-01-21 00:12:00

请参阅下面的脚本:

function findHeader() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  // Define the search parameters
  var searchType = DocumentApp.ElementType.PARAGRAPH;
  var searchHeadings = [DocumentApp.ParagraphHeading.HEADING2, DocumentApp.ParagraphHeading.HEADING1, DocumentApp.ParagraphHeading.TITLE];
  var replacementHeadings = [DocumentApp.ParagraphHeading.NORMAL, DocumentApp.ParagraphHeading.HEADING2, DocumentApp.ParagraphHeading.HEADING1];
  var searchResult = null;
  
  // Style changes you want to apply
  var H2_N = {};
  H2_N[DocumentApp.Attribute.FONT_SIZE] = '11';
  // Since HEADING 2 that are made to be Normal becomes first Normal heading and first Normal heading is to be bold, 
  // You can directly make it bold here to reduce redundancy
  H2_N[DocumentApp.Attribute.BOLD] = true;

  var H1_H2 = {};
  H1_H2[DocumentApp.Attribute.FONT_SIZE] = '11';

  var T_H1 = {};
  T_H1[DocumentApp.Attribute.FONT_SIZE] = '14';
  T_H1[DocumentApp.Attribute.FOREGROUND_COLOR] = '#0000FF';
  T_H1[DocumentApp.Attribute.UNDERLINE] = true;

  var styles = [H2_N, H1_H2, T_H1];

  // Loop all searchHeadings
  searchHeadings.forEach((searchHeading, index) => {  
    // Search until the wanted searchHeading is found
    while (searchResult = body.findElement(searchType, searchResult)) {
      var par = searchResult.getElement().asParagraph();
      if (par.getHeading() == searchHeading) {
        // Replace with its corresponding replacementHeadings and apply approriate styles
        par.setHeading(replacementHeadings[index]);
        par.setAttributes(styles[index]);
      }
    }
  });
}

脚本行为摘要:

  • 降级以下标题(T -> H1H1 -> H2H2 -> N >)
  • 每次降级应用不同的样式:
    • <代码>T -> H1:14PT,蓝色,下划线
    • <代码>H1 -> H2:11PT
    • <代码>H2 -> N:11PT,粗体
  • H2 -> N 直接变为粗体,因为您希望前 N 段为粗体。而当H2降级的时候,就变成了N,成为了第一个N。所以我们可以直接把H2降级的时候变成粗体。

输出:

输出

参考文献:

See script below:

function findHeader() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  // Define the search parameters
  var searchType = DocumentApp.ElementType.PARAGRAPH;
  var searchHeadings = [DocumentApp.ParagraphHeading.HEADING2, DocumentApp.ParagraphHeading.HEADING1, DocumentApp.ParagraphHeading.TITLE];
  var replacementHeadings = [DocumentApp.ParagraphHeading.NORMAL, DocumentApp.ParagraphHeading.HEADING2, DocumentApp.ParagraphHeading.HEADING1];
  var searchResult = null;
  
  // Style changes you want to apply
  var H2_N = {};
  H2_N[DocumentApp.Attribute.FONT_SIZE] = '11';
  // Since HEADING 2 that are made to be Normal becomes first Normal heading and first Normal heading is to be bold, 
  // You can directly make it bold here to reduce redundancy
  H2_N[DocumentApp.Attribute.BOLD] = true;

  var H1_H2 = {};
  H1_H2[DocumentApp.Attribute.FONT_SIZE] = '11';

  var T_H1 = {};
  T_H1[DocumentApp.Attribute.FONT_SIZE] = '14';
  T_H1[DocumentApp.Attribute.FOREGROUND_COLOR] = '#0000FF';
  T_H1[DocumentApp.Attribute.UNDERLINE] = true;

  var styles = [H2_N, H1_H2, T_H1];

  // Loop all searchHeadings
  searchHeadings.forEach((searchHeading, index) => {  
    // Search until the wanted searchHeading is found
    while (searchResult = body.findElement(searchType, searchResult)) {
      var par = searchResult.getElement().asParagraph();
      if (par.getHeading() == searchHeading) {
        // Replace with its corresponding replacementHeadings and apply approriate styles
        par.setHeading(replacementHeadings[index]);
        par.setAttributes(styles[index]);
      }
    }
  });
}

Script Behavior Summary:

  • Demotes the following headings (T -> H1, H1 -> H2, H2 -> N)
  • Applies different styles per demotion:
    • T -> H1: 14PT, BLUE, UNDERLINED
    • H1 -> H2: 11PT
    • H2 -> N: 11PT, BOLD
  • H2 -> N is directly turned to bold as you want the first N paragraph to be bold. And when H2 is demoted, it turns into N and becomes the first N. So we can directly turn H2 into bold when demoted.

Output:

output

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文