替换后以gDoc为中心

我正在寻找通过应用脚本替换Google文档中的字符串。该字符串将在线上存在,但是替换后,我希望它具有特定的字体,大小和对齐方式。

我已经创建了一种样式来解决所有这些属性(我同时包括了水平和垂直对齐),并且大多数都能正常工作。替换字符串时,替换具有正确的字体,大小和粗体属性。由于某些原因,我无法获得更改理由。


  // Define the style for the replacement string.
  var hdrStyle = {};
  hdrStyle[DocumentApp.Attribute.HORIZONTAL_ALIGnmENT] = 
  DocumentApp.HorizontalAlignment.CENTER;
  hdrStyle[DocumentApp.Attribute.VERTICAL_ALIGnmENT] = 
  DocumentApp.VerticalAlignment.CENTER;
  hdrStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri'; 
  hdrStyle[DocumentApp.Attribute.FONT_SIZE] = 24;
  hdrStyle[DocumentApp.Attribute.BOLD] = true;


  { then later }

  documentBody = DocumentApp.openById(fileId).getBody();

  hdrElem = documentBody.findText("old string").getElement();            
  hdrText = hdrElem.asText().setText("new string");

  // Force our 'header style':
  hdrElem.setattributes(hdrStyle);

我尝试在findText之后和之后(如此处)设置样式,但是居中没有变化。

我看到有一个段落居中,但是我不清楚如何“获取”与在查找中返回的元素相关联的段落。

我希望这是一组简单的电话-但是用尽了主意(和耐心)。.

任何帮助将不胜感激!

fgvlty 回答:替换后以gDoc为中心

您可以在hdrElem上使用getParent()来获取要对其应用样式的父段落。

https://developers.google.com/apps-script/reference/document/text#getParent()

documentBody = DocumentApp.openById(fileId).getBody();

hdrElem = documentBody.findText("old string").getElement();            
hdrText = hdrElem.asText().setText("new string");

var hdrParent = hdrElem.getParent()

// Force our 'header style':
hdrParent.setAttributes(hdrStyle);

本文链接:https://www.f2er.com/3142916.html

大家都在问