提问者:小点点

使用google doc扩展脚本在页脚中居中文本


这是我的代码。家庭和大小的工作,但它不是中心。

function styleFooter() {
  var body = DocumentApp.getActiveDocument();
  var foot = body.getFooter();
  var style = {};
  style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
  style[DocumentApp.Attribute.FONT_FAMILY] = 'Helvetica Neue';
  style[DocumentApp.Attribute.FONT_SIZE] = 9;
  foot.setAttributes(style);  
}

共1个答案

匿名用户

这是因为您需要获取段落()并迭代页脚的每个段落。试试这个,它对我有用:

function styleFooter() {
  var body = DocumentApp.getActiveDocument();
  var foot = body.getFooter().getParagraphs(); // gets a list of your footer paragraphs
  var style = {};
  style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
  style[DocumentApp.Attribute.FONT_FAMILY] = 'Helvetica Neue';
  style[DocumentApp.Attribute.FONT_SIZE] = 9;

  // iterates each of your footer paragraphs
  foot.forEach(function(element) {
    element.setAttributes(style); 
  })
}

从Google Docs文档中:

HorizontalRule可以包含在ListItem或段落中,但它本身不能包含任何其他元素。