setHeadingAttributes以在带有GAS的文档中重新定义标题样式 [英] setHeadingAttributes to redefine a heading style in Documents w/ GAS

查看:50
本文介绍了setHeadingAttributes以在带有GAS的文档中重新定义标题样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望重新定义文档的标题样式.我的理解是,getHeadingAttributes和setHeadingAttributes可以让我做到这一点

I am hoping to re-define the heading styles for a document. My understanding is that getHeadingAttributes and setHeadingAttributes would let me do this

https://developers.google.com/apps-script/reference/document/body#setheadingattributesparagraphheading-属性

这就是我要测试的方式-

This is how I'm going about testing this --

function main() { 
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();    

  styledef=body.getHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1); 

  Logger.log("Before: \n" + JSON.stringify(styledef) ); 

  styledef[DocumentApp.Attribute.UNDERLINE] = true;
  styledef[DocumentApp.Attribute.BACKGROUND_COLOR] = '#ffff00';

  body.setHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1, styledef);

  styledef=body.getHeadingAttributes(DocumentApp.ParagraphHeading.HEADING1);

  Logger.log("After: \n" + JSON.stringify(styledef)); 

}

此主体的标题属性已修改,如记录器的输出所揭示:

This Body has its headings attributes modified, as revealed by the Logger output:

    [17-10-04 09:20:53:379 MDT] Before: 
    {"FONT_SIZE":18,"ITALIC":false,"HORIZONTAL_ALIGNMENT":{},"INDENT_END":0,"INDENT_START":0,"LINE_SPACING":1,"UNDERLINE":false,"BACKGROUND_COLOR":null,"INDENT_FIRST_LINE":0,"SPACING_BEFORE":12,"SPACING_AFTER":0,"STRIKETHROUGH":false,"FOREGROUND_COLOR":"#000000","BOLD":true,"FONT_FAMILY":"Arial","VERTICAL_ALIGNMENT":{}}

    [17-10-04 09:20:53:382 MDT] After: 
    {"FONT_SIZE":18,"ITALIC":false,"HORIZONTAL_ALIGNMENT":{},"INDENT_END":0,"INDENT_START":0,"LINE_SPACING":1,"UNDERLINE":true,"BACKGROUND_COLOR":"#ffff00","INDENT_FIRST_LINE":0,"SPACING_BEFORE":12,"SPACING_AFTER":0,"STRIKETHROUGH":false,"FOREGROUND_COLOR":"#000000","BOLD":true,"FONT_FAMILY":"Arial","VERTICAL_ALIGNMENT":{}}

UNDERLINE changed from false to true

BACKGROUND_COLOR changed from null to #ffff00

但是在此脚本所绑定的我的文档中,标题1的样式对于具有该标题的现有段落并未更改.标记为标题1的新段落也不具有我尝试进行的更改的样式.

But in my document to which this script is bound, the styling of Heading 1 has not changed for existing paragraphs with that Heading. Nor are new paragraphs marked Heading 1 styled with the changes I've tried to make.

偶然地,当我在NORMAL而不是HEADING1上运行相同的功能时,更改立即可见:每个标题(普通,标题,字幕,标题1等)都应用了下划线和背景色.

Incidentally, when I run that same function on NORMAL instead of HEADING1, the changes are instantly visible: Every Heading (Normal, Title, Subtitle, Heading 1, etc...) has the underline and background color applied.

我觉得我从根本上误解了这些方法(getHeadingAttributes,setHeadingAttributes)的目的.如果不是这些方法,我想找到正确的方法来重新定义文档的标题样式.你能为我指出正确的方向吗?

I feel like I've fundamentally misunderstood the purpose of these methods (getHeadingAttributes, setHeadingAttributes). If not these methods, I'd like to find the correct way to re-define Heading stylings for a document. Can you point me in the right direction?

非常感谢,

推荐答案

此功能尚未发挥很多,但请检查

Haven't played around much of this yet, but check the Enum ParagraphHeading as it seems close to what you're getting at.

使用 ParagraphHeading 枚举配置 ParagraphElement 的标题样式.

Use the ParagraphHeading enumeration to configure the heading style for ParagraphElement.

 var body = DocumentApp.getActiveDocument().getBody();

 // Append a paragraph, with heading 1.
 var par1 = body.appendParagraph("Title");
 par1.setHeading(DocumentApp.ParagraphHeading.HEADING1);

 // Append a paragraph, with heading 2.
 var par2 = body.appendParagraph("SubTitle");
 par2.setHeading(DocumentApp.ParagraphHeading.HEADING2);

 // Append a paragraph, with normal heading.
 var par3 = body.appendParagraph("Text");
 par3.setHeading(DocumentApp.ParagraphHeading.NORMAL);

这篇关于setHeadingAttributes以在带有GAS的文档中重新定义标题样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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