0

I'm working on a script that applies custom headings to a plain text document imported in Google Docs. The scripts works pretty much as it should. However the resulting document has a weird layout, as if random page breaks were inserted here and there. But there are no page breaks and I can't understand the reason of this layout. Checking the paragraph attributes give me no hints on what is wrong.

Here is the text BEFORE the script is applied:

https://docs.google.com/document/d/1MzFvlkG13i3rrUcz5jmmSppG4sBH6zTXr7RViwdqaIo/edit?usp=sharing

You can make a copy of the document and execute the script (from the Scripts menu, choose Apply Headings). The script applies the appropriate heading to the scene heading, name of the character, dialogue, etc.

As you can see, at the bottom of page 2 and 3 of the resulting document there is a big gap and I can't figure out why. The paragraph attributes seem ok to me...

Here is a copy of the script:

// Apply headings to sceneheadings, actions, characters, dialogues, parentheticals 
// to an imported plain text film script;

function ApplyHeadings() {
  var pars = DocumentApp.getActiveDocument().getBody().getParagraphs(); 
  for(var i=0; i<pars.length; i++) {
    var par = pars[i];
    var partext = par.getText();
    var indt = par.getIndentStart();
    Logger.log(indt);
    if (indt > 100 && indt < 120) {
      var INT = par.findText("INT.");
      var EXT = par.findText("EXT.");
      if (INT != null || EXT != null) {
        par.setHeading(DocumentApp.ParagraphHeading.HEADING1);
        par.setAttributes(ResetAttributes());
      }
      else {
        par.setHeading(DocumentApp.ParagraphHeading.NORMAL);
        par.setAttributes(ResetAttributes());
      }
    }
    else if (indt > 245 && indt < 260) {
      par.setHeading(DocumentApp.ParagraphHeading.HEADING2);
      par.setAttributes(ResetAttributes());
    }
    else if (indt > 170 && indt < 190) {
      par.setHeading(DocumentApp.ParagraphHeading.HEADING3);
      par.setAttributes(ResetAttributes());
    }
    else if (indt > 200 && indt < 240) {
      par.setHeading(DocumentApp.ParagraphHeading.HEADING4);
      par.setAttributes(ResetAttributes());       
    }
  }
}

// Reset all the attributes to "null" apart from HEADING;
function ResetAttributes() {
  var style = {};
  style[DocumentApp.Attribute.STRIKETHROUGH] = null;
  style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = null;
  style[DocumentApp.Attribute.INDENT_START] = null;
  style[DocumentApp.Attribute.INDENT_END] = null;
  style[DocumentApp.Attribute.INDENT_FIRST_LINE] = null;
  style[DocumentApp.Attribute.LINE_SPACING] = null;
  style[DocumentApp.Attribute.ITALIC] = null;
  style[DocumentApp.Attribute.FONT_SIZE] = null;
  style[DocumentApp.Attribute.FONT_FAMILY] = null;
  style[DocumentApp.Attribute.BOLD] = null;
  style[DocumentApp.Attribute.SPACING_BEFORE] = null;
  style[DocumentApp.Attribute.SPACING_AFTER] = null;
  return style;
}

A couple of screenshots to make the problem more clear.

This is page 2 of the document BEFORE the script is applied.

Page 2 before script

This is page two AFTER the script is applied. Headings are applied correctly but... Why the white space at the bottom?

enter image description here

Note: if you manually re-apply HEADING2 to the first paragraph of page 3 (AUDIO TV), the paragraph will jump back to fill the space at the bottom of page 2. This action, however, doesn't change any attribute in the paragraph. So why the magic happens?

Thanks a lot for your patience.

4

1 回答 1

1

这是一个有趣的问题;-)

我复制了您的文档,运行了脚本并有一个惊喜:什么也没发生!

我花了几分钟才意识到我刚刚制作的副本没有为标题定义样式,出于某种原因,所有内容都在 courier new 12pt 中,包括标题。

我检查了日志并看到了缩进值,玩了很多,最终看到标题在那里但没有改变样式。

所以我进入了文档菜单并设置了“使用我的默认样式并且......一切看起来都很好,请参见下面的屏幕截图。 在此处输入图像描述

所以现在你的问题:看起来你的风格定义一定有问题,“错误”是指一些改变不仅仅是字体样式和大小的东西,但老实说,我看不出有什么方法可以猜测是什么,因为我我无法重现它...请尝试重置您的标题样式并重新定义您的默认值....然后告诉我们会发生什么。

PS:这是我的默认标题样式:(以及我的副本的网址仅在视图中:https ://docs.google.com/document/d/1yP0RRCrRSsQc9zCk-sdfu5olNGDkoIrabXanII4qUG0/edit?usp=sharing )

在此处输入图像描述

于 2013-11-14T20:31:33.173 回答