marcomk
marcomk

Reputation: 147

Google Apps Script: weird page layout in a script formatted document

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.

Upvotes: 0

Views: 1345

Answers (1)

Serge insas
Serge insas

Reputation: 46792

That was an interesting problem ;-)

I copied your doc, ran the script and had a surprise : nothing happened !

It took me a few minutes to realize that the copy I just made had no style defined for headings, everything was for some reason in courrier new 12pt, including the headings.

I examined the log and saw the indent values, played with that a lot to finally see that the headings were there but not changing the style.

So I went in the doc menu and set 'Use my default style and... everything looks fine, see screen capture below. enter image description here

So now your question : it appears that there must be something wrong in your style definition, by "wrong" I mean something that changes more than just the font Style and size but honestly I can't see any way to guess what since I'm unable to reproduce it... Please try resetting your heading styles and re-define your default.... and tell us what happens then.

PS : here are my default heading styles : (and the url of my copy in view only :https://docs.google.com/document/d/1yP0RRCrRSsQc9zCk-sdfu5olNGDkoIrabXanII4qUG0/edit?usp=sharing )

enter image description here

Upvotes: 1

Related Questions